Submission #1031756

#TimeUsernameProblemLanguageResultExecution timeMemory
1031756GrindMachineSegments (IZhO18_segments)C++17
7 / 100
490 ms47792 KiB
#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 segtree { // https://codeforces.com/blog/entry/18051 /*=======================================================*/ struct data { vector<int> a; }; data neutral = {}; data merge(data &left, data &right) { data curr; auto &a = left.a, &b = right.a, &c = curr.a; int ptr1 = 0, ptr2 = 0; while(ptr1 < sz(a) and ptr2 < sz(b)){ if(a[ptr1] < b[ptr2]){ c.pb(a[ptr1++]); } else{ c.pb(b[ptr2++]); } } while(ptr1 < sz(a)) c.pb(a[ptr1++]); while(ptr2 < sz(b)) c.pb(b[ptr2++]); return curr; } void create(int i, T v) { tr[i].a.pb(v); } void modify(int i, T v) { } /*=======================================================*/ int n; vector<data> tr; segtree() { } segtree(int siz) { init(siz); } void init(int siz) { n = siz; tr.assign(2 * n, neutral); } void build(vector<T> &a, int siz) { rep(i, siz) create(i + n, a[i]); rev(i, n - 1, 1) tr[i] = merge(tr[i << 1], tr[i << 1 | 1]); } void pupd(int i, T v) { modify(i + n, v); for (i = (i + n) >> 1; i; i >>= 1) tr[i] = merge(tr[i << 1], tr[i << 1 | 1]); } int query(int l, int r, int x, int y) { int res = 0; for (l += n, r += n; l <= r; l >>= 1, r >>= 1) { if(l&1){ auto &a = tr[l++].a; res += upper_bound(all(a),y)-lower_bound(all(a),x); } if(!(r&1)){ auto &a = tr[r--].a; res += upper_bound(all(a),y)-lower_bound(all(a),x); } } return res; } }; struct S{ vector<pii> a; segtree<int> st1,st2; bool empty(){ return a.empty(); } void clear(){ a.clear(); } void build(){ vector<int> ls,rs; for(auto [l,r] : a){ ls.pb(l), rs.pb(r); } st1 = segtree<int>(sz(ls)); st2 = segtree<int>(sz(rs)); st1.build(ls,sz(ls)); st2.build(rs,sz(rs)); } int query(int i, int l1, int r1, int k){ int n = sz(a); return st1.query(i,n-1,r1-k+1,2*inf1)+st2.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}); auto cmp = [&](pii p1, pii p2){ return p1.ss-p1.ff < p2.ss-p2.ff; }; rep(i,20){ if(!a[i].empty()){ for(auto [lx,rx] : a[i].a){ curr.a.pb({lx,rx}); } sort(all(curr.a),cmp); 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 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...