Submission #379357

#TimeUsernameProblemLanguageResultExecution timeMemory
3793572qbingxuanSegments (IZhO18_segments)C++14
100 / 100
4270 ms15968 KiB
#pragma GCC optimize("Ofast")
#pragma loop_opt(on)
#include <bits/extc++.h>
#ifdef local
#define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define debug(a...) qqbx(#a, a)
template <typename ...T> void qqbx(const char *s, T ...a) {
    int cnt = sizeof...(T);
    ((std::cerr << "\033[1;32m(" << s << ") = (") , ... , (std::cerr << a << (--cnt ? ", " : ")\033[0m\n")));
}
#else
#define safe ((void)0)
#define debug(...) ((void)0)
#endif // local
#define all(v) begin(v),end(v)

using namespace std;
using namespace __gnu_pbds;
template <typename T> using rbt = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef int64_t ll;

const int maxc = 2e9, inf = 1e9, K1 = 1024, K2 = 1024, maxn = 200025;

pair<int,int> seg[maxn];
int segId = 0;

vector<tuple<int,int,int>> recent, total;
vector<tuple<int,int,int>> block[maxn];
int maxLen[maxn], minLen[maxn], blockCnt;

struct StaticPrefixSum {
    pair<int,int> st[K2];
    int sz;
    int query(int x) {
        auto it = lower_bound(st, st+sz, make_pair(x, -inf));
        if (it == st) return 0;
        return prev(it) -> second;
    }
    void add(int p, int v) {
        st[sz++] = { p, v };
    }
    void clear() {
        sz = 0;
    }
    void build() {
        sort(st, st+sz);
        int it = 0;
        for (int i = 1; i < sz; i++) {
            if (st[i].first != st[it].first) {
                st[++it] = st[i];
            } else {
                st[it].second += st[i].second;
            }
        }
        sz = it + 1;
        for (int i = 1; i < sz; i++) st[i].second += st[i-1].second;
    }
} lpref[maxn / K2 + 25], rpref[maxn / K2 + 25];
int totSum[maxn];

void build() {
    blockCnt = total.size() / K2 + 1;
    for (int i = 0; i < blockCnt; i++) {
        block[i].clear();
        lpref[i].clear();
        rpref[i].clear();
        totSum[i] = 0;
        maxLen[i] = 0, minLen[i] = inf;
    }
    sort(total.begin(), total.end(), [](tuple<int,int,int> a, tuple<int,int,int> b) {
        auto [al, ar, av] = a;
        auto [bl, br, bv] = b;
        return ar - al > br - bl;
    });
    for (int i = 0; i < total.size(); i++) {
        auto [l, r, v] = total[i];
        int bid = i / K2;
        block[bid].emplace_back(l, r, v);
        lpref[bid].add(-l, v);
        rpref[bid].add(r, v);
        totSum[bid] += v;
        maxLen[bid] = max(maxLen[bid], r - l);
        minLen[bid] = min(minLen[bid], r - l);
    }
    for (int i = 0; i < blockCnt; i++) lpref[i].build(), rpref[i].build();
}

void add(int l, int r, int d) {
    recent.emplace_back(l, r, d);
    if (recent.size() == K1) {
        for (auto [l, r, v]: recent)
            total.emplace_back(l, r, v);
        recent.clear();
        build();
    }
    // N / K1 * NlogN + N * N / K2 * logN + N * K2 + N * K1
}

int query(int l, int r, int k) {
    int res = 0;
    for (int i = 0; i < blockCnt; i++) {
        /*
        debug(i);
        for (auto [nl, nr, v]: block[i])
            debug(nl, nr, v);
        debug(maxLen[i], k);
        */
        if (maxLen[i] < k) break;
        if (minLen[i] >= k) {
            debug(totSum[i], rpref[i].query(l + k), lpref[i].query(-(r - k)));
            res += totSum[i] - rpref[i].query(l + k) - lpref[i].query(-(r - k));
            debug(res);
        } else {
            for (auto [nl, nr, v]: block[i]) {
                if (nr - nl >= k) {
                    res += v;
                    if (nr < l + k) res -= v;
                    if (nl > r - k) res -= v;
                }
            }
            break;
        }
    }
    for (auto [nl, nr, v]: recent) {
        if (nr - nl >= k) {
            res += v;
            if (nr < l + k) res -= v;
            if (nl > r - k) res -= v;
        }
    }
    return res;
}
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    int n, encode;
    cin >> n >> encode;

    int ans = 0;
    for (int i = 0; i < n; i++) {
        int t;
        cin >> t;
        if (t == 1) {
            int a, b;
            cin >> a >> b;
            if (encode) a ^= ans, b ^= ans;
            if (a > b) swap(a, b);
            ++b;
            add(a, b, 1);
            seg[segId++] = { a, b };
        } else if (t == 2) {
            int id;
            cin >> id;
            --id;
            auto [a, b] = seg[id];
            add(a, b, -1);
        } else if (t == 3) {
            int a, b, k;
            cin >> a >> b >> k;
            if (encode) a ^= ans, b ^= ans;
            if (a > b) swap(a, b);
            ++b;
            ans = query(a, b, k);
            cout << ans << '\n';
        }
    }
}
/*
6 1
1 1 2
3 2 4 2
1 3 5 
3 2 3 1
2 1 
3 0 3 1

6 0
1 3 10
1 3 5
3 6 10 6
2 1
1 3 10
3 6 4 2
 */

Compilation message (stderr)

segments.cpp:2: warning: ignoring #pragma loop_opt  [-Wunknown-pragmas]
    2 | #pragma loop_opt(on)
      | 
segments.cpp: In lambda function:
segments.cpp:71:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   71 |         auto [al, ar, av] = a;
      |              ^
segments.cpp:72:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   72 |         auto [bl, br, bv] = b;
      |              ^
segments.cpp: In function 'void build()':
segments.cpp:75:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |     for (int i = 0; i < total.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
segments.cpp:76:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   76 |         auto [l, r, v] = total[i];
      |              ^
segments.cpp: In function 'void add(int, int, int)':
segments.cpp:91:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   91 |         for (auto [l, r, v]: recent)
      |                   ^
segments.cpp: In function 'int query(int, int, int)':
segments.cpp:114:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  114 |             for (auto [nl, nr, v]: block[i]) {
      |                       ^
segments.cpp:124:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  124 |     for (auto [nl, nr, v]: recent) {
      |               ^
segments.cpp: In function 'int main()':
segments.cpp:154:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  154 |             auto [a, b] = seg[id];
      |                  ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...