题目描述

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,并记下了途中留宿过的城市的评级最大值。

由于年代久远,旅行者记下的数字已经遗失了,但记录开始之前每座城市的信仰与评级,还有事件记录本身是完好的。请根据这些信息,还原旅行者记下的数字。 为了方便,我们认为事件之间的间隔足够长,以致在任意一次旅行中,所有城市的评级和信仰保持不变。

输入输出格式

输入格式:

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

输出格式:

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

输入输出样例

输入样例#1:

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
输出样例#1:

8
9
11
3

说明

N,Q < =10^5 , C < =10^5

数据保证对所有QS和QM事件,起点和终点城市的信仰相同;在任意时

刻,城市的评级总是不大于10^4的正整数,且宗教值不大于C。

思路:

  树剖+多棵线段树

来,上代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> #define LL long long
#define maxn 100005 using namespace std; struct EdgeType {
LL to,next;
};
struct EdgeType edge[maxn<<]; struct TreeNodeType {
LL l,r,dis,ma;
};
struct TreeNodeType tree[maxn*]; LL if_z,n,dis[maxn],god[maxn],cnt,q;
LL head[maxn],deep[maxn],f[maxn],size[maxn];
LL belong[maxn],flag[maxn],root[maxn]; char Cget; inline void read_int(LL &now)
{
now=,if_z=,Cget=getchar();
while(Cget>''||Cget<'')
{
if(Cget=='-') if_z=-;
Cget=getchar();
}
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
now*=if_z;
} inline void edge_add(LL from,LL to)
{
cnt++;
edge[cnt].to=to;
edge[cnt].next=head[from];
head[from]=cnt;
} void search_1(LL now,LL fa)
{
LL pos=cnt++;
deep[now]=deep[fa]+,f[now]=fa;
for(LL i=head[now];i;i=edge[i].next)
{
if(edge[i].to==fa) continue;
search_1(edge[i].to,now);
}
size[now]=cnt-pos;
} void search_2(LL now,LL chain)
{
belong[now]=chain,flag[now]=++cnt;
LL pos=;
for(LL i=head[now];i;i=edge[i].next)
{
if(edge[i].to==f[now]) continue;
if(size[edge[i].to]>size[pos]) pos=edge[i].to;
}
if(pos==) return ;
search_2(pos,chain);
for(LL i=head[now];i;i=edge[i].next)
{
if(edge[i].to==f[now]||edge[i].to==pos) continue;
search_2(edge[i].to,edge[i].to);
}
} inline void tree_up(LL now)
{
tree[now].dis=tree[tree[now].l].dis+tree[tree[now].r].dis;
tree[now].ma=max(tree[tree[now].l].ma,tree[tree[now].r].ma);
} void tree_do(LL &now,LL to,LL x,LL l,LL r)
{
if(now==) now=++cnt;
if(l==r)
{
tree[now].ma=x;
tree[now].dis=x;
return ;
}
LL mid=(l+r)>>;
if(to<=mid) tree_do(tree[now].l,to,x,l,mid);
else tree_do(tree[now].r,to,x,mid+,r);
tree_up(now);
} LL tree_query(LL now,LL tol,LL tor,LL l,LL r,LL type)
{
if(tol==l&&tor==r)
{
if(type==) return tree[now].dis;
else return tree[now].ma;
}
LL mid=(l+r)>>;
if(tol>mid) return tree_query(tree[now].r,tol,tor,mid+,r,type);
else if(tor<=mid) return tree_query(tree[now].l,tol,tor,l,mid,type);
else
{
if(type==) return tree_query(tree[now].l,tol,mid,l,mid,type)+tree_query(tree[now].r,mid+,tor,mid+,r,type);
else return max(tree_query(tree[now].l,tol,mid,l,mid,type),tree_query(tree[now].r,mid+,tor,mid+,r,type));
}
} inline LL solve_query(LL x,LL y,LL g,LL type)
{
LL pos=;
while(belong[x]!=belong[y])
{
if(deep[belong[x]]<deep[belong[y]]) swap(x,y);
if(type==) pos+=tree_query(root[g],flag[belong[x]],flag[x],,n,);
else pos=max(pos,tree_query(root[g],flag[belong[x]],flag[x],,n,));
x=f[belong[x]];
}
if(deep[x]>deep[y]) swap(x,y);
if(type==) pos+=tree_query(root[g],flag[x],flag[y],,n,);
else pos=max(pos,tree_query(root[g],flag[x],flag[y],,n,));
return pos;
} int main()
{
read_int(n),read_int(q);
for(LL i=;i<=n;i++) read_int(dis[i]),read_int(god[i]);
LL from,to;
for(LL i=;i<n;i++)
{
read_int(from),read_int(to);
edge_add(from,to);
edge_add(to,from);
}
cnt=,search_1(,);
cnt=,search_2(,);
cnt=;
for(LL i=;i<=n;i++)
{
tree_do(root[god[i]],flag[i],dis[i],,n);
}
char ch[];
for(LL i=;i<=q;i++)
{
cin>>ch;read_int(from),read_int(to);
if(ch[]=='Q')
{
if(ch[]=='S')
{
cout<<solve_query(from,to,god[from],);
putchar('\n');
}
else
{
cout<<solve_query(from,to,god[from],);
putchar('\n');
}
}
else
{
if(ch[]=='C')
{
tree_do(root[god[from]],flag[from],,,n);
god[from]=to;
tree_do(root[god[from]],flag[from],dis[from],,n);
}
else
{
tree_do(root[god[from]],flag[from],to,,n);
dis[from]=to;
}
}
}
return ;
}

最新文章

  1. 前端面霸系列(1):doctype 、Quirks Mode &amp; Standards Mode 、document.compatMode
  2. 可控制导航下拉方向的jQuery下拉菜单代码
  3. install python module
  4. 不用Unity库,利用.NET动态代理自己实现AOP
  5. CSS预处理器之SASS用法指南
  6. iOS 后台播放音乐
  7. activemq java版本要求
  8. Android4.4KitKat支持u盘功能
  9. static的加载先后顺序
  10. gitbook 入门教程之快速体验
  11. npm 安装包失败 --- 清除npm缓存
  12. 20165232 week1 kali安装
  13. LeetCode第十题-正则表达式匹配
  14. python脚本发送邮件
  15. NPOI导入excel文件为DataTable,使用SqlBulkCopy添加到数据库表
  16. Django 浏览器打开警告Not Found: /favicon.ico (转)
  17. 组织安全性SQL
  18. pycharm中配置pep8
  19. 【FCS NOI2018】福建省冬摸鱼笔记 day4
  20. Reusing dialogs with a dialog pool--一个sql server service broker例子

热门文章

  1. python爬虫基础10-selenium大全4/8-Webelement
  2. day14-推导式和生成器表达式
  3. Python之路--序列化
  4. cf 1016C
  5. 给vagrant中的precise64升级VBoxGuestAdditions
  6. MMM的一周计划 准备公告
  7. 反转单词顺序 VS 左旋转字符串
  8. Hive学习笔记(四)-- hive的桶表
  9. loj2256 「SNOI2017」英雄联盟
  10. Numpy ndarray 的高级索引存在 &quot;bug&quot; ?