原文链接http://www.cnblogs.com/zhouzhendong/p/8081514.html


题目传送门 - BZOJ3862


题意概括

  一棵树,n个点,边权为黑或者白,支持3重操作:

  1.链上颜色翻转

  2.对于一条链,把有一个点在这条链上的边全部翻转颜色

  3.询问一条链上有多少黑色。


题解

  毒瘤题。

  对于1、3都是基础操作,很简单。

  主要是2.

  2的话,只需要打区间打标记,表示那些点的连向轻儿子的边全部翻转。然后修改的时候还有一堆特判(具体看代码)

  这题数据坑。

  有a==b的情况,小心了。我被坑了一个下午。

  我是怎么找到这个数据的呢?

  if (a==b) OLE();

void OLE(){
int a[100];
for (int i=23333;;i=i*i)
printf("233");
}

  


代码

#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
using namespace std;
bool isd(char ch){return '0'<=ch&&ch<='9';}
int read(){
int x=0;
char ch=getchar();
while (!isd(ch))
ch=getchar();
while (isd(ch))
x=(x<<1)+(x<<3)+ch-48,ch=getchar();
return x;
}
const int N=100005;
struct Gragh{
int cnt,y[N*2],nxt[N*2],fst[N];
void clear(){
cnt=0;
memset(fst,0,sizeof fst);
}
void add(int a,int b){
y[++cnt]=b,nxt[cnt]=fst[a],fst[a]=cnt;
}
}g;
int T,n,q,cnp;
int size[N],fa[N],depth[N],son[N],p[N],ap[N],top[N];
void Get_Gen_Info(int rt,int pre,int d){
size[rt]=1,son[rt]=-1,fa[rt]=pre,depth[rt]=d;
for (int i=g.fst[rt];i;i=g.nxt[i])
if (g.y[i]!=pre){
int s=g.y[i];
Get_Gen_Info(s,rt,d+1);
size[rt]+=size[s];
if (son[rt]==-1||size[s]>size[son[rt]])
son[rt]=s;
}
}
void Get_Top(int rt,int tp){
top[rt]=tp;
ap[p[rt]=++cnp]=rt;
if (!~son[rt])
return;
Get_Top(son[rt],tp);
for (int i=g.fst[rt];i;i=g.nxt[i]){
int s=g.y[i];
if (s!=fa[rt]&&s!=son[rt])
Get_Top(s,s);
}
}
const int S=N*4;
struct Tree{
int sum,size,r,sr;
}t[S];
void build(int rt,int L,int R){
t[rt].size=R-L+1,t[rt].sum=t[rt].r=t[rt].sr=0;
if (L==R)
return;
int mid=(L+R)>>1,ls=rt<<1,rs=ls|1;
build(ls,L,mid);
build(rs,mid+1,R);
}
void pushup(int rt){
int ls=rt<<1,rs=ls|1;
t[rt].sum=t[ls].sum+t[rs].sum;
}
void pushdown(int rt){
int ls=rt<<1,rs=ls|1;
int &sr=t[rt].sr,&r=t[rt].r;
if (sr)
t[ls].sr^=1,t[rs].sr^=1,sr=0;
if (r){
t[ls].r^=1,t[ls].sum=t[ls].size-t[ls].sum;
t[rs].r^=1,t[rs].sum=t[rs].size-t[rs].sum;
r=0;
}
}
void update(int rt,int L,int R,int xL,int xR,int op){
if (R<xL||L>xR)
return;
if (xL<=L&&R<=xR){
if (op==1){
t[rt].sum=t[rt].size-t[rt].sum;
t[rt].r^=1;
}
else
t[rt].sr^=1;
return;
}
pushdown(rt);
int mid=(L+R)>>1,ls=rt<<1,rs=ls|1;
update(ls,L,mid,xL,xR,op);
update(rs,mid+1,R,xL,xR,op);
pushup(rt);
}
int query(int rt,int L,int R,int xL,int xR){
if (R<xL||L>xR)
return 0;
if (xL<=L&&R<=xR)
return t[rt].sum;
pushdown(rt);
int mid=(L+R)>>1,ls=rt<<1,rs=ls|1;
return query(ls,L,mid,xL,xR)+query(rs,mid+1,R,xL,xR);
}
int asksr(int rt,int L,int R,int pos){
if (L==R)
return t[rt].sr;
pushdown(rt);
int mid=(L+R)>>1,ls=rt<<1,rs=ls|1;
if (pos<=mid)
return asksr(ls,L,mid,pos);
else
return asksr(rs,mid+1,R,pos);
}
void Tupdate(int a,int b,int op){
if (a==b)
return;
int f1=top[a],f2=top[b];
while (f1!=f2){
if (depth[f1]<depth[f2])
swap(f1,f2),swap(a,b);
update(1,1,n,p[f1],p[a],op);
if (op==2){
update(1,1,n,p[f1],p[f1],1);
if (~son[a])
update(1,1,n,p[son[a]],p[son[a]],1);
}
a=fa[f1],f1=top[a];
}
if (depth[a]>depth[b])
swap(a,b);
if (op==2){
update(1,1,n,p[a],p[b],2);
update(1,1,n,p[a],p[a],1);
if (~son[b])
update(1,1,n,p[son[b]],p[son[b]],1);
}
else if (a!=b)
update(1,1,n,p[son[a]],p[b],op);
}
int Tquery(int a,int b){
int f1=top[a],f2=top[b],ans=0;
while (f1!=f2){
if (depth[f1]<depth[f2])
swap(f1,f2),swap(a,b);
if (f1!=a)
ans+=query(1,1,n,p[son[f1]],p[a]);
ans+=query(1,1,n,p[f1],p[f1])^asksr(1,1,n,p[fa[f1]]);
a=fa[f1],f1=top[a];
}
if (depth[a]>depth[b])
swap(a,b);
if (a!=b)
ans+=query(1,1,n,p[son[a]],p[b]);
return ans;
}
int main(){
T=read();
while (T--){
n=read();
g.clear();
for (int i=1,a,b;i<n;i++){
a=read(),b=read();
g.add(a,b),g.add(b,a);
}
cnp=0;
Get_Gen_Info(1,0,0);
Get_Top(1,1);
build(1,1,n);
q=read();
while (q--){
int op,a,b;
op=read(),a=read(),b=read();
if (a==b){
if (op==3)
puts("0");
if (op==2){
update(1,1,n,p[a],p[a],2);
if (~son[a])
update(1,1,n,p[son[a]],p[son[a]],1);
if (fa[a])
update(1,1,n,p[a],p[a],1);
}
continue;
}
if (op<3)
Tupdate(a,b,op);
if (op==3)
printf("%d\n",Tquery(a,b));
}
}
return 0;
}

  

