From 1335cabe0a814e03d816bf1ed5e3186e3f3be64b Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Thu, 14 Nov 2024 10:31:29 +0800 Subject: [PATCH] update --- src/20241007/training_Eric_cai.cpp | 361 +++++++++++---------- src/20241007/training_Lonely_Christmas.cpp | 203 +----------- src/P1395/P1395.cpp | 47 ++- 3 files changed, 227 insertions(+), 384 deletions(-) diff --git a/src/20241007/training_Eric_cai.cpp b/src/20241007/training_Eric_cai.cpp index 0ed6a6a..ef1f1f7 100755 --- a/src/20241007/training_Eric_cai.cpp +++ b/src/20241007/training_Eric_cai.cpp @@ -1,179 +1,182 @@ -#include -#define ll long long -using namespace std; -const int maxn=2e5+5; -struct node -{ - ll mx,sum,t,sv,add,tag,len; -}; -node tr[maxn<<2]; -int n,q; -ll d[maxn<<2]; -inline void mdf(int id,int k,ll w) -{ - if(k==0) - { - tr[id].mx+=w; - tr[id].t+=w*w*tr[id].len+tr[id].sv*w+tr[id].sum*w; - tr[id].sum+=w*tr[id].len; - tr[id].sv+=w*tr[id].len; - tr[id].add+=w; - } - if(k==1) - { - tr[id].mx=w,tr[id].t=w*w*tr[id].len,tr[id].sum=w*tr[id].len,tr[id].sv=w*tr[id].len; - tr[id].add=0,tr[id].tag=w; - } -} -inline void pushdown(int id) -{ - if(tr[id].tag) - { - mdf(id<<1,1,tr[id].tag); - mdf(id<<1|1,1,tr[id].tag); - } - if(tr[id].add) - { - mdf(id<<1,0,tr[id].add); - mdf(id<<1|1,0,tr[id].add); - } - tr[id].tag=tr[id].add=0; -} -node calc(node nd,int id) -{ - node ret=nd; - if(tr[id].len==1) - { - ret.mx=max(ret.mx,tr[id].mx); - ret.sum+=tr[id].sum; - ret.t+=tr[id].sum*ret.mx; - ret.sv+=ret.mx; - ret.len+=tr[id].len; - } - else - { - pushdown(id); - if(tr[id<<1].mx>=nd.mx) - { - ret=calc(nd,id<<1); - ret.mx=max(ret.mx,tr[id<<1|1].mx); - ret.sum+=tr[id<<1|1].sum; - ret.t+=tr[id].t-tr[id<<1].t; - ret.sv+=tr[id].sv-tr[id<<1].sv; - ret.len+=tr[id<<1|1].len; - } - else - { - nd.sum+=tr[id<<1].sum; - nd.t+=tr[id<<1].sum*nd.mx; - nd.sv+=tr[id<<1].len*nd.mx; - nd.len+=tr[id<<1].len; - ret=calc(nd,id<<1|1); - } - } - return ret; -} -inline void pushup(int id) -{ - tr[id]=calc(tr[id<<1],id<<1|1); - tr[id].tag=tr[id].add=0; -} -void build(int id,int l,int r) -{ - if(l==r) - { - tr[id]=(node){d[l],d[l],d[l]*d[l],d[l],0,0,1}; - return; - } - int mid=(l+r)>>1; - build(id<<1,l,mid); - build(id<<1|1,mid+1,r); - pushup(id); -} -void update(int id,int l,int r,int x,int y,int k,ll w) -{ - if(l>=x && r<=y) - { - mdf(id,k,w); - return; - } - int mid=(l+r)>>1; - pushdown(id); - if(x<=mid) update(id<<1,l,mid,x,y,k,w); - if(y>mid) update(id<<1|1,mid+1,r,x,y,k,w); - pushup(id); -} -int num[maxn],cnt,L[maxn],R[maxn]; -void get_num(int id,int l,int r,int x,int y) -{ - if(l>=x && r<=y) - { - cnt++; - num[cnt]=id,L[cnt]=l,R[cnt]=r; - return; - } - int mid=(l+r)>>1; - pushdown(id); - if(x<=mid) get_num(id<<1,l,mid,x,y); - if(y>mid) get_num(id<<1|1,mid+1,r,x,y); -} -int get_pos(int id,int l,int r,node nd,ll t) -{ - if(l==r) return l; - int mid=(l+r)>>1; - pushdown(id); - node dt=calc(nd,id<<1); - if(dt.t<=t) return get_pos(id<<1|1,mid+1,r,dt,t); - else return get_pos(id<<1,l,mid,nd,t); -} -int main() -{ - //system("fc training.out training6.ans /W"); - //return 0; - ios::sync_with_stdio(0); - cin.tie(0);cout.tie(0); - //freopen("training6.in","r",stdin); - //freopen("training.out","w",stdout); - int fl; - node nd,dt; - char opt; - ll s,t,w; - cin>>n>>q; - for(int i=0;i>d[i]; - build(1,0,n-1); - while(q--) - { - cin>>opt>>s>>t; - if(opt=='P') - { - cin>>w; - if(s<=t) update(1,0,n-1,s,t,0,w); - else update(1,0,n-1,0,t,0,w),update(1,0,n-1,s,n-1,0,w); - } - if(opt=='R') - { - cin>>w; - if(s<=t) update(1,0,n-1,s,t,1,w); - else update(1,0,n-1,0,t,1,w),update(1,0,n-1,s,n-1,1,w); - } - if(opt=='Q') - { - fl=0,cnt=0,nd=(node){0,0,0,0,0,0,0}; - get_num(1,0,n-1,s,n-1); - get_num(1,0,n-1,0,n-1); - for(int i=1;i<=cnt;i++) - { - dt=calc(nd,num[i]); - if(dt.t>t) - { - fl=1; - cout< +// #define ll long long +// using namespace std; +// const int maxn=2e5+5; +// struct node +// { +// ll mx,sum,t,sv,add,tag,len; +// }; +// node tr[maxn<<2]; +// int n,q; +// ll d[maxn<<2]; +// inline void mdf(int id,int k,ll w) +// { +// if(k==0) +// { +// tr[id].mx+=w; +// tr[id].t+=w*w*tr[id].len+tr[id].sv*w+tr[id].sum*w; +// tr[id].sum+=w*tr[id].len; +// tr[id].sv+=w*tr[id].len; +// tr[id].add+=w; +// } +// if(k==1) +// { +// tr[id].mx=w,tr[id].t=w*w*tr[id].len,tr[id].sum=w*tr[id].len,tr[id].sv=w*tr[id].len; +// tr[id].add=0,tr[id].tag=w; +// } +// } +// inline void pushdown(int id) +// { +// if(tr[id].tag) +// { +// mdf(id<<1,1,tr[id].tag); +// mdf(id<<1|1,1,tr[id].tag); +// } +// if(tr[id].add) +// { +// mdf(id<<1,0,tr[id].add); +// mdf(id<<1|1,0,tr[id].add); +// } +// tr[id].tag=tr[id].add=0; +// } +// node calc(node nd,int id) +// { +// node ret=nd; +// if(tr[id].len==1) +// { +// ret.mx=max(ret.mx,tr[id].mx); +// ret.sum+=tr[id].sum; +// ret.t+=tr[id].sum*ret.mx; +// ret.sv+=ret.mx; +// ret.len+=tr[id].len; +// } +// else +// { +// pushdown(id); +// if(tr[id<<1].mx>=nd.mx) +// { +// ret=calc(nd,id<<1); +// ret.mx=max(ret.mx,tr[id<<1|1].mx); +// ret.sum+=tr[id<<1|1].sum; +// ret.t+=tr[id].t-tr[id<<1].t; +// ret.sv+=tr[id].sv-tr[id<<1].sv; +// ret.len+=tr[id<<1|1].len; +// } +// else +// { +// nd.sum+=tr[id<<1].sum; +// nd.t+=tr[id<<1].sum*nd.mx; +// nd.sv+=tr[id<<1].len*nd.mx; +// nd.len+=tr[id<<1].len; +// ret=calc(nd,id<<1|1); +// } +// } +// return ret; +// } +// inline void pushup(int id) +// { +// tr[id]=calc(tr[id<<1],id<<1|1); +// tr[id].tag=tr[id].add=0; +// } +// void build(int id,int l,int r) +// { +// if(l==r) +// { +// tr[id]=(node){d[l],d[l],d[l]*d[l],d[l],0,0,1}; +// return; +// } +// int mid=(l+r)>>1; +// build(id<<1,l,mid); +// build(id<<1|1,mid+1,r); +// pushup(id); +// } +// void update(int id,int l,int r,int x,int y,int k,ll w) +// { +// if(l>=x && r<=y) +// { +// mdf(id,k,w); +// return; +// } +// int mid=(l+r)>>1; +// pushdown(id); +// if(x<=mid) update(id<<1,l,mid,x,y,k,w); +// if(y>mid) update(id<<1|1,mid+1,r,x,y,k,w); +// pushup(id); +// } +// int num[maxn],cnt,L[maxn],R[maxn]; +// void get_num(int id,int l,int r,int x,int y) +// { +// if(l>=x && r<=y) +// { +// cnt++; +// num[cnt]=id,L[cnt]=l,R[cnt]=r; +// return; +// } +// int mid=(l+r)>>1; +// pushdown(id); +// if(x<=mid) get_num(id<<1,l,mid,x,y); +// if(y>mid) get_num(id<<1|1,mid+1,r,x,y); +// } +// int get_pos(int id,int l,int r,node nd,ll t) +// { +// if(l==r) return l; +// int mid=(l+r)>>1; +// pushdown(id); +// node dt=calc(nd,id<<1); +// if(dt.t<=t) return get_pos(id<<1|1,mid+1,r,dt,t); +// else return get_pos(id<<1,l,mid,nd,t); +// } +// int main() +// { +// //system("fc training.out training6.ans /W"); +// //return 0; +// ios::sync_with_stdio(0); +// cin.tie(0);cout.tie(0); +// //freopen("training6.in","r",stdin); +// //freopen("training.out","w",stdout); +// int fl; +// node nd,dt; +// char opt; +// ll s,t,w; +// cin>>n>>q; +// for(int i=0;i>d[i]; +// build(1,0,n-1); +// while(q--) +// { +// cin>>opt>>s>>t; +// if(opt=='P') +// { +// cin>>w; +// if(s<=t) update(1,0,n-1,s,t,0,w); +// else update(1,0,n-1,0,t,0,w),update(1,0,n-1,s,n-1,0,w); +// } +// if(opt=='R') +// { +// cin>>w; +// if(s<=t) update(1,0,n-1,s,t,1,w); +// else update(1,0,n-1,0,t,1,w),update(1,0,n-1,s,n-1,1,w); +// } +// if(opt=='Q') +// { +// fl=0,cnt=0,nd=(node){0,0,0,0,0,0,0}; +// get_num(1,0,n-1,s,n-1); +// get_num(1,0,n-1,0,n-1); +// for(int i=1;i<=cnt;i++) +// { +// dt=calc(nd,num[i]); +// if(dt.t>t) +// { +// fl=1; +// cout< -using namespace std; -typedef long long ll; -const int N=200001; -int n,q; -int a[N]; -struct node{ - int mx; - ll sm,smx,tt; - int tg1,tg2; -}t[N*4]; -#define ls (p<<1) -#define rs (p<<1|1) -#define mid (l+r>>1) -void build(int p,int l,int r){ - for(int i=l;i<=r;i++){ - t[p].mx=max(t[p].mx,a[i]); - t[p].sm+=a[i]; - t[p].smx+=t[p].mx; - t[p].tt+=1ll*t[p].mx*a[i]; - } - if(l==r)return; - build(ls,l,mid); - build(rs,mid+1,r); -} -void pushdown(int p,int l,int r){ - int tg; - if(t[p].tg1){ - tg=t[p].tg1; - t[p].tg1=0; - t[ls].mx=t[rs].mx=tg; - t[ls].sm=1ll*(mid-l+1)*tg,t[rs].sm=1ll*(r-mid)*tg; - t[ls].smx=t[ls].sm,t[rs].smx=t[rs].sm; - t[ls].tt=t[ls].sm*tg,t[rs].tt=t[rs].sm*tg; - t[ls].tg1=t[rs].tg1=tg; - t[ls].tg2=t[rs].tg2=0; - } - if(t[p].tg2){ - tg=t[p].tg2; - t[p].tg2=0; - t[ls].mx+=tg,t[rs].mx+=tg; - t[ls].tt+=tg*(1ll*tg*(mid-l+1)+t[ls].sm+t[ls].smx),t[rs].tt+=tg*(1ll*tg*(r-mid)+t[rs].sm+t[rs].smx); - t[ls].sm+=1ll*(mid-l+1)*tg,t[rs].sm+=1ll*(r-mid)*tg; - t[ls].smx+=1ll*(mid-l+1)*tg,t[rs].smx+=1ll*(r-mid)*tg; - t[ls].tg2+=tg,t[rs].tg2+=tg; - } -} -int qmx; -ll qsmx,qtt; -void query(int p,int l,int r){ - if(l==r){ - if(qmx>t[p].mx)qsmx+=qmx,qtt+=qmx*t[p].sm; - else qsmx+=t[p].mx,qtt+=t[p].tt,qmx=t[p].mx; - return; - } - pushdown(p,l,r); - if(qmx>t[ls].mx){ - qsmx+=1ll*qmx*(mid-l+1); - qtt+=qmx*t[ls].sm; - query(rs,mid+1,r); - } - else{ - qsmx+=t[p].smx-t[ls].smx; - qtt+=t[p].tt-t[ls].tt; - query(ls,l,mid); - qmx=t[p].mx; - } -} -void update(int p,int l,int r){ - t[p].mx=max(t[ls].mx,t[rs].mx); - t[p].sm=t[ls].sm+t[rs].sm; - qmx=t[ls].mx; - qsmx=t[ls].smx; - qtt=t[ls].tt; - query(rs,mid+1,r); - t[p].smx=qsmx; - t[p].tt=qtt; -} -void modify1(int p,int l,int r,int x,int y,int d){ - if(x<=l&&r<=y){ - t[p].mx=d; - t[p].sm=1ll*(r-l+1)*d; - t[p].smx=t[p].sm; - t[p].tt=t[p].sm*d; - t[p].tg1=d; - t[p].tg2=0; - return; - } - pushdown(p,l,r); - if(x<=mid)modify1(ls,l,mid,x,y,d); - if(midtm)return l-1; - return l; - } - pushdown(p,l,r); - int mx=qmx; - ll tt=qtt; - query(ls,l,mid); - if(qtt>tm){ - qmx=mx,qtt=tt; - return gettt(ls,l,mid,tm); - } - return gettt(rs,mid+1,r,tm); -} -int getsm(int p,int l,int r,ll tm){ - if(l==r){ - if(t[p].sm>tm)return l-1; - return l; - } - pushdown(p,l,r); - if(t[ls].sm>tm)return getsm(ls,l,mid,tm); - return getsm(rs,mid+1,r,tm-t[ls].sm); -} int main(){ - ios::sync_with_stdio(0); - cin.tie(0),cout.tie(0); - cin>>n>>q; - for(int i=0;i>a[i]; - build(1,0,n-1); - while(q--){ - char op[2]; - cin>>op; - if(op[0]=='R'){ - int l,r,d; - cin>>l>>r>>d; - if(l<=r)modify1(1,0,n-1,l,r,d); - else modify1(1,0,n-1,l,n-1,d),modify1(1,0,n-1,0,r,d); - } - if(op[0]=='P'){ - int l,r,d; - cin>>l>>r>>d; - if(l<=r)modify2(1,0,n-1,l,r,d); - else modify2(1,0,n-1,l,n-1,d),modify2(1,0,n-1,0,r,d); - } - if(op[0]=='Q'){ - int s; - ll tm; - cin>>s>>tm; - m=0; - getseg(1,0,n-1,s,n-1); - if(s)getseg(1,0,n-1,0,s-1); - bool flag=0; - int mx=0; - ll tt=0; - for(int i=1;i<=m;i++){ - qmx=mx,qtt=tt; - query(b[i].p,b[i].l,b[i].r); - if(qtt>tm){ - qmx=mx,qtt=tt; - cout<<(gettt(b[i].p,b[i].l,b[i].r,tm)+1)%n<<'\n'; - flag=1; - break; - } - mx=qmx,tt=qtt; - } - if(flag)continue; - tm-=qtt; - tm/=t[1].mx; - tm%=t[1].sm; - for(int i=1;i<=m;i++){ - if(t[b[i].p].sm>tm){ - cout<<(getsm(b[i].p,b[i].l,b[i].r,tm)+1)%n<<'\n'; - break; - } - tm-=t[b[i].p].sm; - } - } - } - return 0; -} + +} \ No newline at end of file diff --git a/src/P1395/P1395.cpp b/src/P1395/P1395.cpp index eb8caa1..dbfebd9 100644 --- a/src/P1395/P1395.cpp +++ b/src/P1395/P1395.cpp @@ -1,9 +1,11 @@ #include #include +#include #include using ll = int64_t; using vl = std::vector; +#define N(v)#v<<" :"<<(v)<<" " auto &ci = std::cin; auto &co = std::cout; @@ -12,10 +14,10 @@ ll *sn = new ll[max_n]; auto *nds = new vl[max_n]; void dfs(ll ft, ll n){ - if(nds[n].size()<=1){ + sn[n]=1; + if(nds[n].size()==1 && nds[n][0]==ft){ return; } - sn[n]=1; for(ll i:nds[n]){ if(i==ft){ continue; @@ -35,6 +37,43 @@ int main(){ nds[a].push_back(b); nds[b].push_back(a); } - for(ll i{1};i<=n;i++)co<nn && i!=now){ + nn = sn[i]; + nxt = i; + } + } + co<<"nxt: "<nn && i!=f){ + nn = sn[i]; + nxt = i; + } + } + } + ft=t, fb = b; + t+=(sn[now]-sn[nxt]); + b = sn[nxt]-1; + co<<"now: "<