王学长的LCT标程

时间:2022-09-02 20:12:30

善良的王学长竟然亲自打了一遍QAQ好感动QAQ

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#define PAU putchar(' ')
#define ENT putchar('\n')
using namespace std;
const int maxn=+;
inline int read(){
int x=,sig=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')sig=-;ch=getchar();}
while(isdigit(ch))x=*x+ch-'',ch=getchar();
return x*=sig;
}
inline void write(int x){
if(x==){putchar('');return;}if(x<)putchar('-'),x=-x;
int len=,buf[];while(x)buf[len++]=x%,x/=;
for(int i=len-;i>=;i--)putchar(buf[i]+'');return;
}
struct node {
node *ch[],*fa;
bool rev;
int x,sum,siz,mul,add;
inline void add_rev_tag(){
swap(ch[],ch[]);rev^=;return;
}
inline void add_plus_tag(int a){
sum+=siz*a;x+=a;add+=a;return;
}
inline void add_mul_tag(int m){
sum*=m;x*=m;mul*=m;add*=add*m;return;
}
inline void down(){
if(rev){
if(ch[]) ch[]->add_rev_tag();
if(ch[]) ch[]->add_rev_tag();
rev=;
}
if(add){
if(ch[]) ch[]->add_plus_tag(add);
if(ch[]) ch[]->add_plus_tag(add);
add=;
}
if(mul>){
if(ch[]) ch[]->add_mul_tag(mul);
if(ch[]) ch[]->add_mul_tag(mul);
mul=;
} return;
}
inline void update(){
sum=x;siz=;
if(ch[]) sum+=ch[]->sum,siz+=ch[]->siz;
if(ch[]) sum+=ch[]->sum,siz+=ch[]->siz;
return;
}
}lct[maxn];
inline int get_parent(node *x,node *&fa){return (fa=x->fa)?fa->ch[]==x?:fa->ch[]==x?:-:-;}
inline void rotate(node *x){
int t1,t2;
node *fa,*gfa;
t1=get_parent(x,fa);
t2=get_parent(fa,gfa);
if ((fa->ch[t1]=x->ch[t1^])) fa->ch[t1]->fa=fa;
x->ch[t1^]=fa;fa->fa=x;x->fa=gfa;
if (t2!=-) gfa->ch[t2]=x;
fa->update();return;
}
inline void pushdown(node *x){
static node *stack[maxn];
int cnt=;
while(){
stack[cnt++]=x;
node *fa=x->fa;
if (!fa || (fa->ch[]!=x && fa->ch[]!=x)) break;
x=fa;
}
while(cnt--) stack[cnt]->down();
return;
}
inline node * splay(node *x){
pushdown(x);
while(){
int t1,t2;
node *fa,*gfa;
t1=get_parent(x,fa);
if(t1==-) break;
t2=get_parent(fa,gfa);
if(t2==-){
rotate(x);break;
} else if (t1==t2){
rotate(fa);rotate(x);
} else{
rotate(x);rotate(x);
}
}
x->update();
return x;
}
inline node * access(node *x){
node *ret=NULL;
while (x) splay(x)->ch[]=ret,(ret=x)->update(),x=x->fa;
return ret;
}
inline void makeroot(int x){access(lct+x)->add_rev_tag();}
inline void link(int u,int v){
makeroot(u);splay(lct+u)->fa=lct+v;return;
}
inline void cut(int u,int v){
makeroot(u);
node *p=(access(lct+v),splay(lct+v));
p->ch[]->fa=NULL;
p->ch[]=NULL;
p->update();
}
int n,q;
int main(){
n=read();q=read();
int i;
for(i=;i<=n;i++) {
lct[i].x=lct[i].sum=;
lct[i].siz=;
lct[i].mul=;
}
for(i=;i<n;i++){
int u,v;
u=read();v=read();
link(u,v);
}
while(q--){
char ch=getchar();
while(ch<=) ch=getchar();
int u,v,x,y,c;
if(ch=='+'){
u=read();v=read();c=read();
makeroot(u);
access(lct+v)->add_plus_tag(c);
}else if(ch=='-'){
u=read();v=read();x=read();y=read();
cut(u,v);link(x,y);
}else if(ch=='*'){
u=read();v=read();c=read();
makeroot(u);
access(lct+v)->add_mul_tag(c);
}else if(ch=='/'){
u=read();v=read();
makeroot(u);
printf("%u\n",access(lct+v)->sum);
}
}
return ;
}

搜索

复制