Submission #1031780

# Submission time Handle Problem Language Result Execution time Memory
1031780 2024-07-23T07:07:54 Z GrindMachine Segments (IZhO18_segments) C++17
0 / 100
349 ms 15032 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

/*

refs:
other solutions
https://codeforces.com/blog/entry/124766 (log decomposition technique)

*/

const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

template<typename T>
struct fenwick {
    int n;
    vector<vector<int>> tr;
    int LOG = 0;

    fenwick() {

    }

    fenwick(int n_) {
        n = n_;
        tr = vector<vector<int>>(n + 1);
        while((1<<LOG) <= n) LOG++;
    }

    int lsb(int x) {
        return x & -x;
    }

    void pupd(int i, int v) {
        i++;
        for(; i <= n; i += lsb(i)){
            tr[i].pb(v);
        }
    }

    void build(){
        rep1(i,n){
            sort(all(tr[i]));
        }
    }

    void clear(){
        rep1(i,n){
            tr[i].clear();
            tr[i].shrink_to_fit();
        }
        tr.clear();
        tr.shrink_to_fit();
    }

    int get(int i, int x, int y) {
        i++;
        int res = 0;
        for(; i; i ^= lsb(i)){
            auto &a = tr[i];
            if(y == 2*inf1) res += sz(a);
            else res += upper_bound(all(a),y)-a.begin();
            if(x) res -= lower_bound(all(a),x)-a.begin();
        }
        return res;
    }

    int query(int l, int r, int x, int y) {
        if (l > r) return 0;
        int res = get(r,x,y) - get(l-1,x,y);
        return res;
    }
};

struct S{
    vector<pii> a;
    fenwick<int> fenw1,fenw2;
    bool empty(){
        return a.empty();
    }
    void clear(){
        a.clear();
        a.shrink_to_fit();
        fenw1.clear(), fenw2.clear();
    }
    void build(){        
        fenw1 = fenwick<int>(sz(a));
        fenw2 = fenwick<int>(sz(a));
        rep(i,sz(a)){
            fenw1.pupd(i,a[i].ff);
            fenw2.pupd(i,a[i].ss);
        }
        fenw1.build();
        fenw2.build();
    }
    int query(int i, int l1, int r1, int k){
        int n = sz(a);
        return fenw1.query(i,n-1,r1-k+1,2*inf1)+fenw2.query(i,n-1,0,l1+k-1);
    }
};

struct DS{
    S a[20];
    void insert(int l, int r){
        S curr;
        curr.a.pb({l,r});

        rep(i,20){
            if(!a[i].empty()){
                S nxt;
                int ptr1 = 0, ptr2 = 0;
                while(ptr1 < sz(curr.a) and ptr2 < sz(a[i].a)){
                    int len1 = curr.a[ptr1].ss-curr.a[ptr1].ff;
                    int len2 = a[i].a[ptr2].ss-a[i].a[ptr2].ff;
                    if(len1 < len2){
                        nxt.a.pb(curr.a[ptr1++]);
                    }
                    else{
                        nxt.a.pb(a[i].a[ptr2++]);
                    }
                }                
                while(ptr1 < sz(curr.a)) nxt.a.pb(curr.a[ptr1++]);
                while(ptr2 < sz(a[i].a)) nxt.a.pb(a[i].a[ptr2++]);

                curr = nxt;
                nxt.clear();
                a[i].clear();                
            }
            else{
                a[i] = curr;
                a[i].build();
                break;
            }
        }
    }

    int query(int l1, int r1, int k){
        if(r1-l1 < k) return 0;
        int ans = 0;
        rep(i,20){
            if(a[i].empty()) conts;
            auto &b = a[i].a;
            int lo = 0, hi = sz(b)-1;
            int pos = -1;

            while(lo <= hi){
                int mid = (lo+hi) >> 1;
                if(b[mid].ss-b[mid].ff >= k){
                    pos = mid;
                    hi = mid-1;
                }
                else{
                    lo = mid+1;
                }
            }

            if(pos == -1) conts;

            int bad = a[i].query(pos,l1,r1,k);
            int add = sz(b)-pos-bad;
            ans += add;
        }

        return ans;
    }
};

void solve(int test_case)
{
    int q,c; cin >> q >> c;
    
    DS ds1,ds2;
    vector<pii> a;
    a.pb({0,0});

    int last_ans = 0;

    while(q--){
        int t; cin >> t;
        if(t == 1){
            int l,r; cin >> l >> r;
            l ^= c*last_ans, r ^= c*last_ans;
            if(l > r) swap(l,r);
            ds1.insert(l,r);
            a.pb({l,r});
        }
        else if(t == 2){
            int id; cin >> id;
            auto [l,r] = a[id];
            ds2.insert(l,r);
        }
        else{
            int l,r,k; cin >> l >> r >> k;
            l ^= c*last_ans, r ^= c*last_ans;
            if(l > r) swap(l,r);
            k--;
            int ans = ds1.query(l,r,k)-ds2.query(l,r,k);
            cout << ans << endl;
            last_ans = ans;
        }
    }
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 344 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 349 ms 11020 KB Output is correct
2 Runtime error 1 ms 604 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 309 ms 15032 KB Output is correct
2 Correct 307 ms 14536 KB Output is correct
3 Runtime error 1 ms 604 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 604 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 344 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 344 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -