2631: tree
来源:互联网 发布:大数据分析师招聘 编辑:程序博客网 时间:2024/05/22 14:31
2631: tree
Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 3561 Solved: 1189
[Submit][Status][Discuss]
Description
一棵n个点的树,每个点的初始权值为1。对于这棵树有q个操作,每个操作为以下四种操作之一:
+ u v c:将u到v的路径上的点的权值都加上自然数c;
- u1 v1 u2 v2:将树中原有的边(u1,v1)删除,加入一条新边(u2,v2),保证操作完之后仍然是一棵树;
* u v c:将u到v的路径上的点的权值都乘上自然数c;
/ u v:询问u到v的路径上的点的权值和,求出答案对于51061的余数。
Input
第一行两个整数n,q
接下来n-1行每行两个正整数u,v,描述这棵树
接下来q行,每行描述一个操作
接下来n-1行每行两个正整数u,v,描述这棵树
接下来q行,每行描述一个操作
Output
对于每个/对应的答案输出一行
Sample Input
3 2
1 2
2 3
* 1 3 4
/ 1 1
1 2
2 3
* 1 3 4
/ 1 1
Sample Output
4
HINT
数据规模和约定
10%的数据保证,1<=n,q<=2000
另外15%的数据保证,1<=n,q<=5*10^4,没有-操作,并且初始树为一条链
另外35%的数据保证,1<=n,q<=5*10^4,没有-操作
100%的数据保证,1<=n,q<=10^5,0<=c<=10^4
Source
#include<iostream>#include<cstdio>#include<queue>#include<vector>#include<bitset>#include<algorithm>#include<cstring>#include<map>#include<stack>#include<set>#include<cmath>#include<ext/pb_ds/priority_queue.hpp>using namespace std;const int maxn = 1E5 + 10;typedef unsigned int LL;const LL mo = 51061;int n,m,ch[maxn][2],rev[maxn],fa[maxn],pfa[maxn],vis[maxn];LL va[maxn],sum[maxn],Add[maxn],siz[maxn],Multi[maxn];char com[10];vector <int> v[maxn];queue <int> Q;void pushdown(int x){if (rev[x]) {swap(ch[x][0],ch[x][1]); rev[x] ^= 1;if (ch[x][0]) rev[ch[x][0]] ^= 1;if (ch[x][1]) rev[ch[x][1]] ^= 1;}if (Add[x] || Multi[x] != 1) {va[x] *= Multi[x]; va[x] %= mo;sum[x] *= Multi[x]; sum[x] %= mo;va[x] += Add[x]; va[x] %= mo;sum[x] += Add[x]*siz[x]; sum[x] %= mo;for (int i = 0; i < 2; i++)if (ch[x][i]) {Add[ch[x][i]] = Multi[x]*Add[ch[x][i]] + Add[x],Add[ch[x][i]] %= mo;Multi[ch[x][i]] *= Multi[x],Multi[ch[x][i]] %= mo;}Add[x] = 0; Multi[x] = 1;}}void maintain(int x){siz[x] = 1; sum[x] = va[x];for (int i = 0; i < 2; i++)if (ch[x][i]) {siz[x] += siz[ch[x][i]];sum[x] += sum[ch[x][i]]; sum[x] %= mo;}}void rotate(int x){int y = fa[x],z = fa[y];pfa[x] = pfa[y]; pfa[y] = 0;int d = ch[y][0] == x?0:1;ch[y][d] = ch[x][d^1];if (ch[x][d^1]) fa[ch[x][d^1]] = y; ch[x][d^1] = y; fa[y] = x; fa[x] = z;if (z) ch[z][ch[z][1] == y] = x;if (ch[y][0]) pushdown(ch[y][0]);if (ch[y][1]) pushdown(ch[y][1]);maintain(y);}int s[maxn],top;void splay(int x){top = 0;for (int now = x; now; now = fa[now]) s[++top] = now;while (top) {pushdown(s[top--]);}for (int y; y = fa[x]; rotate(x))if (fa[y])rotate((ch[fa[y]][0] == y)^(ch[y][0] == x)?x:y);if (ch[x][0]) pushdown(ch[x][0]);if (ch[x][1]) pushdown(ch[x][1]);maintain(x);}void Access(int x){for (int u = x,v = 0; u; v = u,u = pfa[u]) {splay(u);if (ch[u][1]) fa[ch[u][1]] = 0,pfa[ch[u][1]] = u;ch[u][1] = v; if (v) pfa[v] = 0,fa[v] = u;}}void ChangeRoot(int x) {Access(x); splay(x); rev[x] ^= 1;}void Join(int x,int y) {ChangeRoot(x); pfa[x] = y; splay(y);}void Cut(int x,int y) {ChangeRoot(x); Access(y); splay(y); ch[y][0] = fa[x] = 0;}int Query(int x,int y) {ChangeRoot(x); Access(y); splay(y); return sum[y];}void add(int x,int y,int c){ChangeRoot(x); Access(y); splay(y);Add[y] += 1LL*c; Add[y] %= mo; }void multi(int x,int y,int c){ChangeRoot(x); Access(y); splay(y);//Add[y] *= 1LL*c; Add[y] %= mo;Multi[y] *= 1LL*c; Multi[y] %= mo; }int getint(){char ch = getchar();int ret = 0;while (ch < '0' || '9' < ch) ch = getchar();while ('0' <= ch && ch <= '9') ret = ret*10 + ch - '0',ch = getchar();return ret;}char c[20];void print( int k ) { if (!k) {puts("0"); return;} int num = 0; while( k > 0 ) c[++num] = k % 10, k /= 10; while( num ) putchar( c[num--]+48 ); putchar( 10 ); } int main(){freopen("a.in","r",stdin);freopen("a.out","w",stdout); n = getint(); m = getint();for (int i = 1; i <= n; i++) sum[i] = siz[i] = Multi[i] = va[i] = 1;for (int i = 1; i < n; i++) Join(getint(),getint());while (m--) {scanf("%s",com);int x = getint(),y = getint();if (com[0] == '+') {int c = getint(); add(x,y,c);}else if (com[0] == '-') {Cut(x,y); x = getint(); y = getint();Join(x,y);}else if (com[0] == '*') {int c = getint(); multi(x,y,c);}else print(Query(x,y));}return 0;}
0 0
- 2631: tree
- BZOJ 2631 Tree Link-Cut-Tree(LCT)
- BZOJ-2631 tree Link-Cut-Tree
- bzoj 2631 Tree [Link-Cut Tree]
- BZOJ 2631 tree Link Cut Tree
- bzoj 2631: tree
- BZOJ 2631 tree LCT
- BZOJ 2631 TREE
- 【LCT】BZOJ 2631:tree
- 2631: tree LCT
- bzoj 2631 tree
- BZOJ 2631 Tree LCT
- BZOJ 2631 tree
- BZOJ 2631: tree
- BZOJ 2631: tree Link_Cut_Tree
- BZOJ 2631 tree LCT
- 【BZOJ 2631】tree LCT
- BZOJ 2631 tree
- Android 菜单(OptionMenu)大全
- matalb k-means函数使用方法(附一个很好的k-means参考博客)
- RHEL 7中安装部署LAMP服务器
- uva10129-欧拉回路
- IOS UIView 详解
- 2631: tree
- 【疑问笔记】window.onload vs $(document).ready vs defer
- ORA-12505: TNS: 监听程序当前无法识别连接描述符中所给出的SID等错误解决方法
- 使用shape 自定义ui之渐变半圆阴影
- 常用SQL语句总结(3)
- wilddog(野狗)js sdk和promise(bluebird)的结合使用
- 日期控件WdatePicker
- 移位运算(乘、除、余)
- __block __weak漫谈基础集合类(NSArray,NSSet,NSOrderedSet,NSDictionary,NSMapTable,NSHashTable, NSPointerArra