Submission #441745

#TimeUsernameProblemLanguageResultExecution timeMemory
441745fhvirusSegments (IZhO18_segments)C++17
100 / 100
3865 ms14032 KiB
// Knapsack DP is harder than FFT. #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; #define ff first #define ss second #define pb emplace_back #define AI(x) begin(x),end(x) template<class I>bool chmax(I&a,I b){return a<b?(a=b,true):false;} template<class I>bool chmin(I&a,I b){return b<a?(a=b,true):false;} #ifdef OWO #define debug(args...) SDF(#args, args) #define OIU(args...) ostream& operator<<(ostream&O,args) #define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;} LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss) template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));} template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';} #else #pragma GCC optimize("Ofast") #define debug(...) ((void)0) #endif const int inf = INT_MAX; const int kN = 200002; const int kK = 512; const int kB = kN / kK + 1; struct PRE: vector<pii> { void build(){ if(empty()) return; sort(AI()); auto i = begin(), j = next(i); for(; j != end(); ++j){ if(i->ff != j->ff){ ++i; *i = *j; i->ss += prev(i)->ss; } else i->ss += j->ss; } erase(next(i), end()); } int que(int p){ auto i = lower_bound(AI(), pii(p, inf)); return i == begin() ? 0 : prev(i)->ss; } }; struct BLK { int mnl, mxl; PRE sl, sr; vector<tuple<int, int, int>> ms; void clear(){ mnl = inf, mxl = 0; sl.clear(); sr.clear(); ms.clear(); } void add(int l, int r, int v){ sl.pb(l, v); sr.pb(r, v); ms.pb(l, r, v); } void build(){ if(ms.empty()) return; { auto [l, r, v] = ms.back(); mnl = (r - l + 1); } { auto [l, r, v] = ms.front(); mxl = (r - l + 1); } sl.build(); sr.build(); } int que(int ql, int qr, int k){ return sl.que(qr - k + 1) - sr.que(ql + k - 2); } int bru(int ql, int qr, int k){ int ans = 0; for(auto [l, r, v]: ms){ if(r - l + 1 < k) continue; if(l <= qr - k + 1) ans += v; if(r <= ql + k - 2) ans -= v; } return ans; } }; int n, t; int tot = 0; pii seg[kN]; int cnt, bnd, bn; vector<tuple<int, int, int>> all, tmp; BLK blk[kB]; bool cmp(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; } void build(){ // rebuild { sort(AI(tmp), cmp); all.insert(end(all), AI(tmp)); auto e = end(all) - tmp.size(); tmp.clear(); inplace_merge(begin(all), e, end(all), cmp); } int n = all.size(); bn = n / kK + 1; for(int i = 0, j = 0, k = 0; i < n; ++i){ if(k == 0) blk[j].clear(); auto [l, r, v] = all[i]; blk[j].add(l, r, v); ++k; if(k >= kK or i == n-1) k = 0, blk[j].build(), ++j; } } void insert(int l, int r){ seg[++tot] = pii(l, r); tmp.pb(l, r, 1); ++cnt; } void remove(int id){ auto [l, r] = seg[id]; tmp.pb(l, r, -1); ++cnt; } int query(int ql, int qr, int k){ if(cnt >= bnd){ cnt = 0; build(); } int ans = 0; for(int i = 0; i < bn; ++i){ if(blk[i].mxl < k) break; if(blk[i].mnl < k){ ans += blk[i].bru(ql, qr, k); break; } ans += blk[i].que(ql, qr, k); } debug(tmp); for(auto [l, r, v]: tmp){ if(r - l + 1 < k) continue; if(l <= qr - k + 1) ans += v; if(r <= ql + k - 2) ans -= v; } return ans; } signed main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin >> n >> t; bnd = ceil(sqrt(n)); int lastans = 0; for(int cmd, a, b, k, id, i = 0; i < n; ++i){ cin >> cmd; if(cmd == 1){ cin >> a >> b; int l = (a ^ (lastans * t)); int r = (b ^ (lastans * t)); if(l > r) swap(l, r); insert(l, r); } else if(cmd == 2){ cin >> id; remove(id); } else if(cmd == 3){ cin >> a >> b >> k; int l = (a ^ (lastans * t)); int r = (b ^ (lastans * t)); if(l > r) swap(l, r); lastans = query(l, r, k); cout << lastans << '\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...