Submission #744052

#TimeUsernameProblemLanguageResultExecution timeMemory
744052zaneyuWeirdtree (RMI21_weirdtree)C++17
100 / 100
568 ms48536 KiB
#include "weirdtree.h" #include<bits/stdc++.h> using namespace std; using ll=long long; using ld=long double; using pii=pair<ll,ll>; #define f first #define s second #define pb push_back #define REP(i,n) for(int i=0;i<n;i++) #define REP1(i,n) for(int i=1;i<=n;i++) #define FILL(n,x) memset(n,x,sizeof(n)) #define ALL(_a) _a.begin(),_a.end() #define sz(x) (int)x.size() #define SORT_UNIQUE(c) (sort(c.begin(),c.end()),c.resize(distance(c.begin(),unique(c.begin(),c.end())))) const ll maxn=3e5+5; const ll maxlg=__lg(maxn)+2; const ll INF64=4e18; const int INF=0x3f3f3f3f; const int MOD=1e9+7; const ld PI=acos(-1); const ld eps=1e-4; #define lowb(x) x&(-x) #define MNTO(x,y) x=min(x,(__typeof__(x))y) #define MXTO(x,y) x=max(x,(__typeof__(x))y) struct node{ int m1,m2; ll sum; int cnt=0; }seg[4*maxn]; ll lazy[4*maxn]; int arr[maxn]; int n; int MX; node merge(node a,node b){ node c; c.m1=max(a.m1,b.m1); c.sum=a.sum+b.sum; if(a.m1==b.m1){ c.cnt=a.cnt+b.cnt; c.m2=max(a.m2,b.m2); } else if(c.m1==a.m1){ c.cnt=a.cnt; c.m2=max(a.m2,b.m1); } else{ c.cnt=b.cnt; c.m2=max(a.m1,b.m2); } return c; } void pushdown(int idx,int l,int r){ if(!lazy[idx]) return; seg[idx].sum+=seg[idx].cnt*lazy[idx]; seg[idx].m1+=lazy[idx]; if(l==r){ lazy[idx]=0; return; } for(int c:{idx*2,idx*2+1}){ if(lazy[c]+seg[c].m1==seg[idx].m1-lazy[idx]) lazy[c]+=lazy[idx]; } lazy[idx]=0; } void build(int idx,int l,int r){ lazy[idx]=0; if(l==r){ seg[idx].sum=seg[idx].m1=arr[l],seg[idx].m2=-1; seg[idx].cnt=1; return; } int mid=(l+r)/2; build(idx*2,l,mid),build(idx*2+1,mid+1,r); seg[idx]=merge(seg[idx*2],seg[idx*2+1]); } void upd(int idx,int l,int r,int p,int x){ pushdown(idx,l,r); if(r<p or l>p) return; if(l==r){ seg[idx].sum=seg[idx].m1=x; return; } int mid=(l+r)/2; upd(idx*2,l,mid,p,x),upd(idx*2+1,mid+1,r,p,x); seg[idx]=merge(seg[idx*2],seg[idx*2+1]); } void upd2(int idx,int l,int r,int ql,int qr,int x){ pushdown(idx,l,r); if(r<ql or l>qr or seg[idx].m1<MX) return; if(ql<=l and r<=qr and seg[idx].m1-x>seg[idx].m2){ lazy[idx]-=x; pushdown(idx,l,r); return; } int mid=(l+r)/2; upd2(idx*2,l,mid,ql,qr,x),upd2(idx*2+1,mid+1,r,ql,qr,x); seg[idx]=merge(seg[idx*2],seg[idx*2+1]); } node query(int idx,int l,int r,int ql,int qr){ pushdown(idx,l,r); if(ql<=l and r<=qr) return seg[idx]; int mid=(l+r)/2; if(qr<=mid) return query(idx*2,l,mid,ql,qr); if(ql>mid) return query(idx*2+1,mid+1,r,ql,qr); return merge(query(idx*2,l,mid,ql,qr),query(idx*2+1,mid+1,r,ql,qr)); } int need; int find(int idx,int l,int r,int ql,int qr){ pushdown(idx,l,r); if(r<ql or l>qr or seg[idx].m1<MX) return -1; //cout<<idx<<l<<' '<<r<<'\n'; if(ql<=l and r<=qr){ if(seg[idx].cnt<need){ need-=seg[idx].cnt; return -1; } } if(l==r) return l; int mid=(l+r)/2; int z=find(idx*2,l,mid,ql,qr); if(z!=-1) return z; return find(idx*2+1,mid+1,r,ql,qr); } void initialise(int N, int Q, int h[]) { n=N; REP(i,N) arr[i]=h[i+1]; build(1,0,n-1); } void cut(int l, int r, int k) { --l,--r; while(k){ node cur=query(1,0,n-1,l,r); if(cur.m1==0) break; MX=cur.m1; ll df=cur.m1-cur.m2; MXTO(cur.m2,0); //cout<<cur.m1<<' '<<cur.m2<<' '<<cur.cnt<<'\n'; if(df*cur.cnt<=k){ k-=df*cur.cnt; upd2(1,0,n-1,l,r,df); continue; } upd2(1,0,n-1,l,r,k/cur.cnt); MX-=(k/cur.cnt); need=(k%(cur.cnt)); if(need){ int p=find(1,0,n-1,l,r); //cout<<p<<' '; upd2(1,0,n-1,l,p,1); } break; } } void magic(int i, int x) { --i; upd(1,0,n-1,i,x); } long long int inspect(int l, int r) { --l,--r; return query(1,0,n-1,l,r).sum; }
#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...