bzoj 4999: This Problem Is Too Simple!
来源:互联网 发布:pascal语言编程难吗 编辑:程序博客网 时间:2024/05/22 11:38
题意:
给您一颗树,每个节点有个初始值。
现在支持以下两种操作:
1. C i x(0<=x<2^31) 表示将i节点的值改为x。
2. Q i j x(0<=x<2^31) 表示询问i节点到j节点的路径上有多少个值为x的节点。
题解:
看到这种东西,首先想到的就是树剖了吧。
但假如对于每一种值都开一棵线段树,空间爆炸。
但动态开点就没问题了。
关于值比较大,用map就行了。
code:
#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<map>#include<algorithm>using namespace std;struct TRnode{ int fa,son,tot,top,dep;}TR[100010];int ys[100010],z=0;struct trnode{ int lc,rc,c;}tr[5000010];int tot=0,root[300010];map <int,int>s;int num=0;int color[100010],n,m;struct node{ int y,next;}a[200010];int len=0,last[100010];void ins(int x,int y){ a[++len].y=y; a[len].next=last[x];last[x]=len;}void pre_node(int x,int fa){ TR[x].fa=fa;TR[x].dep=TR[fa].dep+1;TR[x].tot=1; for(int i=last[x];i;i=a[i].next) { int y=a[i].y; if(y==fa) continue; pre_node(y,x); if(TR[y].tot>TR[TR[x].son].tot) TR[x].son=y; TR[x].tot+=TR[y].tot; }}void pre_edge(int x,int top){ ys[x]=++z;TR[x].top=top; if(TR[x].son) pre_edge(TR[x].son,top); for(int i=last[x];i;i=a[i].next) { int y=a[i].y; if(y==TR[x].fa||y==TR[x].son) continue; pre_edge(y,y); }}void update(int &x,int l,int r,int k,int c){ if(!x) x=++tot; if(l==r){tr[x].c+=c;return;} int mid=(l+r)/2; if(k<=mid) update(tr[x].lc,l,mid,k,c); else update(tr[x].rc,mid+1,r,k,c); tr[x].c=tr[tr[x].lc].c+tr[tr[x].rc].c;}int findans(int &x,int l,int r,int fl,int fr){ if(!x) return 0; if(l==fl&&r==fr) return tr[x].c; int mid=(l+r)/2; if(fr<=mid) return findans(tr[x].lc,l,mid,fl,fr); if(fl>mid) return findans(tr[x].rc,mid+1,r,fl,fr); return findans(tr[x].lc,l,mid,fl,mid)+findans(tr[x].rc,mid+1,r,mid+1,fr);}int solve(int froot,int x,int y){ int tx=TR[x].top,ty=TR[y].top,ans=0; while(tx!=ty) { if(TR[tx].dep<TR[ty].dep) swap(x,y),swap(tx,ty); ans+=findans(froot,1,n,ys[tx],ys[x]); x=TR[tx].fa;tx=TR[x].top; } if(TR[x].dep<TR[y].dep) swap(x,y); ans+=findans(froot,1,n,ys[y],ys[x]); return ans;}int main(){ s.clear(); scanf("%d %d",&n,&m); for(int i=1;i<=n;i++) { int x;scanf("%d",&x); if(!s[x]) s[x]=++num; color[i]=s[x]; } for(int i=1;i<n;i++) { int x,y;scanf("%d %d",&x,&y); ins(x,y);ins(y,x); } pre_node(1,0);pre_edge(1,1); for(int i=1;i<=n;i++) update(root[color[i]],1,n,ys[i],1); while(m--) { char S[3];scanf("%s",S); if(S[0]=='C') { int i,x;scanf("%d %d",&i,&x); if(!s[x]) s[x]=++num; update(root[color[i]],1,n,ys[i],-1); color[i]=s[x]; update(root[color[i]],1,n,ys[i],1); } else { int i,j,x;scanf("%d %d %d",&i,&j,&x); if(!s[x]) s[x]=++num; printf("%d\n",solve(root[s[x]],i,j)); } }}
阅读全文
2 0
- bzoj 4999: This Problem Is Too Simple!
- bzoj 4999: This Problem Is Too Simple!
- BZOJ 4999: This Problem Is Too Simple!
- [BZOJ]4999: This Problem Is Too Simple! 树链剖分
- bzoj 4999: This Problem Is Too Simple! 树链剖分+线段树
- 4999: This Problem Is Too Simple!
- 4999: This Problem Is Too Simple!
- bzoj 4999 This problem is too simple 树链剖分+动态开点线段树
- 【bzoj4999】This Problem Is Too Simple!
- bzoj4999 This Problem Is Too Simple!(树链剖分+动态开点线段树)
- SVN update problem: This client is too old to work with working copy
- This is the first problem for test. Since all we know the ASCII code, your job is simple: Input numb
- Typically this problem occurs when you are putting in a data that is too long for the column. In t
- BZOJ 3489 A simple rmq problem
- 【31.94%】【BZOJ 3489】A simple rmq problem
- bzoj 3489: A simple rmq problem
- BZOJ 3489 A simple rmq problem
- BZOJ 3489: A simple rmq problem 树套树
- Google六驾马车:MapReduce/Bigtable/GFS和Caffeine/Dremel/Pregel
- 点击事件知识点
- Death Podracing HDU
- HTML5的特性与优缺点
- 非常棒的网络流总结
- bzoj 4999: This Problem Is Too Simple!
- 设计模式之外观模式
- 2017 GDUFS ACM暑假集训 总结
- Android关机流程
- Time travel HDU
- java.lang.AbstractMethodError com.mysql.jdbc.Connection.isValid(I)Z
- GCC, MSVC 实现静态链接、动态链接
- UVa 11384 Help is needed for Dexter
- SniperOJ Web by Assassin