CodeForces - 343D 树链剖分
题目链接:http://codeforces.com/problemset/problem/343/D
题意:给定一棵n个n-1条边的树,起初所有节点权值为0,然后m个操作。 1 x:把x为根的子树的点的权值修改为1; 2 x:把x结点到根路径上的点修改为0; 3 x:查询结点x的值。
思路:树链剖分。 对于操作1,子树操作记录下当前点为根时,dfs的最后一个点的id是多少(endid[]),然后就可以把子树操作用区间来维护了。 对于操作2,树链剖分。 对于3操作,线段树单点查询。
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<time.h>
#include<cmath>
#include<sstream>
#include<assert.h>
using namespace std;
#define L(x) x<<1
#define R(x) x<<1|1
typedef long long int LL;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
const int MAXN = + ;
int head[MAXN], tot, cnt;
struct Edge{
int to, next;
}Edges[MAXN * ];
void add(int u, int v){
Edges[tot].to = v;
Edges[tot].next = head[u];
head[u] = tot++;
}
int id[MAXN], endid[MAXN],son[MAXN], deep[MAXN], size[MAXN], fa[MAXN], reid[MAXN], top[MAXN];
void Init(){
tot = ; cnt = ;
memset(head, -, sizeof(head));
memset(son, -, sizeof(son));
}
void DFS1(int u, int p,int dep){
fa[u] = p; size[u] = ; deep[u] = dep;
for (int i = head[u]; i != -; i = Edges[i].next){
if (Edges[i].to != p){
DFS1(Edges[i].to, u,dep+);
size[u] += size[Edges[i].to];
if (son[u] == - || size[Edges[i].to] > size[son[u]]){
son[u] = Edges[i].to;
}
}
}
}
void DFS2(int u, int tp){
id[u] = ++cnt; reid[id[u]] = u; top[u] = tp;
if (son[u] == -){ endid[u] = cnt; return; }
DFS2(son[u], tp);
for (int i = head[u]; i != -; i = Edges[i].next){
if (son[u] != Edges[i].to&&Edges[i].to != fa[u]){
DFS2(Edges[i].to, Edges[i].to);
}
}
endid[u] = cnt;
}
struct Node{
int st, ed;
int lazy,val;
}Seg[MAXN * ];
void pushDown(int k){
if (Seg[k].lazy!=-){
Seg[L(k)].lazy = Seg[L(k)].val = Seg[k].lazy;
Seg[R(k)].lazy = Seg[R(k)].val = Seg[k].lazy;
Seg[k].lazy = -;
}
}
void Build(int l, int r, int k){
Seg[k].st = l; Seg[k].ed = r; Seg[k].lazy = -; Seg[k].val = ;
if (l == r){
return;
}
int mid = (l + r) / ;
Build(l, mid, L(k)); Build(mid + , r, R(k));
}
void Modify(int l,int r,int val,int k){
if (Seg[k].st == l&&Seg[k].ed == r){
Seg[k].lazy = Seg[k].val = val;
return;
}
pushDown(k);
if (r <= Seg[L(k)].ed){
Modify(l, r, val, L(k));
}
else if (l >= Seg[R(k)].st){
Modify(l, r, val, R(k));
}
else{
Modify(l, Seg[L(k)].ed, val, L(k));
Modify(Seg[R(k)].st, r, val, R(k));
}
}
void Modify(int u, int v,int val){
int f1 = top[u], f2 = top[v];
while (f1 != f2){
if (deep[f1] < deep[f2]){
swap(f1, f2);
swap(u, v);
}
Modify(id[f1], id[u], val,);
u = fa[f1]; f1 = top[u];
}
if (deep[u] > deep[v]){
swap(u, v);
}
Modify(id[u], id[v],val, );
}
int Query(int pos, int k){
if (Seg[k].st == Seg[k].ed){
return Seg[k].val;
}
pushDown(k);
if (pos <= Seg[L(k)].ed){
return Query(pos, L(k));
}
else{
return Query(pos, R(k));
}
}
int main(){
//#ifdef kirito
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
//#endif
// int start = clock();
int n,m;
while (~scanf("%d",&n)){
Init();
for (int i = ; i < n; i++){
int u, v;
scanf("%d%d", &u, &v);
add(u, v); add(v, u);
}
DFS1(, , ); DFS2(, ); Build(, n, );
scanf("%d", &m);
while (m--){
int u, v;
scanf("%d%d", &u, &v);
switch (u){
case : Modify(id[v], endid[v], , ); break;
case : Modify(v, , ); break;
default: printf("%d\n", Query(id[v], )); break;
}
}
}
//#ifdef LOCAL_TIME
// cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
return ;
}
CodeForces - 343D 树链剖分的更多相关文章
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
- Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组
Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations ...
- CodeForces 191C 树链剖分 第4遍
非常无奈,模板重新无奈的打错了.. 只是,非常快便找到了.. 题意:给一些边,有一些操作,每次操作,都要在这些边上加上1,求每一个边的边权.. #include<cstdio> #incl ...
- CodeForces 343D water tree(树链剖分)
Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...
- Codeforces 343D Water Tree & 树链剖分教程
原题链接 题目大意 给定一棵根为1,初始时所有节点值为0的树,进行以下三个操作: 将以某点为根的子树节点值都变为1 将某个节点及其祖先的值都变为0 *询问某个节点的值 解题思路 这是一道裸的树链剖分题 ...
- Educational Codeforces Round 3 E. Minimum spanning tree for each edge (最小生成树+树链剖分)
题目链接:http://codeforces.com/contest/609/problem/E 给你n个点,m条边. 问枚举每条边,问你加这条边的前提下组成生成树的权值最小的树的权值和是多少. 先求 ...
- Codeforces Round #329 (Div. 2) D. Happy Tree Party 树链剖分
D. Happy Tree Party Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/593/p ...
- 【Codeforces】【网络流】【树链剖分】【线段树】ALT (CodeForces - 786E)
题意 现在有m个人,每一个人都特别喜欢狗.另外还有一棵n个节点的树. 现在每个人都想要从树上的某个节点走到另外一个节点,且满足要么这个人自带一条狗m,要么他经过的所有边h上都有一条狗. 2<=n ...
- Codeforces 704E Iron Man [树链剖分,计算几何]
Codeforces 这题--真是搞死我了-- 好不容易下定了决心要不颓废,要写题,结果一调就调了十几个小时-- 思路 我们发现在树上做非常不舒服,于是树链剖分之后一次在重链上的移动就可以看做是在df ...
随机推荐
- dict常用操作
dict是python中的常用数据结构,应该尽量掌握其使用方法 """ 初始化一个dict的四种方式: 1. dict() -> 创建一个空的dict 2. dic ...
- 多个webView嵌套在UIScrollView里面内存优化
之前一直纠结在使用UIWebView上,发现加载6个UIWebView占用了290M左右内存,切换时还会增加.后来看到一篇文章:http://blog.csdn.net/kongu/article/d ...
- MJ瀑布流学习笔记
1. 如果系统自带的布局的话,是这样: //系统自带的UICollectionViewFlowLayout 而不是UICollectionViewLayout UICollectionViewFlow ...
- 怎么测试php代码
没有任何一名程序员可以一气呵成.完美无缺的在不用调试的情况下完成一个功能或模块.调试实际分很多种情况. 暴力调试 这种方式简单粗暴,一般PHP程序员都会用,那就是浏览器调试,在编辑器内写完代码后随后打 ...
- Java 迭代器删除元素ConcurrentModificationException异常。
Java是不支持容器类在使用迭代器迭代过程中,使用如 list.remove(obj)方法删除元素.否则会抛出ava.util.ConcurrentModificationException异常.应该 ...
- luoguP3806 【模板】点分治1
#include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) #define ...
- html简单标签代码
html简单标签代码demo <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ht ...
- nginx配置-Rewrite
rewrite功能就是,使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向.rewrite只能放在server{},location{},if{}中,并且只能 ...
- JS-Number 的精度
JS 使用 IEEE 754 的双精度数表示数字,1 位符号,10 位指数,53 位底数. 所以 JS 数字精度近似为 15.95 位 10 进制(10 ** 15.95). 也就是说整部加小数部分超 ...
- 理解JavaScript中的回调函数
理解回调函数,首先要知道在JavaScript中,函数也是对象,它可以赋值给变量,也可以作为参数传递给另一个函数.比如: var add=function(a,b){ console.log(a+b) ...