bzoj 4999: This Problem Is Too Simple!
Description
Input
Output
Sample Input
10 20 30 40 50
1 2
1 3
3 4
3 5
Q 2 3 40
C 1 40
Q 2 3 40
Q 4 5 30
C 3 10
Q 4 5 30
Sample Output
1
1
0
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
const int M=1e6+,mod=;
int read(){
int ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
return ans*f;
}
int n,m,k,v[M],l[M],r[M],pos[M];
int first[M],cnt,ans[M],mark[M];
struct node{int to,next;}e[*M];
void ins(int a,int b){e[++cnt]=(node){b,first[a]}; first[a]=cnt;}
void insert(int a,int b){ins(a,b); ins(b,a);}
int dep[M],f[M][],sum;
void dfs(int x){
l[x]=pos[x]=++sum;
for(int i=;(<<i)<=dep[x];i++) f[x][i]=f[f[x][i-]][i-];
for(int i=first[x];i;i=e[i].next){
int now=e[i].to;
if(!dep[now]){
dep[now]=dep[x]+;
f[now][]=x;
dfs(now);
}
}r[x]=sum;
}
int find(int x,int y){
if(dep[x]<dep[y]) std::swap(x,y);
int d=dep[x]-dep[y];
for(int i=;(<<i)<=d;i++) if(<<i&d) x=f[x][i];
if(x==y) return x;
for(int i=;i>=;i--)
if(f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
return f[x][];
}
struct P{int s,x,T;};
std::vector<P>e1[M];
struct Q{int l,r,T;};
std::vector<Q>e2[M];
int star[mod],cnth;
struct H{int to,next;}hash[M];
int get(int x){
int w=x%mod;
for(int i=star[w];i;i=hash[i].next) if(hash[i].to==x) return i;
cnth++; hash[cnth].to=x; hash[cnth].next=star[w]; star[w]=cnth;
return cnth;
}
char c[];
int s[M],now[M];
int lowbit(int x){return x&-x;}
void add(int x,int ss){
while(x<=n){
if(now[x]!=k) now[x]=k,s[x]=;
s[x]+=ss;
x+=lowbit(x);
}
}
int query(int x){
int ans=;
while(x){
if(now[x]==k) ans+=s[x];
x-=lowbit(x);
}
return ans;
}
int main(){
n=read(); m=read();
for(int i=;i<=n;i++){
v[i]=get(read());
e1[v[i]].push_back((P){,i,});
}
int x,y;
for(int i=;i<n;i++) x=read(),y=read(),insert(x,y);
dep[]=; dfs();
for(int i=;i<=m;i++){
scanf("%s",c);
if(c[]=='C'){
x=read(); k=get(read());
if(v[x]==k) continue;
e1[k].push_back((P){,x,i});
e1[v[x]].push_back((P){-,x,i});
v[x]=k;
}
else{
mark[i]=;
x=read(); y=read(); k=get(read());
e2[k].push_back((Q){x,y,i});
}
}
for(k=;k<=cnth;k++){
int now=;
P* h1=e1[k].data();
Q* h2=e2[k].data();
for(int i=;i<e2[k].size();i++){
while(now<e1[k].size()&&h1[now].T<=h2[i].T){
add(l[h1[now].x],h1[now].s);
add(r[h1[now].x]+,-h1[now].s);
now++;
}
int id=h2[i].T;
ans[id]+=query(pos[h2[i].l]); ans[id]+=query(pos[h2[i].r]);
int lca=find(h2[i].l,h2[i].r),fa=f[lca][];
ans[id]-=query(pos[lca]); ans[id]-=query(pos[fa]);
}
}
for(int i=;i<=m;i++)if(mark[i]) printf("%d\n",ans[i]);
return ;
}
bzoj 4999: This Problem Is Too Simple!的更多相关文章
- BZOJ 4999: This Problem Is Too Simple! DFS序+LCA+树状数组+离线
Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) , ...
- [BZOJ 4999]This Problem Is Too Simple!
[BZOJ 4999]This Problem Is Too Simple! 题目 给您一颗树,每个节点有个初始值. 现在支持以下两种操作: 1. C i x(0<=x<2^31) 表示将 ...
- 4999: This Problem Is Too Simple!
Description 给您一颗树,每个节点有个初始值. 现在支持以下两种操作: C i x(0<=x<2^31) 表示将i节点的值改为x. Q i j x(0<=x<2^31 ...
- 【BZOJ4999】This Problem Is Too Simple!(线段树)
[BZOJ4999]This Problem Is Too Simple!(线段树) 题面 BZOJ 题解 对于每个值,维护一棵线段树就好啦 动态开点,否则空间开不下 剩下的就是很简单的问题啦 当然了 ...
- 【BZOJ4999】This Problem Is Too Simple! 离线+树状数组+LCA
[BZOJ4999]This Problem Is Too Simple! Description 给您一颗树,每个节点有个初始值. 现在支持以下两种操作: 1. C i x(0<=x<2 ...
- bzoj4999 This Problem Is Too Simple!
Description 给您一颗树,每个节点有个初始值. 现在支持以下两种操作: 1. C i x(0<=x<2^31) 表示将i节点的值改为x. 2. Q i j x(0<=x&l ...
- 2019.03.09 bzoj4999: This Problem Is Too Simple!(树链剖分+线段树动态开点)
传送门 题意:给一颗树,每个节点有个初始值,要求支持将i节点的值改为x或询问i节点到j节点的路径上有多少个值为x的节点. 思路: 考虑对每种颜色动态开点,然后用树剖+线段树维护就完了. 代码: #in ...
- BZOJ4999: This Problem Is Too Simple!树链剖分+动态开点线段树
题目大意:将某个节点的颜色变为x,查询i,j路径上多少个颜色为x的点... 其实最开始一看就是主席树+树状数组+DFS序...但是过不去...MLE+TLE BY FCWWW 其实树剖裸的一批...只 ...
- BZOJ4999:This Problem Is Too Simple!(DFS序&树上差分&线段树动态开点:区间修改单点查询)
Description 给您一颗树,每个节点有个初始值. 现在支持以下两种操作: 1. C i x(0<=x<2^31) 表示将i节点的值改为x. 2. Q i j x(0<=x&l ...
随机推荐
- php杂记——2(数组的使用)
1.建立升序数组:range(); $numarr1 = range(1,4); //(1,2,3,4) $numarr2 = range(1,10,2); //(1,3,5,7,9) $letter ...
- json与python解析
1.json.dumps 将 Python 对象编码成 JSON 字符串 json.loads 将已编码的 JSON 字符串解码为 Python 对象 2.json.dump() ...
- Jmeter从文件中读取参数值
1. 通过函数助手,从本地文件中取值选项->函数助手对话框->选择__CSVRead函数->调用参数其中,函数助手对话框中,第一栏填写本地文件所在地址,第二栏写需要入参的值,有点类似 ...
- react实现换肤功能
一.目标 提供几种主题色给用户选择,然后根据用户的选择改变应用的主题色: 二.实现原理 1.准备不同主题色的样式文件: 2.将用户的选择记录在本地缓存中: 3.每次进入应用时,读取缓存 ...
- Mysql性能优化四:分库,分区,分表,你们如何做?
分库分区分表概念 分区 就是把一张表的数据分成N个区块,在逻辑上看最终只是一张表,但底层是由N个物理区块组成的 分表 就是把一张数据量很大的表按一定的规则分解成N个具有独立存储空间的实体表.系统读写时 ...
- PAT——乙级1022:D进制的A+B &乙级1037:在霍格沃茨找零钱
1022 D进制的A+B (20 point(s)) 输入两个非负 10 进制整数 A 和 B (≤230−1),输出 A+B 的 D (1<D≤10)进制数. 输入格式: 输入在一行中依 ...
- 计算机概念总结5-阿里云的了解-ecs
1.ecs 1.1ecs 云服务器Elastic Compute Service(ECS)是阿里云提供的一种基础云计算服务.使用云服务器ECS就像使用水.电.煤气等资源一样便捷.高效.您无需提前采购硬 ...
- BZOJ 3779 重组病毒 LCT+线段树(维护DFS序)
原题干(由于是权限题我就直接砸出原题干了,要看题意概述的话在下面): Description 黑客们通过对已有的病毒反编译,将许多不同的病毒重组,并重新编译出了新型的重组病毒.这种病毒的繁殖和变异能力 ...
- NO3——BFS
#include <stdio.h> #include <string.h> #include <queue> using namespace std; struc ...
- winform构造函数和load事件
有些地方,有些代码写在构造函数里面运行不成功: 但是加在load事件里面运行成功: 有时候,反则反之.