BZOJ-3531 旅行 树链剖分+动态开点线段树

来源:互联网 发布:sip协议端口号 编辑:程序博客网 时间:2024/05/14 19:45

3531: [Sdoi2014]旅行
Time Limit: 20 Sec Memory Limit: 512 MB
Submit: 1097 Solved: 517
[Submit][Status][Discuss]

Description
S国有N个城市,编号从1到N。城市间用N-1条双向道路连接,满足从一个城市出发可以到达其它所有城市。每个城市信仰不同的宗教,如飞天面条神教、隐形独角兽教、绝地教都是常见的信仰。为了方便,我们用不同的正整数代表各种宗教, S国的居民常常旅行。旅行时他们总会走最短路,并且为了避免麻烦,只在信仰和他们相同的城市留宿。当然旅程的终点也是信仰与他相同的城市。S国政府为每个城市标定了不同的旅行评级,旅行者们常会记下途中(包括起点和终点)留宿过的城市的评级总和或最大值。
在S国的历史上常会发生以下几种事件:
”CC x c”:城市x的居民全体改信了c教;
”CW x w”:城市x的评级调整为w;
”QS x y”:一位旅行者从城市x出发,到城市y,并记下了途中留宿过的城市的评级总和;
”QM x y”:一位旅行者从城市x出发,到城市y,并记下了途中留宿过
的城市的评级最大值。
由于年代久远,旅行者记下的数字已经遗失了,但记录开始之前每座城市的信仰与评级,还有事件记录本身是完好的。请根据这些信息,还原旅行者记下的数字。 为了方便,我们认为事件之间的间隔足够长,以致在任意一次旅行中,所有城市的评级和信仰保持不变。

Input
输入的第一行包含整数N,Q依次表示城市数和事件数。
接下来N行,第i+l行两个整数Wi,Ci依次表示记录开始之前,城市i的
评级和信仰。
接下来N-1行每行两个整数x,y表示一条双向道路。
接下来Q行,每行一个操作,格式如上所述。

Output
对每个QS和QM事件,输出一行,表示旅行者记下的数字。

Sample Input
5 6
3 1
2 3
1 2
3 3
5 1
1 2
1 3
3 4
3 5
QS 1 5
CC 3 1
QS 1 5
CW 3 3
QS 1 5
QM 2 4

Sample Output
8
9
11
3

HINT
N,Q < =10^5 , C < =10^5
数据保证对所有QS和QM事件,起点和终点城市的信仰相同;在任意时
刻,城市的评级总是不大于10^4的正整数,且宗教值不大于C。

Source
Round 1 Day 1

题解:
树链剖分无疑,但是需要一些技巧,同样需要注意一些细节
首先需要对每个宗教建一棵线段树,所以做法需要写技巧,动态的开点,动态的建树,对每个宗教记录三个值,root,ll,rr,root记录的是这个宗教的根,ll是左儿子,rr右儿子;这样每次向下询问的时候,以及向上更新的时候就不再是now<<1 和now<<1|1了
注意了这些问题,剩下的就是些写法上的技巧了。
CC操作:把x城市在原来的宗教中评级置成0,在新的宗教中加入评级,并记录宗教的变化
CW操作:直接修改评级,记录下
QS操作:区间查询和,不断跳跳就好
QM操作:区间查询最大,同上。

