博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 4897 树链剖分(重轻链)
阅读量:6689 次
发布时间:2019-06-25

本文共 6281 字,大约阅读时间需要 20 分钟。

Little Devil I

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 894    Accepted Submission(s): 296

Problem Description
There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and can’t refuse any request from the devil. Also, this devil is looking like a very cute Loli.
The devil likes to make thing in chaos. This kingdom’s road system is like simply a tree(connected graph without cycle). A road has a color of black or white. The devil often wants to make some change of this system.
In details, we call a path on the tree from a to b consists of vertices lie on the shortest simple path between a and b. And we say an edge is on the path if both its two endpoints is in the path, and an edge is adjacent to the path if exactly one endpoint of it is in the path.
Sometimes the devil will ask you to reverse every edge’s color on a path or adjacent to a path.
The king’s daughter, WJMZBMR, is also a cute loli, she is surprised by her father’s lolicon-like behavior. As she is concerned about the road-system’s status, sometimes she will ask you to tell there is how many black edge on a path.
Initially, every edges is white.
 

 

Input
The first line contains an integer T, denoting the number of the test cases.
For each test case, the first line contains an integer n, which is the size of the tree. The vertices be indexed from 1.
On the next n-1 lines, each line contains two integers a,b, denoting there is an edge between a and b. 
The next line contains an integer Q, denoting the number of the operations.
On the next Q lines, each line contains three integers t,a,b. t=1 means we reverse every edge’s color on path a to b. t=2 means we reverse every edge’s color adjacent to path a to b. t=3 means we query about the number of black edge on path a to b.
T<=5.
n,Q<=10^5.
Please use scanf,printf instead of cin,cout,because of huge input.
 

 

Output
For each t=3 operation, output the answer in one line.
 

 

Sample Input
1 10 2 1 3 1 4 1 5 1 6 5 7 4 8 3 9 5 10 6 10 2 1 6 1 3 8 3 8 10 2 3 4 2 10 8 2 4 10 1 7 6 2 7 3 2 1 4 2 10 10
 

 

Sample Output
3

 

/*hdu 4897 树链剖分(重轻链)problem:给你一棵树,初始每条边为白色,然后是三种操作1.将u->v链上面的所有边的颜色翻转 (例:white -> black)  这个在线段树上很好处理,用个翻转标记,然后记录数量即可2.将u->v链上面所有邻接的边翻转(边上只有一个点在链上面)3.询问u->v上面有多少黑色的边solve:对于1,3操作树链剖分很好解决。但是在操作2上面就GG了.所以去参考了很多博客 - -,很久才明白大致思路就操作2而言,主要可以看成在一条重链上面的 和 跨越了很多重轻链的那种.主要是轻链两端连接的是重链,所以在操作2的时候可以考虑直接在每个点上面打标记(除了有的叶子节点,重链基本上覆盖了所有的点).所以轻链的颜色就是:  左端点rev2^右端点rev2^边的颜色(边的颜色线段树很好维护的)如果重链分成很多条边来用也可以实现,但是无疑到达lca的效率为很低,所有需要考虑其他方法然后就是维护重链上面的颜色,如果u,v在一条重链的中间部分,打标记可以维护对轻链的影响。所以只需要考虑对两端重链的影响,于是把与两端相邻的边用操作1翻转就好了.在操作2下一条重链最多只需要更新左右两个端点,但是却有很多条轻链。 所以重链可以直接更新,轻链则需要标记来维护了。因为没有判断,有时线段树会出现l>r导致RE了很久- -hhh-2016-08-18 21:18:55*/#pragma comment(linker,"/STACK:124000000,124000000")#include 
#include
#include
#include
#include
#include
#include
#define lson i<<1#define rson i<<1|1#define ll long long#define clr(a,b) memset(a,b,sizeof(a))#define key_val ch[ch[root][1]][0]using namespace std;const int maxn = 200100;const int inf = 0x3f3f3f3f;int head[maxn],tot,pos,son[maxn];int top[maxn],fp[maxn],fa[maxn],dep[maxn],num[maxn],p[maxn];int n;struct Edge{ int to,next;} edge[maxn<<2];void ini(){ tot = 0,pos = 1; clr(head,-1),clr(son,-1);}void add_edge(int u,int v){ edge[tot].to = v,edge[tot].next = head[u],head[u] = tot++;}void dfs1(int u,int pre,int d){// cout << u << " " <
<<" " <
<
num[son[u]]) son[u] = v; } }}void getpos(int u,int sp){ top[u] = sp; p[u] = pos++; fp[p[u]] = u; if(son[u] == -1)return ; getpos(son[u],sp); for(int i = head[u]; ~i ; i = edge[i].next) { int v = edge[i].to; if(v != son[u] && v != fa[u]) getpos(v,v); }}struct node{ int l,r,mid; int rev1,rev2; int num;} tree[maxn << 2];void push_up(int i){ tree[i].num = tree[lson].num + tree[rson].num;}void build(int i,int l,int r){ tree[i].l = l,tree[i].r = r; tree[i].mid=(l+r) >>1; tree[i].rev1 = tree[i].rev2 = 0; tree[i].num = 0; if(l == r) {// cout << fp[l] <<" " <
<
r) return ; if(tree[i].l >= l && tree[i].r <= r) { if(flag == 1) { tree[i].num = tree[i].r-tree[i].l+1-tree[i].num; tree[i].rev1 ^= 1; } else tree[i].rev2 ^= 1; return ; } push_down(i); int mid = tree[i].mid; if(r <= mid) update_area(lson,l,r,flag); else if(l > mid) update_area(rson,l,r,flag); else { update_area(lson,l,mid,flag); update_area(rson,mid+1,r,flag); } push_up(i);}int query(int i,int l,int r,int flag){ if(l > r) return 0; if(tree[i].l >= l && tree[i].r <= r) { if(flag == 1) return tree[i].num; else return tree[i].rev2; } push_down(i); int mid = tree[i].mid; if(r <= mid) return query(lson,l,r,flag); else if(l > mid) return query(rson,l,r,flag); else return query(lson,l,mid,flag)+query(rson,mid+1,r,flag); push_up(i);}void update_rev1(int u,int v){ int f1 = top[u],f2 = top[v]; while(f1 != f2) { if(dep[f1] < dep[f2]) { swap(f1,f2),swap(u,v); } update_area(1,p[f1],p[u],1); u = fa[f1],f1 = top[u]; } if(dep[u] > dep[v]) swap(u,v); update_area(1,p[son[u]],p[v],1);}void update_rev2(int u,int v){ int f1 = top[u],f2 = top[v];// cout << u << " " <
<
dep[v]) swap(u,v); update_area(1,p[u],p[v],2); int par = fa[u];// cout <
<<" "<< son[v] <
0) update_area(1,p[u],p[u],1); if(son[v] != -1) update_area(1,p[son[v]],p[son[v]],1);}int Find(int u,int v){// cout <<"*********************************************************"<
dep[v]) swap(u,v);// cout << query(1,p[u]+1,p[v],1) <

  

转载于:https://www.cnblogs.com/Przz/p/5792194.html

你可能感兴趣的文章
Beego自动化文档(最新版)
查看>>
自动备份MySQL数据库并发送邮件的SHELL脚本
查看>>
GenericObjectPool 避免泄漏
查看>>
vue2.0源码解读之选项合并策略 optionMergeStrategies
查看>>
实现iOS图片等资源文件的热更新化(三):动态的资源文件夹
查看>>
关于javascrip ==(等号) 和===(恒等)判断
查看>>
PHP 生成唯一订单号函数
查看>>
mysql优化学习笔记
查看>>
从零开始写个编译器吧系列——将在 GitBook 上以在线电子书的形式继续连载
查看>>
[LintCode] Sort Integers II [Merge-sort, Quick-sort, Heap-sort]
查看>>
RabbitMQ 基础教程(1) - Hello World
查看>>
英特尔前任 CEO 安迪·格鲁夫的传奇一生
查看>>
memcache 安装 (windows和linux)
查看>>
浅谈Android应用保护(一):Android应用逆向的基本方法
查看>>
IIFE语法
查看>>
Mysql 架构及优化之-主从复制同步部署
查看>>
【11】把 Elasticsearch 当数据库使:Filter 下钻
查看>>
iOS原生分享—UIActivityViewController
查看>>
创业的N种死法:抵御DDoS攻击花钱致死
查看>>
[TODO]Iterator, foreach, generics and callback in C# and Python
查看>>