【SDOI2014】【BZOJ3531】旅行

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

本来想资瓷一下LCT的做法..
可是..
LCT果然是错误的姿势(常数太大T_T)..
速度差点垫底..
听潇爷说这题事实上是弱化版..原题仅仅能用LCT?

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 100010
#define GET (ch>='0'&&ch<='9')
#define LL long long
#define MAXINT 0x7fffffff
using namespace std;
int n,m;
int sta[MAXN],top;
int noww[MAXN],nowc[MAXN];
struct splay
{
    int ch[2],fa,val,maxn;
    LL sum;
    bool rev;
}tree[MAXN];
void in(int &x)
{
    char ch=getchar();x=0;
    while (!GET)    ch=getchar();;
    while (GET) x=x*10+ch-'0',ch=getchar();
}
bool is_root(int x) {return tree[tree[x].fa].ch[0]!=x&&tree[tree[x].fa].ch[1]!=x;}
void push_up(int x)
{
    tree[x].maxn=max(tree[x].val,max(tree[tree[x].ch[0]].maxn,tree[tree[x].ch[1]].maxn));
    tree[x].sum=tree[tree[x].ch[0]].sum+tree[tree[x].ch[1]].sum+tree[x].val;
}
void push_down(int x)
{
    if (tree[x].rev)
    {
        tree[tree[x].ch[0]].rev^=1;tree[tree[x].ch[1]].rev^=1;
        swap(tree[x].ch[0],tree[x].ch[1]);tree[x].rev^=1;
    }
}
void rot(int x)
{
    int y=tree[x].fa,z=tree[y].fa,l,r;
    l=(tree[y].ch[1]==x);r=l^1;
    if (!is_root(y))    tree[z].ch[tree[z].ch[1]==y]=x;
    tree[tree[x].ch[r]].fa=y;tree[y].fa=x;tree[x].fa=z;
    tree[y].ch[l]=tree[x].ch[r];tree[x].ch[r]=y;
    push_up(y);push_up(x);
}
void Splay(int x)
{
    sta[++top]=x;
    for (int i=x;!is_root(i);i=tree[i].fa)  sta[++top]=tree[i].fa;
    while (top) push_down(sta[top--]);
    while (!is_root(x))
    {
        int y=tree[x].fa,z=tree[y].fa;
        if (!is_root(y))    {if ((tree[y].ch[0]==x)^(tree[z].ch[0]==y)) rot(x); else    rot(y);}
        rot(x);
    }
}
void access(int x)  {for (int i=0;x;i=x,x=tree[x].fa)   Splay(x),tree[x].ch[1]=i,push_up(x);}
void make_root(int x)   {access(x);Splay(x);tree[x].rev^=1;}
void link(int x,int y)  {make_root(x);tree[x].fa=y;}
void cut(int x,int y)   {make_root(x);access(y);Splay(y);tree[y].ch[0]=tree[x].fa=0;push_up(y);}
void split(int x,int y) {make_root(x);access(y);Splay(y);}
void add(int x,int delta)   {make_root(x);tree[x].val+=delta;tree[x].val=max(tree[x].val,0);push_up(x);}
void remove(int x)  {make_root(x);tree[x].val=0;push_up(x);}
struct Query
{
    int id,opt,x,c,w;
    bool operator <(const Query& a)const    {return c==a.c?(id==a.id?x<a.x:id<a.id):c<a.c;}
    bool operator ==(const Query& a)const   {return opt==0&&a.opt==0&&id>=MAXINT&&a.id==id&&x==a.x;}
}s[MAXN<<3];//0 modify 1 query_sum 2 query_max
int cnt,Top;
LL ans[MAXN<<3];
int main()
{
    in(n);in(m);int u,v;char ch[4];
    for (int i=1;i<=n;i++)
    {
        in(noww[i]);in(nowc[i]);
        s[++cnt]=(Query){0,0,i,nowc[i],noww[i]};s[++cnt]=(Query){MAXINT,0,i,nowc[i],-noww[i]};
    }
    for (int i=1;i<n;i++)   in(u),in(v),link(u,v);
    for (int i=1;i<=m;i++)
    {
        scanf("%s",ch);in(u);in(v);
        if (ch[0]=='C'&&ch[1]=='C')
            s[++cnt]=(Query){++Top,0,u,nowc[u],-noww[u]},
            s[++cnt]=(Query){++Top,0,u,(nowc[u]=v),noww[u]},
            s[++cnt]=(Query){MAXINT,0,u,nowc[u],-noww[u]};
        if (ch[0]=='C'&&ch[1]=='W') s[++cnt]=(Query){++Top,0,u,nowc[u],v-noww[u]},noww[u]=v;
        if (ch[0]=='Q'&&ch[1]=='S') s[++cnt]=(Query){++Top,1,u,nowc[u],v};
        if (ch[0]=='Q'&&ch[1]=='M') s[++cnt]=(Query){++Top,2,u,nowc[u],v};
    }
    sort(s+1,s+cnt+1);cnt=unique(s+1,s+cnt+1)-s-1;
    for (int i=1;i<=cnt;i++)
        if (!s[i].opt)
        {
            if (s[i].id<0x3f3f3f3f) add(s[i].x,s[i].w); else    remove(s[i].x);
        }
        else    split(s[i].x,s[i].w),ans[s[i].id]=s[i].opt==1?tree[s[i].w].sum:tree[s[i].w].maxn;
    for (int i=1;i<=Top;i++)    if (ans[i]) printf("%lld\n",ans[i]);
}

posted on 2017-07-17 14:35  wgwyanfs  阅读(252)  评论(0编辑  收藏  举报

导航