code:(自己YY的开点线段树,一个地方写的丑,找男神看了看….YM)

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>using namespace std;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*10+ch-'0'; ch=getchar();}    return x*f;}#define maxn 301000int n,q;struct data{int next,to;}edge[maxn<<1];int head[maxn],cnt;struct dat{int w,c;}city[maxn];void add(int u,int v) {cnt++; edge[cnt].to=v; edge[cnt].next=head[u]; head[u]=cnt;}void insert(int u,int v){add(u,v); add(v,u);}//--------------------------------------------------------------------------------------int deep[maxn],size[maxn],fa[maxn],son[maxn],pl[maxn],sz,pr[maxn],pre[maxn],top[maxn];void dfs_1(int now){    size[now]=1;    for (int i=head[now]; i; i=edge[i].next)        if (edge[i].to!=fa[now])            {                fa[edge[i].to]=now;                deep[edge[i].to]=deep[now]+1;                dfs_1(edge[i].to);                if (size[son[now]]<size[edge[i].to]) son[now]=edge[i].to;                size[now]+=size[edge[i].to];            }}void dfs_2(int now,int chain){    pl[now]=++sz; pre[sz]=now; top[now]=chain;    if (son[now]) dfs_2(son[now],chain);    for (int i=head[now]; i; i=edge[i].next)        if (edge[i].to!=son[now] && edge[i].to!=fa[now])            dfs_2(edge[i].to,edge[i].to);    pr[now]=sz;}//--------------------------------------------------------------------------------------int sum[maxn<<2],maxx[maxn<<2],tot,root[maxn<<2],ll[maxn<<2],rr[maxn<<2];inline void update(int now){    sum[now]=sum[ll[now]]+sum[rr[now]];    if (maxx[ll[now]]>maxx[rr[now]])        maxx[now]=maxx[ll[now]]; else maxx[now]=maxx[rr[now]];}void point_change(int &now,int l,int r,int loc,int val){    if (!now) now=++tot;    if (l==r) {sum[now]=maxx[now]=val;return;}    int mid=(l+r)>>1;    if (loc<=mid) point_change(ll[now],l,mid,loc,val);    else point_change(rr[now],mid+1,r,loc,val);    update(now);}int segment_ask_sum(int now,int l,int r,int L,int R){    if (!now) return 0;    if(L<=l && R>=r) return sum[now];    int mid=(l+r)>>1;int ans=0;      if (L<=mid) ans+=segment_ask_sum(ll[now],l,mid,L,R);    if (R>mid) ans+=segment_ask_sum(rr[now],mid+1,r,L,R);    return ans;}int segment_ask_max(int now,int l,int r,int L,int R){    if (!now) return 0;    if (L<=l && R>=r) return maxx[now];    int mid=(l+r)>>1; int ans=-0x7fffffff;    if (L<=mid) ans=max(ans,segment_ask_max(ll[now],l,mid,L,R));    if (R>mid) ans=max(ans,segment_ask_max(rr[now],mid+1,r,L,R));    return ans;}//--------------------------------------------------------------------------------------void CC(int x,int y){    point_change(root[city[x].c],1,n,pl[x],0);      point_change(root[y],1,n,pl[x],city[x].w);      city[x].c=y;  }void CW(int x,int y){    point_change(root[city[x].c],1,n,pl[x],y);    city[x].w=y;}void QS(int x,int y){    int ans=0; int cc=city[x].c,tpx,tpy;    while (top[x]!=top[y])        {            tpx=top[x],tpy=top[y];            if (deep[tpx]<deep[tpy]) swap(tpx,tpy),swap(x,y);            ans+=segment_ask_sum(root[cc],1,n,pl[tpx],pl[x]);            x=fa[tpx];        }    //if (x==y) {printf("%d\n",ans);return;}    if (deep[x]>deep[y]) swap(x,y);    ans+=segment_ask_sum(root[cc],1,n,pl[x],pl[y]);    printf("%d\n",ans);}void QM(int x,int y){    int ans=-0x7fffffff; int cc=city[x].c,tpx,tpy;    while (top[x]!=top[y])        {            tpx=top[x],tpy=top[y];            if (deep[tpx]<deep[tpy]) swap(tpx,tpy),swap(x,y);            ans=max(ans,segment_ask_max(root[cc],1,n,pl[tpx],pl[x]));            x=fa[tpx];        }    if (deep[x]>deep[y]) swap(x,y);    ans=max(ans,segment_ask_max(root[cc],1,n,pl[x],pl[y]));    printf("%d\n",ans);}//--------------------------------------------------------------------------------------int main(){    n=read(),q=read();    for (int i=1; i<=n; i++) city[i].w=read(),city[i].c=read();    for (int u,v,i=1; i<=n-1; i++)        u=read(),v=read(),insert(u,v);    dfs_1(1);dfs_2(1,1);    for (int i=1; i<=n; i++) point_change(root[city[i].c],1,n,pl[i],city[i].w);    for (int i=1; i<=q; i++)        {            char opt[10]; scanf("%s",opt); int x=read(),y=read();            switch (opt[1])                {                    case 'C': CC(x,y); break;                    case 'S': QS(x,y); break;                    case 'M': QM(x,y); break;                    case 'W': CW(x,y); break;                }        }    return 0;}
0 0