WISCO信息组NOIP模拟赛-部落冲突
传送门
首先肯定考虑树剖,这里没有要求区间加,所以可以用树状数组维护,不会卡常的
这里是边权,可以转化为点权:让每条边连接的较深的节点的点权等于边权即可,然后计算的时候减去lca
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define MAXN 300005
#define LOG 20
using namespace std;
int read(){
int x=;char ch=getchar();
while(ch<''||ch>''){ch=getchar();}
while(ch>=''&&ch<=''){x=x*+(ch^);ch=getchar();}
return x;
}
int n,T;
int a[MAXN],dat[MAXN];
int dep[MAXN],size[MAXN],gs[MAXN],fa[][MAXN];
int top[MAXN],tree[MAXN],pre[MAXN],tot;
int first[MAXN],nxt[MAXN<<],to[MAXN<<],cnt;
int id[MAXN],tmp; void add(int x,int y){
nxt[++cnt]=first[x];first[x]=cnt;to[cnt]=y;
nxt[++cnt]=first[y];first[y]=cnt;to[cnt]=x;
}
int lca(int x,int y){
if(dep[x]<dep[y]){
swap(x,y);
}
for(int k=dep[x]-dep[y],p=;k;k>>=,p++){
if(k&){
x=fa[p][x];
}
}
if(x==y){
return x;
}
for(int k=LOG-;k>=;k--){
if(fa[k][x]!=fa[k][y]){
x=fa[k][x],y=fa[k][y];
}
}
return fa[][x];
}
void change(int k,int x){
while(k<=n){
dat[k]+=x;
k+=(k&-k);
}
}
int query(int k){
int ret=;
while(k>=){
ret+=dat[k];
k-=(k&-k);
}
return ret;
}
void dfs1(int x){
size[x]=;
for(int e=first[x];e;e=nxt[e]){
int y=to[e];
if(y==fa[][x]){
continue;
}
fa[][y]=x;
dep[y]=dep[x]+;
dfs1(y);
size[x]+=size[y];
if(size[y]>size[gs[x]]){
gs[x]=y;
}
}
}
void dfs2(int x,int t){
top[x]=t;
tree[x]=(++tot);
pre[tot]=x;
if(!gs[x]){
return;
}
dfs2(gs[x],t);
for(int e=first[x];e;e=nxt[e]){
int y=to[e];
if(y==fa[][x]||y==gs[x]){
continue;
}
dfs2(y,y);
}
}
int ask(int x,int y){
int f1=top[x],f2=top[y];
if(dep[f1]<dep[f2]){
swap(x,y),swap(f1,f2);
}
int ret=-a[lca(x,y)];
while(f1!=f2){
ret+=query(tree[x])-query(tree[f1]-);
x=fa[][f1]; f1=top[x];
if(dep[f1]<dep[f2]){
swap(x,y),swap(f1,f2);
}
}
if(dep[x]<dep[y]){
swap(x,y);
}
ret+=query(tree[x])-query(tree[y]-);
return (ret<=);
}
void init(){
n=read();T=read();
for(int i=;i<n;i++){
int x=read(),y=read();
add(x,y);
}
dfs1();
dfs2(,);
for(int k=;k<LOG;k++){
for(int i=;i<=n;i++){
fa[k][i]=fa[k-][fa[k-][i]];
}
}
}
void solve(){
char ch[];
while(T--){
scanf("%s",ch);
int x=read();
if('Q'==ch[]){
int y=read();
if(ask(x,y)){
printf("Yes\n");
}
else{
printf("No\n");
}
}
else if('C'==ch[]){
int y=read();
if(dep[x]<dep[y]){
swap(x,y);
}
change(tree[x],);
a[x]++;
id[++tmp]=x;
}
else{
x=id[x];
change(tree[x],-);
a[x]--;
}
}
}
int main()
{
init();
solve();
return ;
}
树剖AC
也可以是树上差分,用树状数组+dfs序,本质上是差不多的
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define MAXN 300005
#define LOG 20
using namespace std;
int read(){
int x=;char ch=getchar();
while(ch<''||ch>''){ch=getchar();}
while(ch>=''&&ch<=''){x=x*+(ch^);ch=getchar();}
return x;
}
int n,T;
int first[MAXN],nxt[MAXN<<],to[MAXN<<],cnt;
int pin[MAXN],pout[MAXN],tot;
int dat[MAXN<<];
int fa[LOG][MAXN],dep[MAXN];
int id[MAXN],tmp;
void change(int k,int x){
while(k<=(n<<)){
dat[k]+=x;
k+=(k&-k);
}
}
int query(int k){
int ret=;
while(k>=){
ret+=dat[k];
k-=(k&-k);
}
return ret;
}
void add(int x,int y){
nxt[++cnt]=first[x];first[x]=cnt;to[cnt]=y;
nxt[++cnt]=first[y];first[y]=cnt;to[cnt]=x;
}
int lca(int x,int y){
if(dep[x]<dep[y]){
swap(x,y);
}
for(int k=dep[x]-dep[y],p=;k;k>>=,p++){
if(k&){
x=fa[p][x];
}
}
if(x==y){
return x;
}
for(int k=LOG-;k>=;k--){
if(fa[k][x]!=fa[k][y]){
x=fa[k][x],y=fa[k][y];
}
}
return fa[][x];
}
void dfs(int x){
pin[x]=(++tot);
for(int e=first[x];e;e=nxt[e]){
int y=to[e];
if(y==fa[][x]){
continue;
}
dep[y]=dep[x]+;
fa[][y]=x;
dfs(y);
}
pout[x]=(++tot);
}
int ask(int x,int y){
int t=query(pin[x])+query(pin[y])-*query(pin[lca(x,y)]);
return (t<=);
}
void init(){
n=read(); T=read();
for(int i=;i<n;i++){
int x=read(),y=read();
add(x,y);
}
dfs();
for(int k=;k<LOG;k++){
for(int i=;i<=n;i++){
fa[k][i]=fa[k-][fa[k-][i]];
}
}
}
void solve(){
char ch[]={};
while(T--){
scanf("%s",ch);
int x=read();
if('Q'==ch[]){
int y=read();
if(ask(x,y)){
printf("Yes\n");
}
else{
printf("No\n");
}
}
else if('C'==ch[]){
int y=read();
if(dep[x]<dep[y]){
swap(x,y);
}
change(pin[x],);
change(pout[x]+,-);
id[++tmp]=x;
}
else{
x=id[x];
change(pin[x],-);
change(pout[x]+,);
}
}
}
int main()
{
// freopen("data.in","r",stdin);
init();
solve();
return ;
}
树上差分AC
WISCO信息组NOIP模拟赛-部落冲突的更多相关文章
- WISCO信息组NOIP模拟赛-数据结构
传送门 差分+暴力 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstri ...
- NOIP模拟赛20161022
NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...
- contesthunter暑假NOIP模拟赛第一场题解
contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...
- NOIP模拟赛 by hzwer
2015年10月04日NOIP模拟赛 by hzwer (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...
- 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程
数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...
- 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...
- 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...
- 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...
- CH Round #58 - OrzCC杯noip模拟赛day2
A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...
随机推荐
- 2017北京国庆刷题Day1 afternoon
期望得分:100+100+100=300 实际得分:100+100+100=300 T1 一道图论好题(graph) Time Limit:1000ms Memory Limit:128MB 题目 ...
- HTML标签小记文本类标签
文本类标签: <input type="text" name="" value="">文本框 type(方式,方法)name文 ...
- SQL常用语句,随时用随时更新
更多详细说明文档查询 http://www.postgres.cn/docs/9.5/infoschema-columns.html 1.1通过表名查询表的属性 SELECT * FROM sys.s ...
- React 深入系列1:React 中的元素、组件、实例和节点
文:徐超,<React进阶之路>作者 授权发布,转载请注明作者及出处 React 深入系列,深入讲解了React中的重点概念.特性和模式等,旨在帮助大家加深对React的理解,以及在项目中 ...
- 我所知道的window.location
多说无益 直接上干货 假如一个地址为 http://127.0.0.1:5000/index.html?id=4 window.location.href -- 完整路径 -- http://127 ...
- python __str__ 和__repr__方法
看下面的例子就明白了 class Test(object): def __init__(self, value='hello, world!'): self.data = value >> ...
- AutoCAD中的扩展字典及扩展记录(C#)
在学习CAD扩展记录的过程中,遇到了一些问题,也积累了一些经验,现在给大家分享一些我的学习心得.在学习扩展字典之前需要读者了解cad的组码,也就是DxfCode.感兴趣的也可以了解一下扩展数据的相关内 ...
- 你考虑清楚了吗就决定用 Bootstrap ?
近年来,在前端项目中, Bootstrap 已经成为了一个非常受欢迎的工具. Bootstrap 的确有很多优点,然而,如果你的团队中恰好有一个专职的前端工程师.那我推荐你们不要使用 Bootstra ...
- 记java应用linux服务单个CPU使用率100%分析
之前在做项目的过程中,项目完成后在linux服务器上做性能测试,当服务跑起来的时候发现cpu使用率很奇怪,java应用把单个cpu跑满了,其他cpu利用率0%. 刚开始遇到这问题的时候我第一时间反应使 ...
- Python之线程
操作系统线程理论 线程概念的引入背景 进程 之前我们已经了解了操作系统中进程的概念,程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程.程序和进程的区别 ...