제출 #1156818

#제출 시각아이디문제언어결과실행 시간메모리
1156818steveonalexSegments (IZhO18_segments)C++17
40 / 100
3505 ms41092 KiB

/*

 ________  ___  ________  ________          ________   ___  ________  ________  ________     
    |\   ____\|\  \|\   ____\|\   __  \        |\   ___  \|\  \|\   ____\|\   ____\|\   __  \    
    \ \  \___|\ \  \ \  \___|\ \  \|\  \       \ \  \\ \  \ \  \ \  \___|\ \  \___|\ \  \|\  \   
     \ \  \  __\ \  \ \  \  __\ \   __  \       \ \  \\ \  \ \  \ \  \  __\ \  \  __\ \   __  \  
      \ \  \|\  \ \  \ \  \|\  \ \  \ \  \       \ \  \\ \  \ \  \ \  \|\  \ \  \|\  \ \  \ \  \ 
       \ \_______\ \__\ \_______\ \__\ \__\       \ \__\\ \__\ \__\ \_______\ \_______\ \__\ \__\
        \|_______|\|__|\|_______|\|__|\|__|        \|__| \|__|\|__|\|_______|\|_______|\|__|\|__|
                                                                                                 
                                                                                             
                                                                                             
     ________  ________  ________  ________  ___  ___  ________ _________  ___  ________  ________      
|\   __  \|\   __  \|\   __  \|\   ___ \|\  \|\  \|\   ____\\___   ___\\  \|\   __  \|\   ___  \    
\ \  \|\  \ \  \|\  \ \  \|\  \ \  \_|\ \ \  \\\  \ \  \___\|___ \  \_\ \  \ \  \|\  \ \  \\ \  \   
 \ \   ____\ \   _  _\ \  \\\  \ \  \ \\ \ \  \\\  \ \  \       \ \  \ \ \  \ \  \\\  \ \  \\ \  \  
  \ \  \___|\ \  \\  \\ \  \\\  \ \  \_\\ \ \  \\\  \ \  \____   \ \  \ \ \  \ \  \\\  \ \  \\ \  \ 
   \ \__\    \ \__\\ _\\ \_______\ \_______\ \_______\ \_______\  \ \__\ \ \__\ \_______\ \__\\ \__\
    \|__|     \|__|\|__|\|_______|\|_______|\|_______|\|_______|   \|__|  \|__|\|_______|\|__| \|__|
                                                                                                    
                                                                                                    
    Written by: giga nigga                                                                               

*/

// #include "largest.h"
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
 
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ull mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
 
// mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
    double wow = (double) ((ull) rng()) / ((ull)(0-1));
    return wow * (r - l) + l;
}
 
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }
 
template <class T>
    void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: container) out << item << separator;
        out << finish;
    }
 
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

const int BLOCK = 4000;
const int INF = 2e9 + 69;


struct FenwickTree{
    int n;
    vector<int> X;
    vector<vector<int>> val, a;
    vector<pair<int,int>> points;

    FenwickTree(){
    }

    void clear(){
        val.clear(); a.clear();
        points.clear();
    }

    void add_points(pair<int, int> p){
        points.push_back(p);
    }

    void fake_update(int i, int j){
        while(i <= n){
            val[i].push_back(j);
            i += LASTBIT(i);
        }
    }

    void update(int i, int j){
        while(i <= n){
            int _j = lower_bound(ALL(val[i]), j) - val[i].begin();
            a[i][_j]++;
            i += LASTBIT(i);
        }
    }

    void build_tree(){
        for(auto i: points){
            X.push_back(i.first);
        }
        X.push_back(-INF);
        remove_dup(X);
        n = X.size() - 1;
        a.resize(n+1); val.resize(n+1);

        for(auto i: points){
            i.first = lower_bound(ALL(X), i.first) - X.begin();
            fake_update(i.first, i.second);
        }
        for(int i = 1; i<=n; ++i) {
            val[i].push_back(-INF);
            remove_dup(val[i]);
            a[i].resize(val[i].size());
        }

        for(auto i: points){
            i.first = lower_bound(ALL(X), i.first) - X.begin();
            update(i.first, i.second);
        }

        for(int i = 1; i <= n; ++i){
            for(int j = 1; j < (int) a[i].size(); ++j) a[i][j] += a[i][j-1];
        }
    }


    int get(int i, int j){
        i = upper_bound(ALL(X), i) - X.begin() - 1;
        int ans = 0;
        while(i > 0){
            int idx = upper_bound(ALL(val[i]), j) - val[i].begin() - 1;
            ans += a[i][idx];
            i -= LASTBIT(i);
        }
        return ans;
    }
};


void solve(){
    int q, t; cin >> q >> t;
    vector<pair<int, int>> range_list;
    vector<bool> activated;

    vector<array<int, 3>> ranges;

    int last_ans = 0;
    FenwickTree st1, st2;
    int tot = 0;
    vector<int> len_list;

    for(int it = 1; it <= q; ++it){
        if (it % BLOCK == 0){
            tot = 0;
            len_list.clear();
            st1.clear(); st2.clear();
            ranges.clear();
            for(int i = 0; i < (int) range_list.size(); ++i) if (activated[i]){
                tot++;
                int l, r; tie(l, r) = range_list[i];
                int len = r - l + 1;
                st1.add_points(make_pair(-len, r));
                st2.add_points(make_pair(-len, -l));

                len_list.push_back(len);
            }
            sort(ALL(len_list));
            st1.build_tree(); st2.build_tree();
        }
        int type; cin >> type;
        if (type == 1){
            int a, b; cin >> a >> b;
            a ^= t * last_ans;
            b ^= t * last_ans;
            if (a > b) swap(a, b);
            range_list.push_back({a, b});
            activated.push_back(1);
            ranges.push_back({{a, b, 1}});
        }
        else if (type == 2){
            int id; cin >> id;
            id--;
            activated[id] = 0;

            int a, b; tie(a, b) = range_list[id];
            ranges.push_back({{a, b, -1}});
        }
        else{
            int l, r, k; cin >> l >> r >> k;
            l ^= t * last_ans;
            r ^= t * last_ans;

            if (l > r) swap(l, r);

            if (r - l + 1 < k){
                cout << 0 << "\n";
                last_ans = 0;
                continue;
            }

            int ans = tot;
            for(auto i: ranges){
                int L = max(l, i[0]);
                int R = min(r, i[1]);
                if (R - L + 1 >= k) ans += i[2];
            }
            ans -= lower_bound(ALL(len_list), k) - len_list.begin();
            ans -= st1.get(-k, l + k - 2);
            ans -= st2.get(-k, -(r - k + 2));

            cout << ans << "\n";
            last_ans = ans;
        }
    }
}

int main(void){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    clock_t start = clock();

    solve();

    cerr << "Time elapsed: " << clock() - start << "ms!\n";
    return 0;
}
#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...