Tree
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3039 Accepted Submission(s): 524
Problem Description
You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are numbered from 1 to N
There are N - 1 edges numbered from 1 to N - 1.
Each node has a value and each edge has a value. The initial value is 0.
There are two kind of operation as follows:
● ADD1 u v k: for nodes on the path from u to v, the value of these nodes increase by k.
● ADD2 u v k: for edges on the path from u to v, the value of these edges increase by k.
After finished M operation on the tree, please output the value of each node and edge.
There are N - 1 edges numbered from 1 to N - 1.
Each node has a value and each edge has a value. The initial value is 0.
There are two kind of operation as follows:
● ADD1 u v k: for nodes on the path from u to v, the value of these nodes increase by k.
● ADD2 u v k: for edges on the path from u to v, the value of these edges increase by k.
After finished M operation on the tree, please output the value of each node and edge.
Input
The first line of the input is T (1 ≤ T ≤ 20), which stands for the number of test cases you need to solve.
The first line of each case contains two integers N ,M (1 ≤ N, M ≤10 5),denoting the number of nodes and operations, respectively.
The next N - 1 lines, each lines contains two integers u, v(1 ≤ u, v ≤ N ), denote there is an edge between u,v and its initial value is 0.
For the next M line, contain instructions “ADD1 u v k” or “ADD2 u v k”. (1 ≤ u, v ≤ N, -10 5 ≤ k ≤ 10 5)
The first line of each case contains two integers N ,M (1 ≤ N, M ≤10 5),denoting the number of nodes and operations, respectively.
The next N - 1 lines, each lines contains two integers u, v(1 ≤ u, v ≤ N ), denote there is an edge between u,v and its initial value is 0.
For the next M line, contain instructions “ADD1 u v k” or “ADD2 u v k”. (1 ≤ u, v ≤ N, -10 5 ≤ k ≤ 10 5)
Output
For each test case, print a line “Case #t:”(without quotes, t means the index of the test case) at the beginning.
The second line contains N integer which means the value of each node.
The third line contains N - 1 integer which means the value of each edge according to the input order.
The second line contains N integer which means the value of each node.
The third line contains N - 1 integer which means the value of each edge according to the input order.
Sample Input
2 4 2 1 2 2 3 2 4 ADD1 1 4 1 ADD2 3 4 2 4 2 1 2 2 3 1 4 ADD1 1 4 5 ADD2 3 2 4
Sample Output
Case #1: 1 1 0 1 0 2 2 Case #2: 5 0 0 5 0 4 0
思路:直接用线段树维护链会T。有一个区间更新的规律——[l, r]增k,相当于val[l] += k, val[r+1] -= k,求解一个点val[i] = sigma(val[j]) 1 <= j <= i-1。这样树剖后求解前缀和就可以了。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (100000+10)
#define MAXM (300000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while((a)--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
#pragma comment(linker, "/STACK:102400000,102400000")
#define fi first
#define se second
using namespace std;
struct Edge{
int to, next;
};
Edge edge[MAXN<<1];
int head[MAXN], edgenum;
void init(){
edgenum = 0; CLR(head, -1);
}
void addEdge(int u, int v)
{
edge[edgenum].to = v;
edge[edgenum].next = head[u];
head[u] = edgenum++;
}
int son[MAXN], num[MAXN];
int top[MAXN], pos[MAXN], pedge[MAXN], id;
int dep[MAXN], pre[MAXN];
void DFS1(int u, int fa, int d)
{
dep[u] = d; pre[u] = fa; num[u] = 1; son[u] = -1;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(v == fa) continue;
DFS1(v, u, d+1);
num[u] += num[v];
if(son[u] == -1 || num[son[u]] < num[v])
son[u] = v;
}
}
void DFS2(int u, int T)
{
top[u] = T; pos[u] = ++id; pedge[id] = u;
if(son[u] == -1) return ;
DFS2(son[u], T);
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(v == pre[u] || v == son[u]) continue;
DFS2(v, v);
}
}
LL Ans[MAXN];
void Change(int u, int v, LL z, int op)
{
int f1 = top[u], f2 = top[v];
while(f1 != f2)
{
if(dep[f1] < dep[f2])
{
swap(u, v);
swap(f1, f2);
}
Ans[pos[f1]] += z; Ans[pos[u]+1] -= z;
//Pl(Ans[pos[f1]]);
u = pre[f1], f1 = top[u];
}
if(op == 2 && u == v) return ;
if(dep[u] > dep[v]) swap(u, v);
if(op == 1) {Ans[pos[u]] += z, Ans[pos[v]+1] -= z;}
else {Ans[pos[son[u]]] += z; Ans[pos[v]+1] -= z;}
}
struct Node{
int op, x, y; LL z;
};
Node In[MAXN];
bool cmp(Node a, Node b){
return a.op < b.op;
}
int s[MAXN], e[MAXN];
int main()
{
int t, kcase = 1; Ri(t);
W(t)
{
int n, m; Ri(n); Ri(m); init();
for(int i = 1; i <= n-1; i++)
{
Ri(s[i]), Ri(e[i]);
addEdge(s[i], e[i]);
addEdge(e[i], s[i]);
}
DFS1(1, -1, 1); id = 0; DFS2(1, 1);
for(int i = 1; i <= m; i++)
{
char str[10]; Rs(str);
if(str[3] == '1') In[i].op = 1;
else In[i].op = 2;
Ri(In[i].x); Ri(In[i].y); Rl(In[i].z);
}
CLR(Ans, 0);
sort(In+1, In+m+1, cmp); int i = 1;
printf("Case #%d:\n", kcase++);
for(; i <= m; i++)
{
if(In[i].op == 2) break;
Change(In[i].x, In[i].y, In[i].z, 1);
}
for(int j = 1; j <= id; j++) Ans[j] += Ans[j-1];
for(int j = 1; j <= n; j++)
{
if(j > 1) printf(" ");
printf("%lld", Ans[pos[j]]);
}
printf("\n"); CLR(Ans, 0);
for(; i <= m; i++)
Change(In[i].x, In[i].y, In[i].z, 2);
for(int j = 1; j <= id; j++) Ans[j] += Ans[j-1];
for(int j = 1; j <= n-1; j++)
{
if(j > 1) printf(" ");
if(dep[s[j]] > dep[e[j]]) swap(s[j], e[j]);
printf("%lld", Ans[pos[e[j]]]);
}
printf("\n");
}
return 0;
}