BZOJ3862 Little Devil I 树链剖分的更多相关文章

  1. hdu_4897_Little Devil I(树链剖分)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4897 题意:有三种操作,1是在树上的两个节点之间的路径改变当前的颜色,2是改变树上有且只有一个端点在u ...

  2. BZOJ3862Little Devil I——树链剖分+线段树

    题目大意: 给一棵树,每条边可能是黑色或白色(起始都是白色),有三种操作: 1.将u到v路径上所有边颜色翻转(黑->白,白->黑) 2.将只有一个点在u到v路径上的边颜色翻转 3.查询u到 ...

  3. hdu 4897 Little Devil I (树链剖分+线段树)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4897 题意: 给你一棵树,一开始每条边都是白色,有三种操作: 1.将 u - v路径上的边转换颜色 ...

  4. HDU 4897 Little Devil I 树链剖分+线段树

    Little Devil I Problem Description There is an old country and the king fell in love with a devil. T ...

  5. HDU 4897 Little Devil I(树链剖分)(2014 Multi-University Training Contest 4)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4897 Problem Description There is an old country and ...

  6. 树链剖分+线段树 HDOJ 4897 Little Devil I(小恶魔)

    题目链接 题意: 给定一棵树,每条边有黑白两种颜色,初始都是白色,现在有三种操作: 1 u v:u到v路径(最短)上的边都取成相反的颜色 2 u v:u到v路径上相邻的边都取成相反的颜色(相邻即仅有一 ...

  7. HDU4897 (树链剖分+线段树)

    Problem Little Devil I (HDU4897) 题目大意 给定一棵树,每条边的颜色为黑或白,起始时均为白. 支持3种操作: 操作1:将a->b的路径中的所有边的颜色翻转. 操作 ...

  8. hdu 4897 树链剖分(重轻链)

    Little Devil I Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  9. BZOJ 3626: [LNOI2014]LCA [树链剖分 离线|主席树]

    3626: [LNOI2014]LCA Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2050  Solved: 817[Submit][Status ...

随机推荐

  1. python学习第7天

    编码的进阶 文件操作 深浅copy

  2. SSH localhost免密不成功 + 集群状态显示Configured Capacity: 0 (0 KB)

    前一天运行hadoop一切安好,今天重新运行出现BUG.下面对遇到的bug.产生原因以及解决方法进行一下简单总结记录. [bug1]用ssh localhost免密登录时提示要输入密码. 原因分析:之 ...

  3. 【原创】大数据基础之Hadoop(2)hdfs和yarn最简绿色部署

    环境:3结点集群 192.168.0.1192.168.0.2192.168.0.3 1 配置root用户服务期间免密登录 参考:https://www.cnblogs.com/barneywill/ ...

  4. 6)协程三( asyncio处理并发)

    一:使用 asyncio处理并发 介绍 asyncio 包,这个包使用事件循环驱动的协程实现并发.这是 Python 中最大也是最具雄心壮志的库之一. 二:示例 1)单任务协程处理和普通任务比较 #普 ...

  5. 技术的热门度曲线:GHC

      全球最大的 IT 咨询公司高德纳(Gartner),有一个"技术热门度曲线"模型(Gartner Hype Cycle). 该模型认为,一门技术的发展要经历五个阶段. (1)启 ...

  6. Windows&Word 常用快捷键

    Win:显示开始菜单 Win + E:打开文件管理器 Win + D:显示桌面 Win + L:锁定计算机 Win + I:打开设置 Win + M:最小化所有窗口 Alt + F4:1.用来关闭当前 ...

  7. jdbcTemplate 调用存储过程。 入参 array 返回 cursor

    注:本文来源<   jdbcTemplate 调用存储过程. 入参 array 返回 cursor   > 需求: java传入一个list object.从数据库找到相关的数据并返回. ...

  8. laravel 迁移枚举

    $table->enum('type', ['replace', 'warning'])->comment('类型');

  9. laravel 注入那点事

    public function delete(Group $groupId, Post $postId) { $postId->delete(); return response()->j ...

  10. cf1025c 思维题

    /* bwwwbwwbw wwbwwwbwb 不管从哪里断开翻转.翻转后的串再整体翻转一定是2s的子串 */ #include<bits/stdc++.h> using namespace ...