Submission #516528

# Submission time Handle Problem Language Result Execution time Memory
516528 2022-01-21T12:49:09 Z Dasha_Gnedko Segments (IZhO18_segments) C++17
23 / 100
1559 ms 65540 KB
#include <bits/stdc++.h>

#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")

using namespace std;

using namespace __gnu_pbds;
template <typename T> using ordered_set = tree <T, null_type, less < T >, rb_tree_tag, tree_order_statistics_node_update>;

mt19937 gen(time(0));

#define ll long long
#define ld long double
#define pb push_back
#define F first
#define S second
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
#define sz(a) int32_t(a.size())
#define endl '\n'
//#define int long long

const int N = 1e5 + 100;
const int M = 31;
const int mod = 998244353;
const int inf = 1e9 + 7;

vector < int > ve;

struct Fenwick
{
    vector < ordered_set < pair < int, int > > > f;
    int n, state = 0;

    void build()
    {
        n = sz(ve);
        f.resize(n);
    }

    void upd(int k, pair < int, int > p, int fl)
    {
        int l = 0, r = sz(ve) - 1, i = n;
        while (l <= r)
        {
            int mid = (l + r) / 2;
            if (ve[mid] <= k)
            {
                i = mid;
                l = mid + 1;
            }
            else r = mid - 1;
        }
        for(; i < n; i = (i | (i + 1)))
        {
            if (fl) f[i].insert(p);
            else f[i].erase(p);
        }
    }

    int sum(int i, pair < int, int > p)
    {
        int ans = 0;
        for(; i >= 0; i = (i & (i + 1)) - 1)
            ans += f[i].order_of_key(p);
        return ans;
    }

    int get_ans(int i, int l, int r)
    {
        int ans = sum(i, {r + 1, -1});
        if (l) ans -= sum(i, {l, -1});
        return ans;
    }

    int get(int l, int r, int k)
    {
        if (l > r) return 0;
        int ans = get_ans(n - 1, l, r);
        int le = 0, ri = sz(ve) - 1, i = -1;
        while (le <= ri)
        {
            int mid = (le + ri) / 2;
            if (ve[mid] <= k)
            {
                i = mid;
                le = mid + 1;
            }
            else ri = mid - 1;
        }
        if (i) ans -= get_ans(i - 1, l, r);
        return ans;
    }

    int get1(int l, int r, int k)
    {
        if (l > r) return 0;
        int le = 0, ri = sz(ve) - 1, i = -1;
        while (le <= ri)
        {
            int mid = (le + ri) / 2;
            if (ve[mid] <= k)
            {
                i = mid;
                le = mid + 1;
            }
            else ri = mid - 1;
        }
        return get_ans(i, l, r);
    }

    int get2(int r, int k)
    {
        if (r < 0) return 0;
        int le = 0, ri = sz(ve) - 1, i = -1;
        while (le <= ri)
        {
            int mid = (le + ri) / 2;
            if (ve[mid] <= k)
            {
                i = mid;
                le = mid + 1;
            }
            else ri = mid - 1;
        }
        return sum(i, {r + 1, -1});
    }
};

