Steven_MengのBlog

传送门

需要一棵支持区间加,区间和查询的线段树

其他没什么好说的,注意开$\rm long$ $\rm long$

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
#include <bits/stdc++.h>
#define MAXN 2000005
#define int long long
using namespace std;
inline int read(){
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9'){
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9'){
x=(x<<3)+(x<<1)+(ch^'0');
ch=getchar();
}
return x*f;
}
vector<int>G[MAXN];
inline void AddEdge(int u,int v){
G[u].push_back(v);
}
int sz[MAXN],big[MAXN],fa[MAXN],top[MAXN],dep[MAXN];
void dfs1(int u,int father){
fa[u]=father;sz[u]=1;
for (register int i=0;i<G[u].size();++i){
int v=G[u][i];
if (v!=father){
dep[v]=dep[u]+1;
dfs1(v,u);
sz[u]+=sz[v];
if (sz[big[u]]<sz[v]) big[u]=v;
}
}
}
int ql[MAXN],qr[MAXN],cnt;
void dfs2(int u,int t){
ql[u]=++cnt;
top[u]=t;
if (big[u]) dfs2(big[u],t);
for (register int i=0;i<G[u].size();++i){
int v=G[u][i];
if (v!=fa[u]&&v!=big[u]){
dfs2(v,v);
}
}
}
namespace SegmentTree{
struct node{
int l,r;
int val,tag;
inline int len(){
return r-l+1;
}
}tree[MAXN<<2];
#define lc i<<1
#define rc i<<1|1
inline void pushup(int i){
tree[i].val=tree[lc].val+tree[rc].val;
}
inline void Change(int i,int val){
tree[i].val+=val*tree[i].len();
tree[i].tag+=val;
}
inline void pushdown(int i){
if (tree[i].tag){
Change(lc,tree[i].tag);
Change(rc,tree[i].tag);
tree[i].tag=0;
}
}
void Build(int i,int l,int r){
tree[i].l=l,tree[i].r=r;
tree[i].val=tree[i].tag=0;
if (l==r) {
return ;
}
int mid=(l+r)>>1;
Build(lc,l,mid);
Build(rc,mid+1,r);
}
int Query(int i,int L,int R){
if (L<=tree[i].l&&tree[i].r<=R){
return tree[i].val;
}
int mid=(tree[i].l+tree[i].r)>>1,ans=0;
pushdown(i);
if (L<=mid) ans+=Query(lc,L,R);
if (mid<R) ans+=Query(rc,L,R);
return ans;
}
void Update(int i,int L,int R,int val){
if (L<=tree[i].l&&tree[i].r<=R){
Change(i,val);
return ;
}
int mid=(tree[i].l+tree[i].r)>>1;
pushdown(i);
if (L<=mid) Update(lc,L,R,val);
if (mid<R) Update(rc,L,R,val);
pushup(i);
}
}
using namespace SegmentTree;
inline void Update_Chain(int u,int v,int w){
while (top[u]!=top[v]){
if (dep[top[u]]<dep[top[v]]){
swap(u,v);
}
Update(1,ql[top[u]],ql[u],w);
u=fa[top[u]];
}
if (dep[u]>dep[v]) swap(u,v);
Update(1,ql[u],ql[v],w);
}
inline void Init(){
dfs1(1,0);
dfs2(1,1);
}
#undef int
int main(){
#define int long long
int n=read();
for (register int i=1;i<n;++i){
int u=read()+1,v=read()+1;
AddEdge(u,v);
AddEdge(v,u);
}
Init();
Build(1,1,cnt);
int m=read();
while (m--){
char ch[2];
scanf("%s",ch);
if (ch[0]=='Q') {
int u=read()+1;
printf("%lld\n",Query(1,ql[u],ql[u]+sz[u]-1));
}
else if (ch[0]=='A'){
int u=read()+1,v=read()+1,w=read();
Update_Chain(u,v,w);
}
}
}

 评论