-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3221.cpp
185 lines (169 loc) · 3.11 KB
/
3221.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
#define N 100100
#define M 200200
#define T 8008000
struct edge{
int t,next;
};
struct data{
int l,r;
ll s,p,q;
};
edge e[M];
data a[T];
int h[N],d[N],p[N],t[N],f[N],s[N],c[N];
int n,m,i,j,k,u,v,w,b,g;
ll ans,x,y;
char ch[2];
void addedge(int u,int v){
h[0]++;
e[h[0]]=(edge){v,h[u]};
h[u]=h[0];
}
void dfs1(int cur){
int i;
s[cur]=1;
for(i=h[cur];i!=-1;i=e[i].next)
if(e[i].t!=f[cur]){
f[e[i].t]=cur;
d[e[i].t]=d[cur]+1;
dfs1(e[i].t);
s[cur]+=s[e[i].t];
}
}
void dfs2(int cur,int root){
int i,c;
k++;
p[cur]=k;
t[cur]=root;
c=0;
for(i=h[cur];i!=-1;i=e[i].next)
if(e[i].t!=f[cur] && s[e[i].t]>s[c])c=e[i].t;
if(c)dfs2(c,root);
for(i=h[cur];i!=-1;i=e[i].next)
if(e[i].t!=f[cur] && e[i].t!=c)dfs2(e[i].t,e[i].t);
}
ll calc(ll u,ll v,int w){
return u+v*(w-1);
}
ll sum(ll u,ll v,int l,int r){
return (calc(u,v,l)+calc(u,v,r))*(r-l+1)>>1;
}
int lca(int u,int v){
while(t[u]!=t[v]){
if(d[t[u]]>d[t[v]])u=f[t[u]];
else v=f[t[v]];
}
return d[u]<d[v]?u:v;
}
void modify(int &cur,int pre,int l,int r,int u,int v,ll p,ll q){
int mid;
k++;
cur=k;
a[cur]=a[pre];
if(l==u && r==v){
a[cur].p+=p;
a[cur].q+=q;
return;
}
a[cur].s+=sum(p,q,1,v-u+1);
mid=(l+r)>>1;
if(v<=mid)modify(a[cur].l,a[pre].l,l,mid,u,v,p,q);
else if(u>mid)modify(a[cur].r,a[pre].r,mid+1,r,u,v,p,q);
else{
modify(a[cur].l,a[pre].l,l,mid,u,mid,p,q);
modify(a[cur].r,a[pre].r,mid+1,r,mid+1,v,calc(p,q,mid+2-u),q);
}
}
ll ask(int cur,int l,int r,int u,int v){
int mid;
ll s;
if(!cur)return 0;
s=sum(a[cur].p,a[cur].q,u-l+1,v-l+1);
if(l==u && r==v)return s+a[cur].s;
mid=(l+r)>>1;
if(v<=mid)s+=ask(a[cur].l,l,mid,u,v);
else if(u>mid)s+=ask(a[cur].r,mid+1,r,u,v);
else{
s+=ask(a[cur].l,l,mid,u,mid);
s+=ask(a[cur].r,mid+1,r,mid+1,v);
}
return s;
}
void add(int x,int y,int k,ll u,ll v){
int z,l,r,g;
z=lca(x,y);
l=1;
r=d[x]+d[y]-(d[z]<<1)+1;
while(t[x]!=t[y]){
if(d[t[x]]>d[t[y]]){
g=d[x]-d[t[x]]+l;
modify(c[k],c[k],1,n,p[t[x]],p[x],calc(u,v,g),-v);
l=g+1;
x=f[t[x]];
}else{
g=r-d[y]+d[t[y]];
modify(c[k],c[k],1,n,p[t[y]],p[y],calc(u,v,g),v);
r=g-1;
y=f[t[y]];
}
}
if(d[x]<d[y])modify(c[k],c[k],1,n,p[x],p[y],calc(u,v,l),v);
else modify(c[k],c[k],1,n,p[y],p[x],calc(u,v,r),-v);
}
ll query(int x,int y,int k){
ll s;
s=0;
while(t[x]!=t[y]){
if(d[t[x]]>d[t[y]]){
s+=ask(c[k],1,n,p[t[x]],p[x]);
x=f[t[x]];
}else{
s+=ask(c[k],1,n,p[t[y]],p[y]);
y=f[t[y]];
}
}
if(d[x]<d[y])s+=ask(c[k],1,n,p[x],p[y]);
else s+=ask(c[k],1,n,p[y],p[x]);
return s;
}
int main(){
scanf("%d%d",&n,&m);
memset(h,-1,sizeof(h));
for(i=1;i<n;i++){
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
dfs1(1);
dfs2(1,1);
g=b=k=0;
for(i=1;i<=m;i++){
scanf("%s",ch);
if(ch[0]=='c'){
scanf("%lld%lld%d%d",&x,&y,&u,&v);
x^=ans;
y^=ans;
g++;
c[g]=c[b];
b=g;
add(x,y,b,u,v);
}
if(ch[0]=='q'){
scanf("%lld%lld",&x,&y);
x^=ans;
y^=ans;
ans=query(x,y,b);
printf("%lld\n",ans);
}
if(ch[0]=='l'){
scanf("%lld",&x);
b=x^ans;
}
}
}