线段树动态开点+树链剖分BZOJ4999
以每个一个颜色开一颗线段树,内部以dfs序作为线段树节点,权值代表出现次数,维护线段树区间和
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
using namespace std;
const int maxn = 2e5+;
struct edge{
int next,to;
}e[maxn*];
struct node{
int l,r;
int w;
}tree[maxn*];
int head[maxn],siz[maxn],top[maxn],son[maxn];
int d[maxn],fa[maxn],id[maxn],rk[maxn],dfn[maxn],c[maxn];
int n,m,uu,vv,tot,cnt;
int rt[maxn];
map<int,int>p;
void add(int x,int y){
e[++cnt].next=head[x];
e[cnt].to=y;
head[x]=cnt;
}
void dfs1(int u,int f,int depth){
d[u]=depth;
fa[u]=f;
siz[u]=;
for (int i=head[u];i;i=e[i].next){
int v=e[i].to;
if (v==f)continue;
dfs1(v,u,depth+);
siz[u]+=siz[v];
if (siz[v]>siz[son[u]])
son[u]=v;
}
}
void dfs2(int u,int t){
top[u]=t;
dfn[u]=++cnt;
if (!son[u])
return;
dfs2(son[u],t);
for (int i=head[u];i;i=e[i].next)
{
int v=e[i].to;
if (v!=son[u] && v!=fa[u])
dfs2(v,v);
}
}
void Modify(int &x,int l,int r,int pos,int w)
{
if (!x)x=++tot; tree[x].w+=w;
if (l==r)return;
int mid=(l+r)>>;
if (pos<=mid)Modify(tree[x].l,l,mid,pos,w);
else Modify(tree[x].r,mid+,r,pos,w);
}
int query(int x,int l,int r,int L,int R){
if((!tree[x].w) || (!x))return ;
if (L<=l && r<=R){
return tree[x].w;
}
int mid=(l+r)>>;
int ans=;
if (L<=mid)ans+=query(tree[x].l,l,mid,L,R);
if (R>mid)ans+=query(tree[x].r,mid+,r,L,R);
return ans;
}
int qryRange(int x,int y,int k){
int res=;
while(top[x]!=top[y]){
if (d[top[x]]<d[top[y]])swap(x,y);
res+=query(rt[k],,n,dfn[top[x]],dfn[x]);
x=fa[top[x]];
}
if (d[y]>d[x])swap(x,y);
res+=query(rt[k],,n,dfn[y],dfn[x]);
return res;
}
int main(){
int sum=,q;
while(~scanf("%d%d",&n,&q)){
cnt=;
sum=;
for (int i=;i<=n;i++){
scanf("%d",&c[i]);
}
for (int i=;i<=n;i++){
scanf("%d%d",&uu,&vv);
add(uu,vv);
add(vv,uu);
}
cnt=;
dfs1(,,);
dfs2(,);
tot=;
for (int i=;i<=n;i++){
if (p[c[i]]==){
p[c[i]]=++sum;
}
Modify(rt[p[c[i]]],,n,dfn[i],);
}
char tmp[];
int u,w,v;
while(q--){
scanf("%s",tmp);
if (tmp[]=='C')
{
scanf("%d%d",&u,&w);
Modify(rt[p[c[u]]],,n,dfn[u],-);
if (!p[w])p[w]=++sum;
Modify(rt[p[w]],,n,dfn[u],);
c[u]=w;
}else {
scanf("%d%d%d",&u,&v,&w);
if (!p[w])printf("0\n");
else printf("%d\n",qryRange(u,v,p[w]));
}
}
}
return ;
}
线段树动态开点+树链剖分BZOJ4999的更多相关文章
- 洛谷P3313 [SDOI2014]旅行 题解 树链剖分+线段树动态开点
题目链接:https://www.luogu.org/problem/P3313 这道题目就是树链剖分+线段树动态开点. 然后做这道题目之前我们先来看一道不考虑树链剖分之后完全相同的线段树动态开点的题 ...
- BZOJ_4636_蒟蒻的数列_线段树+动态开点
BZOJ_4636_蒟蒻的数列_线段树+动态开点 Description 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个数列,初始值均为0,他进行N次操作,每次将 ...
- hdu6183 Color it 线段树动态开点+查询减枝
题目传送门 题目大意: 有多次操作.操作0是清空二维平面的点,操作1是往二维平面(x,y)上放一个颜色为c的点,操作2是查询一个贴着y轴的矩形内有几种颜色的点,操作3退出程序. 思路: 由于查询的矩形 ...
- P3939 数颜色 线段树动态开点
P3939 数颜色 线段树动态开点 luogu P3939 水.直接对每种颜色开个权值线段树即可,注意动态开点. #include <cstdio> #include <algori ...
- HDU - 6183 暴力,线段树动态开点,cdq分治
B - Color itHDU - 6183 题目大意:有三种操作,0是清空所有点,1是给点(x,y)涂上颜色c,2是查询满足1<=a<=x,y1<=b<=y2的(a,b)点一 ...
- codedecision P1113 同颜色询问 题解 线段树动态开点
题目描述:https://www.cnblogs.com/problems/p/11789930.html 题目链接:http://codedecision.com/problem/1113 这道题目 ...
- (持续更新)虚树,KD-Tree,长链剖分,后缀数组,后缀自动机
真的就是讲课两天,吸收一个月呢! \(1.\)虚树 \(2.\)KD-Tree \(3.\)长链剖分 \(4.\)后缀数组 后缀数组 \(5.\)后缀自动机 后缀自动机
- BZOJ 3531 SDOI2014 旅行 树链剖分+线段树动态开点
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3531 题意概述: 给出一棵N个点的树,树上的每个结点有一个颜色和权值,支持以下四种操作: ...
- Gym - 101848C Object-Oriented Programming (树链剖分+线段树+动态开点)
C. Object-Oriented Programming time limit per test 3.0 s memory limit per test 1024 MB input standar ...
随机推荐
- 设置mysql二进制日志过期时间
((none)) > show variables like 'expire_logs_days'; +------------------+-------+ | Variable_name | ...
- Ubuntu 18.04 美化
Ubuntu 18.04 美化 sudo apt install gnome-tweak-tool sudo apt install gnome-shell-extensions sudo apt i ...
- Django项目:CRM(客户关系管理系统)--27--19PerfectCRM实现King_admin数据修改
登陆密码设置参考 http://www.cnblogs.com/ujq3/p/8553784.html {#table_data_list.html#} {## ————————08PerfectCR ...
- return语句必须要注意的地方
先看下面程序: function foo1() { return { /*返回对象{}他留有一个大括号跟return在同一行*/ bar: "hello" }; } functio ...
- didFailWithError: Error Domain=kCLErrorDomain Code=0 “The operation couldn’t be completed. (kCLError
This error also occurs if you have Scheme/Edit Scheme/Options/Allow Location Simulation checked but ...
- Java review-basic6
1. Weak references: In computer programming, a weak reference is a reference that does not protect t ...
- jnhs-SpringMVC的controller向jsp传递数据的五种方式
参考此文http://blog.sina.com.cn/s/blog_6d3c1ec601014h1h.html 1 使用ModelAndVoew 引入:org.springframework.web ...
- ifconfig命令为centos linux系统配置临时的局域名IP、网关以及子网掩码
ifconfig eth0 192.168.1.25 netmask 255.255.255.0 broadcast 192.168.1.1 up netmask:子网掩码broadcast:默认网关
- PHPStorm 批量选择,多光标同时编辑相同的内容
一直按Alt+J
- 荷畔微风 - 在函数计算FunctionCompute中使用WebAssembly
WebAssembly 是一种新的W3C规范,无需插件可以在所有现代浏览器中实现近乎原生代码的性能.同时由于 WebAssembly 运行在轻量级的沙箱虚拟机上,在安全.可移植性上比原生进程更加具备优 ...