Fenwick f1, f2;
vector < pair < pair < int, int >, pair < int, int > > > ask;
//ordered_set < pair < int, int > > st1, st2;

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif // LOCAL

    int q, T;
    cin >> q >> T;
    for(int i = 0; i < q; i++)
    {
        int type;
        cin >> type;
        if (type == 1)
        {
            int l, r;
            cin >> l >> r;
            ask.pb({{type, l}, {r, -1}});
        }
        if (type == 2)
        {
            int id;
            cin >> id;
            id--;
            ask.pb({{type, id}, {-1, -1}});
        }
        if (type == 3)
        {
            int l, r, k;
            cin >> l >> r >> k;
            ask.pb({{type, l}, {r, k}});
            ve.pb(k);
        }
    }
    ve.pb(1);
    sort(ve.begin(), ve.end());
    ve.erase(unique(ve.begin(), ve.end()), ve.end());
    f1.build();
    f2.build();
    f2.state = 1;
    vector < pair < int, int > > a;
    int ans = 0;
    for(auto to: ask)
    {
        int type = to.F.F;
        if (type == 1)
        {
            int l = to.F.S, r = to.S.F;
            l = (l ^ (T * ans));
            r = (r ^ (T * ans));
            if (l > r) swap(l, r);
            f1.upd(r - l + 1, {l, sz(a)}, 1);
            f2.upd(r - l + 1, {r, sz(a)}, 1);
//            st1.insert({l, sz(a)});
//            st2.insert({r, sz(a)});
            a.pb({l, r});
        }
        if (type == 2)
        {
            int id = to.F.S;
            int l = a[id].F, r = a[id].S;
            f1.upd(r - l + 1, {l, id}, 0);
            f2.upd(r - l + 1, {r, id}, 0);
//            st1.erase({l, id});
//            st2.erase({r, id});
        }
        if (type == 3)
        {
            int l = to.F.S, r = to.S.F, k = to.S.S;
            l = (l ^ (T * ans));
            r = (r ^ (T * ans));
            if (l > r) swap(l, r);
            ans = 0;
            if (r - l + 1 < k)
            {
                cout << 0 << endl;
                continue;
            }
            ans += f1.get(l + 1, r - k + 1, k);
            ans += f1.get1(0, l, 2e9 + 1);
            ans -= f2.get1(0, l + k - 2, 2e9 + 1);
            if (l + k - 2 >= l) ans += f2.get2(l + k - 2, k - 1);
            ans -= f1.get1(0, l, k - 1);
            cout << ans << endl;
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 8 ms 776 KB Output is correct
4 Correct 6 ms 716 KB Output is correct
5 Correct 16 ms 2944 KB Output is correct
6 Correct 16 ms 3000 KB Output is correct
7 Correct 11 ms 1132 KB Output is correct
8 Correct 13 ms 2108 KB Output is correct
9 Correct 10 ms 1612 KB Output is correct
10 Correct 9 ms 1796 KB Output is correct
11 Correct 17 ms 2584 KB Output is correct
12 Correct 18 ms 2600 KB Output is correct
13 Correct 13 ms 2400 KB Output is correct
14 Correct 14 ms 1976 KB Output is correct
15 Correct 6 ms 716 KB Output is correct
16 Correct 10 ms 760 KB Output is correct
17 Correct 11 ms 1484 KB Output is correct
18 Correct 16 ms 2516 KB Output is correct
19 Correct 9 ms 1332 KB Output is correct
20 Correct 11 ms 1304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1559 ms 62476 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 53 ms 2964 KB Output is correct
2 Correct 57 ms 2924 KB Output is correct
3 Correct 57 ms 2936 KB Output is correct
4 Correct 53 ms 2988 KB Output is correct
5 Correct 168 ms 11688 KB Output is correct
6 Correct 148 ms 10268 KB Output is correct
7 Correct 140 ms 11360 KB Output is correct
8 Correct 156 ms 14392 KB Output is correct
9 Correct 145 ms 13828 KB Output is correct
10 Correct 155 ms 11252 KB Output is correct
11 Correct 85 ms 3288 KB Output is correct
12 Correct 138 ms 11260 KB Output is correct
13 Correct 121 ms 10312 KB Output is correct
14 Correct 118 ms 6260 KB Output is correct
15 Correct 126 ms 5732 KB Output is correct
16 Correct 86 ms 4792 KB Output is correct
17 Correct 170 ms 8968 KB Output is correct
18 Correct 126 ms 8928 KB Output is correct
19 Correct 137 ms 9068 KB Output is correct
20 Correct 136 ms 9024 KB Output is correct
21 Correct 76 ms 3572 KB Output is correct
22 Correct 115 ms 6968 KB Output is correct
23 Correct 140 ms 9136 KB Output is correct
24 Correct 149 ms 7300 KB Output is correct
25 Correct 56 ms 2932 KB Output is correct
26 Correct 63 ms 2916 KB Output is correct
27 Correct 61 ms 2936 KB Output is correct
28 Correct 52 ms 3008 KB Output is correct
29 Correct 136 ms 9684 KB Output is correct
30 Correct 148 ms 9804 KB Output is correct
31 Correct 159 ms 14056 KB Output is correct
32 Correct 159 ms 11240 KB Output is correct
33 Correct 128 ms 10528 KB Output is correct
34 Correct 107 ms 5604 KB Output is correct
35 Correct 177 ms 8444 KB Output is correct
36 Correct 135 ms 10996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 176 ms 9032 KB Output is correct
2 Correct 171 ms 9144 KB Output is correct
3 Correct 228 ms 9276 KB Output is correct
4 Correct 183 ms 9244 KB Output is correct
5 Runtime error 613 ms 65540 KB Execution killed with signal 9
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 8 ms 776 KB Output is correct
4 Correct 6 ms 716 KB Output is correct
5 Correct 16 ms 2944 KB Output is correct
6 Correct 16 ms 3000 KB Output is correct
7 Correct 11 ms 1132 KB Output is correct
8 Correct 13 ms 2108 KB Output is correct
9 Correct 10 ms 1612 KB Output is correct
10 Correct 9 ms 1796 KB Output is correct
11 Correct 17 ms 2584 KB Output is correct
12 Correct 18 ms 2600 KB Output is correct
13 Correct 13 ms 2400 KB Output is correct
14 Correct 14 ms 1976 KB Output is correct
15 Correct 6 ms 716 KB Output is correct
16 Correct 10 ms 760 KB Output is correct
17 Correct 11 ms 1484 KB Output is correct
18 Correct 16 ms 2516 KB Output is correct
19 Correct 9 ms 1332 KB Output is correct
20 Correct 11 ms 1304 KB Output is correct
21 Runtime error 1559 ms 62476 KB Memory limit exceeded
22 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 8 ms 776 KB Output is correct
4 Correct 6 ms 716 KB Output is correct
5 Correct 16 ms 2944 KB Output is correct
6 Correct 16 ms 3000 KB Output is correct
7 Correct 11 ms 1132 KB Output is correct
8 Correct 13 ms 2108 KB Output is correct
9 Correct 10 ms 1612 KB Output is correct
10 Correct 9 ms 1796 KB Output is correct
11 Correct 17 ms 2584 KB Output is correct
12 Correct 18 ms 2600 KB Output is correct
13 Correct 13 ms 2400 KB Output is correct
14 Correct 14 ms 1976 KB Output is correct
15 Correct 6 ms 716 KB Output is correct
16 Correct 10 ms 760 KB Output is correct
17 Correct 11 ms 1484 KB Output is correct
18 Correct 16 ms 2516 KB Output is correct
19 Correct 9 ms 1332 KB Output is correct
20 Correct 11 ms 1304 KB Output is correct
21 Runtime error 1559 ms 62476 KB Memory limit exceeded
22 Halted 0 ms 0 KB -