Submission #429081

#TimeUsernameProblemLanguageResultExecution timeMemory
429081MarcoMeijerComparing Plants (IOI20_plants)C++14
44 / 100
2666 ms128468 KiB
#include "plants.h" #include <bits/stdc++.h> using namespace std; // macros typedef long long ll; typedef long double ld; typedef pair<int, int> ii; typedef pair<ll, ll> lll; typedef tuple<int, int, int> iii; typedef vector<int> vi; typedef vector<ii> vii; typedef vector<iii> viii; typedef vector<ll> vll; typedef vector<lll> vlll; #define REP(a,b,c) for(int a=int(b); a<int(c); a++) #define RE(a,c) REP(a,0,c) #define RE1(a,c) REP(a,1,c+1) #define REI(a,b,c) REP(a,b,c+1) #define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--) #define FOR(a,b) for(auto& a : b) #define all(a) a.begin(), a.end() #define INF 1e9 #define EPS 1e-9 #define pb push_back #define popb pop_back #define fi first #define se second #define sz size() mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); // output template<class T1, class T2> void OUT(const pair<T1,T2>& x); template<class T> void OUT(const vector<T>& x); template<class T> void OUT(const T& x) {cout << x;} template<class H, class... T> void OUT(const H& h, const T&... t) {OUT(h); OUT(t...); } template<class T1, class T2> void OUT(const pair<T1,T2>& x) {OUT(x.fi,' ',x.se);} template<class T> void OUT(const vector<T>& x) {RE(i,x.size()) OUT(i==0?"":" ",x[i]);} template<class... T> void OUTL(const T&... t) {OUT(t..., "\n"); } template<class H> void OUTLS(const H& h) {OUTL(h); } template<class H, class... T> void OUTLS(const H& h, const T&... t) {OUT(h,' '); OUTLS(t...); } const int N = (1<<20); int n, k; vi r; struct Seg { ii mn[N*2]; int laz[N*2]; void build(int p=1, int l=0, int r=N-1) { laz[p] = 0; if(l == r) { mn[p] = {0,l}; return; } int m=(l+r)/2; build(p*2 ,l ,m); build(p*2+1,m+1,r); mn[p] = min(mn[p*2],mn[p*2+1]); } void add(int p, int v) { laz[p] += v; mn[p] = {mn[p].fi+v, mn[p].se}; } void pushDown(int p) { add(p*2 ,laz[p]); add(p*2+1,laz[p]); laz[p] = 0; } void add(int i, int j, int v, int p=1, int l=0, int r=N-1) { if(j < l || i > r) return; if(i <= l && j >= r) { add(p,v); return; } pushDown(p); int m=(l+r)/2; add(i,j,v,p*2,l,m); add(i,j,v,p*2+1,m+1,r); mn[p] = min(mn[p*2],mn[p*2+1]); } ii get(int i, int j, int p=1, int l=0, int r=N-1) { if(j < l || i > r) return {INF,INF}; if(i <= l && j >= r) return mn[p]; pushDown(p); int m=(l+r)/2; ii a = get(i,j,p*2,l,m); ii b = get(i,j,p*2+1,m+1,r); return min(a,b); } void addCircle(int i, int l, int v) { int j = i + l - 1; add(i,min(n-1,j),v); if(j > n-1) add(0,j-n,v); } }; struct SegMin { ii seg[N*2]; void set(int i, ii v, int p=1, int l=0, int r=N-1) { if(i < l || i > r) return; if(l == r) { seg[p] = v; return; } int m=(l+r)/2; set(i,v,p*2 ,l ,m); set(i,v,p*2+1,m+1,r); seg[p] = min(seg[p*2], seg[p*2+1]); } ii get(int i, int j, int p=1, int l=0, int r=N-1) { if(j < i) return {INF,INF}; if(j < l || i > r) return {INF,INF}; if(i <= l && j >= r) return seg[p]; int m=(l+r)/2; ii a=get(i,j,p*2 ,l ,m); ii b=get(i,j,p*2+1,m+1,r); return min(a,b); } } segmin; Seg seg1, seg2; vi val; vi ra; vector<vi> dist; vi nextL[20], nextR[20]; void init(int k_, vi r_) { k = k_; r = r_; n = r.size(); seg1.build(); seg2.build(); RE(i,n) seg1.add(i,i,r[i]); RE(i,n) seg2.add(i,i,1); val.assign(n,0); REV(i,1,n+1) { while(true) { ii res = seg1.get(0,n-1); if(res.fi != 0) break; int j = res.se; seg1.add(j,j,INF); seg2.add(j,j,-1); seg2.addCircle((j+1)%n, k-1, 1); } ii res = seg2.get(0,n-1); int j = res.se; val[j] = i; seg2.add(j,j,INF); seg2.addCircle((j+1)%n, k-1, -1); seg1.addCircle((j-k+n+1)%n, k-1, -1); } RE(i,20) nextL[i].assign(n*2+2,0), nextR[i].assign(n*2+2,0); RE(i,20) nextL[i][0 ] = nextR[i][0 ] = 0; RE(i,20) nextL[i][n*2+1] = nextR[i][n*2+1] = n*2+1; RE(i,n*2) segmin.set(i,{INF,INF}); ra.assign(n+2,0); RE(i,n) ra[val[i]] = i; REV(i,1,n+1) { int x = ra[i]; nextL[0][x+1 ] = segmin.get(max(0,x-k),x-1).se; nextR[0][x+1 ] = segmin.get(x+1,min(n*2-1,x+k)).se; nextL[0][x+1+n] = segmin.get(max(0,x+n-k),x+n-1).se; nextR[0][x+1+n] = segmin.get(x+n+1,min(n*2-1,x+n+k)).se; if(nextL[0][x+1] == INF) nextL[0][x+1] = 0; if(nextR[0][x+1] == INF) nextR[0][x+1] = n*2+1; if(nextL[0][x+1+n] == INF) nextL[0][x+1+n] = 0; if(nextR[0][x+1+n] == INF) nextR[0][x+1+n] = n*2+1; segmin.set(x ,{i,x+1 }); segmin.set(x+n,{i,x+1+n}); } REP(j,1,20) RE(i,n*2+2) { nextL[j][i] = nextL[j-1][nextL[j-1][i]]; nextR[j][i] = nextR[j-1][nextR[j-1][i]]; } } int compare_plants(int x, int y) { int mult = 1; if(val[x] < val[y]) swap(x,y), mult = -1; RE(_,2) { vi* ar = _ ? nextL : nextR; int a = y+1; int b = x+1; if( _ && y < x) a += n; if(!_ && y > x) b += n; REV(i,0,20) while(_ ? (ar[i][a] >= b) : (ar[i][a] <= b)) a = ar[i][a]; if(val[(a-1+n)%n] <= val[(b-1+n)%n] && (ar[0][a] != n*2+1 || a == b)) return mult; } 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...
#Verdict Execution timeMemoryGrader output
Fetching results...