# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
553314 | 2022-04-25T12:19:34 Z | Amylopectin | Monkey and Apple-trees (IZhO12_apple) | C++14 | 1 ms | 340 KB |
#include <iostream> #include <stdio.h> using namespace std; const int mxn = 8e5 + 10;int ro,pa[mxn] = {},lrp[mxn] = {},lch[mxn] = {},rch[mxn] = {}, lbo[mxn] = {},rbo[mxn] = {},nlbo[mxn] = {},nrbo[mxn] = {},rip[mxn] = {},memcou = 0,ru = 0; int fimi(int l,int r) { if(l < r) return l; return r; } int fima(int l,int r) { if(l > r) return l; return r; } int upd(int fn) { rip[fn] = nrbo[fn] - nlbo[fn] + 1; if(lch[fn] != -1) { rip[fn] += rip[lch[fn]]; lbo[fn] = lbo[lch[fn]]; } else { lbo[fn] = nlbo[fn]; } if(rch[fn] != -1) { rip[fn] += rip[rch[fn]]; rbo[fn] = rbo[rch[fn]]; } else { rbo[fn] = nrbo[fn]; } return 0; } int sply(int cn) { int fn,tn,f; if(ro == cn) return 0; fn = pa[cn]; if(fn == ro) { pa[cn] = pa[fn]; if(fn != ro) { if(lrp[fn] == -1) { rch[pa[fn]] = cn; } else { lch[pa[fn]] = cn; } } if(lrp[cn] == -1) { lrp[cn] = lrp[fn]; rch[fn] = lch[cn]; if(lch[cn] != -1) { pa[lch[cn]] = fn; lrp[lch[cn]] = -1; } lch[cn] = fn; pa[fn] = cn; lrp[fn] = 1; } else { lrp[cn] = lrp[fn]; lch[fn] = rch[cn]; if(rch[cn] != -1) { pa[rch[cn]] = fn; lrp[rch[cn]] = 1; } rch[cn] = fn; pa[fn] = cn; lrp[fn] = -1; } } else { tn = pa[fn]; pa[cn] = pa[tn]; if(tn != ro) { if(lrp[tn] == -1) { rch[pa[tn]] = cn; } else { lch[pa[tn]] = cn; } } if(lrp[cn] == -1) { if(lrp[fn] == -1) { lrp[cn] = lrp[tn]; rch[tn] = lch[fn]; if(lch[fn] != -1) { pa[lch[fn]] = tn; lrp[lch[fn]] = -1; } rch[fn] = lch[cn]; if(lch[cn] != -1) { pa[lch[cn]] = fn; lrp[lch[cn]] = -1; } lch[fn] = tn; pa[tn] = fn; lrp[tn] = 1; lch[cn] = fn; pa[fn] = cn; lrp[fn] = 1; } else { lrp[cn] = lrp[tn]; rch[fn] = lch[cn]; if(lch[cn] != -1) { pa[lch[cn]] = fn; lrp[lch[cn]] = -1; } lch[tn] = rch[cn]; if(rch[cn] != -1) { pa[rch[cn]] = tn; lrp[rch[cn]] = 1; } lch[cn] = fn; pa[fn] = cn; lrp[fn] = 1; rch[cn] = tn; pa[tn] = cn; lrp[tn] = -1; } } else { if(lrp[tn] == -1) { lrp[cn] = lrp[tn]; rch[tn] = lch[cn]; if(lch[cn] != -1) { pa[lch[cn]] = tn; lrp[lch[cn]] = -1; } lch[fn] = rch[cn]; if(rch[cn] != -1) { pa[rch[cn]] = fn; lrp[rch[cn]] = 1; } lch[cn] = tn; pa[tn] = cn; lrp[tn] = 1; rch[cn] = fn; pa[fn] = cn; lrp[fn] = -1; } else { lrp[cn] = lrp[tn]; lch[tn] = rch[fn]; if(rch[fn] != -1) { pa[rch[fn]] = tn; lrp[rch[fn]] = 1; } lch[fn] = rch[cn]; if(rch[cn] != -1) { pa[rch[cn]] = fn; lrp[rch[cn]] = 1; } rch[fn] = tn; pa[tn] = fn; lrp[tn] = -1; rch[cn] = fn; pa[fn] = cn; lrp[fn] = -1; } } upd(tn); } upd(fn); upd(cn); if(fn != ro && tn != ro) { sply(cn); } ro = cn; return 0; } int del(int cn) { if(lch[cn] == -1) { if(rch[cn] == -1) { if(pa[cn] != -1) { if(lrp[cn] == -1) { rch[pa[cn]] = -1; } else { lch[pa[cn]] = -1; } } return pa[cn]; } else { if(pa[cn] != -1) { if(lrp[cn] == -1) { rch[pa[cn]] = rch[cn]; } else { lch[pa[cn]] = rch[cn]; } } else { ro = rch[cn]; } lrp[rch[cn]] = lrp[cn]; pa[rch[cn]] = pa[cn]; return rch[cn]; } } else { if(rch[cn] == -1) { if(pa[cn] != -1) { if(lrp[cn] == -1) { rch[pa[cn]] = lch[cn]; } else { lch[pa[cn]] = lch[cn]; } } else { ro = lch[cn]; } lrp[lch[cn]] = lrp[cn]; pa[lch[cn]] = pa[cn]; return lch[cn]; } else { int fn = lch[cn]; while(rch[fn] != -1) { fn = rch[fn]; } del(fn); pa[fn] = pa[cn]; lrp[fn] = lrp[cn]; if(lch[cn] != fn) { lch[fn] = lch[cn]; } else { lch[fn] = -1; } rch[fn] = rch[cn]; if(pa[cn] != -1) { if(lrp[cn] == -1) { rch[pa[cn]] = fn; } else { lch[pa[cn]] = fn; } } return fn; } } while(1) printf("efef"); return -1; } int ripe(int cn,int tl,int tr) { int fn; if(nlbo[cn] > tr) { if(lch[cn] != -1) { ripe(lch[cn],tl,tr); } else { lch[cn] = ru; lch[ru] = -1; rch[ru] = -1; pa[ru] = cn; lrp[ru] = 1; nlbo[ru] = tl; nrbo[ru] = tr; lbo[ru] = tl; rbo[ru] = tr; rip[ru] = tr-tl+1; lbo[cn] = tl; rip[cn] += rip[ru]; ru ++; sply(ru-1); } } else if(nrbo[cn] < tl) { if(rch[cn] != -1) { ripe(rch[cn],tl,tr); } else { rch[cn] = ru; lch[ru] = -1; rch[ru] = -1; pa[ru] = cn; lrp[ru] = -1; nlbo[ru] = tl; nrbo[ru] = tr; lbo[ru] = tl; rbo[ru] = tr; rip[ru] = tr-tl+1; rbo[cn] = tr; rip[cn] += rip[ru]; ru ++; sply(ru-1); } } else { tl = fimi(tl,nlbo[cn]); tr = fima(tr,nrbo[cn]); fn = del(cn); if(fn == -1) { ro = ru; lch[ru] = -1; rch[ru] = -1; pa[ru] = -1; lrp[ru] = 0; nlbo[ru] = tl; nrbo[ru] = tr; lbo[ru] = tl; rbo[ru] = tr; rip[ru] = tr-tl+1; ru ++; sply(ru-1); } else { ripe(fn,tl,tr); } } return 0; } int getva(int cn,int tl,int tr) { if(lbo[cn] > tr || rbo[cn] < tl) { return 0; } if(lbo[cn] >= tl && rbo[cn] <= tr) { return rip[cn]; } int cva = 0; if(nlbo[cn] >= tl && nrbo[cn] <= tr) { cva += nrbo[cn] - nlbo[cn] + 1; } else if(nrbo[cn] >= tl && nlbo[cn] < tl) { cva += nrbo[cn] - tl + 1; } else if(nlbo[cn] <= tr && nrbo[cn] > tr) { cva += tr - nlbo[cn] + 1; } if(lch[cn] != -1) { cva += getva(lch[cn],tl,tr); } if(rch[cn] != -1) { cva += getva(rch[cn],tl,tr); } return cva; } int main() { int i,j,n,m,cl,cr,sta,cva,c; scanf("%d",&n); ro = -1; for(i=0; i<n; i++) { scanf("%d %d %d",&sta,&cl,&cr); cl += c; cr += c; if(sta == 1) { if(ro == -1) { cva = 0; } else { cva = getva(ro,cl,cr); } printf("%d\n",cva); c += cva; } else { if(ro == -1) { ro = ru; lch[ru] = -1; rch[ru] = -1; pa[ru] = -1; lrp[ru] = 0; nlbo[ru] = cl; nrbo[ru] = cr; lbo[ru] = cl; rbo[ru] = cr; rip[ru] = cr-cl+1; ru ++; } else { ripe(ro,cl,cr); } } } return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 340 KB | Output is correct |
2 | Incorrect | 1 ms | 340 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |