codeforces 343D 树剖后odt维护
子树修改+路径修改+单点查询
树链剖分+区间维护即可
由于只有单点查询,我直接用了odt,复杂度还行
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
#define per(ii,a,b) for(int ii=b;ii>=a;--ii)
#define forn(ii,x) for(int ii=head[x];ii;ii=e[ii].next)
using namespace std;
const int maxn=5e5+20,maxm=2e6+10;
const int INF=0x3f3f3f3f;
const ll mod=1e9+7;
//head
int casn,n,m,k;
int num[maxn];
namespace odt{
struct segnode{
int l,r;mutable int val;
bool operator<(const segnode &b)const {return l<b.l;}
};
set<segnode> nd;
#define iter set<segnode>::iterator
auto split(int pos){
auto it=nd.lower_bound({pos,pos,0});
if(it!=nd.end()&&it->l==pos) return it;
it--;
int l=it->l,r=it->r,val=it->val;
nd.erase(it);nd.insert({l,pos-1,val});
return nd.insert({pos,r,val}).fi;
}
void update(int l,int r,int val){
auto itr=split(r+1);auto itl=split(l);
nd.erase(itl,itr);nd.insert({l,r,val});
}
int query(int pos){
auto it=nd.lower_bound({pos,pos,0});
if(it!=nd.end()&&it->l==pos) return it->val;
it--;
return it->val;
}
} namespace gg{
struct node{int to,next;}e[maxn<<1];
int head[maxn],nume,mp[maxn];
void add(int a,int b){e[++nume]={b,head[a]};head[a]=nume;}
int ltop[maxn],fa[maxn],deep[maxn];
int sz[maxn],remp[maxn];
int son[maxn],cnt,out[maxn];
void dfs1(int now,int pre,int d){
deep[now]=d,fa[now]=pre,sz[now]=1;son[now]=0;
forn(i,now) {int to=e[i].to;
if(to!=pre){
dfs1(to,now,d+1);sz[now]+=sz[to];
if(sz[to]>sz[son[now]]) son[now]=to;
}
}
}
void dfs2(int now,int pre,int sp){
ltop[now]=sp;mp[now]=++cnt;remp[cnt]=now;
if(son[now]) dfs2(son[now],now,sp);
forn(i,now){
int to=e[i].to;
if(to!=son[now]&&to!=pre) dfs2(to,now,to);
}
}
void update1(int a){odt::update(mp[a],mp[a]+sz[a]-1,1);}
void update2(int now){
while(now>1){
int l=max(mp[ltop[now]],2),r=mp[now];
if(l<=r) odt::update(l,r,0);
now=fa[ltop[now]];
}
}
} int main() {
IO;
cin>>n;
odt::nd.insert({1,n,0});
m=n-1;
while(m--){int a,b;
cin>>a>>b;
gg::add(a,b);
gg::add(b,a);
}
gg::dfs1(1,1,0);
gg::dfs2(1,0,1);
cin>>m;
while(m--){int a,b;
cin>>a>>b;
if(a==1) gg::update1(b);
if(a==2) gg::update2(b);
if(a==3) cout<<odt::query(gg::mp[b])<<endl;
}
}
codeforces 343D 树剖后odt维护的更多相关文章
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
- gym 102059A 树链剖分后odt维护区间
题意 一棵树 多次修改,每次修改一个点到根的所有边的颜色,并询问现在有哪些颜色染了恰好$m$条边 题解: 稍加思考可以知道,从某个点到根节点的颜色数,均摊复杂度很低,因此,可以考虑珂朵莉树维护重链剖分 ...
- CodeForces - 343D 树链剖分
题目链接:http://codeforces.com/problemset/problem/343/D 题意:给定一棵n个n-1条边的树,起初所有节点权值为0,然后m个操作. 1 x:把x为根的子树的 ...
- P2486 [SDOI2011]染色(树剖)区间覆盖+区间的连续段
https://www.luogu.org/problemnew/show/P2486 值的一看https://www.cnblogs.com/Tony-Double-Sky/p/9283262.ht ...
- BZOJ4196 [Noi2015]软件包管理器 【树剖】
题目 Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖(即下载安装这个软件 ...
- BZOJ3531 [Sdoi2014]旅行 【树剖 + 线段树】
题目 S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足 从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教,如飞天面条神教.隐形独角兽教.绝地教都是常见的信仰.为了方便,我们用 ...
- 2019牛客暑期多校训练营(第六场)C:Palindrome Mouse(回文树+树剖)
题意:给定字符串Str,求出回文串集合为S,问S中的(a,b)满足a是b的子串的对数. 思路:开始和题解的思路差不多,维护当前后缀的每个串的最后出现位置,但是不知道怎么套“最小回文分割”,所以想到了树 ...
- P4315 月下“毛景树”[树剖]
题目描述 毛毛虫经过及时的变形,最终逃过的一劫,离开了菜妈的菜园. 毛毛虫经过千山万水,历尽千辛万苦,最后来到了小小的绍兴一中的校园里. 爬啊爬~爬啊爬毛毛虫爬到了一颗小小的"毛景树&quo ...
- BZOJ 4712 洪水 (线段树+树剖动态维护DP)
题目大意:略 题目传送门 数据结构好题,但据说直接上动态DP会容易处理不少,然而蒟蒻不会.一氧化碳大爷说还有一个$log$的做法,然而我只会$log^{2}$的.. 考虑静态时如何处理,设$f[x]$ ...
随机推荐
- windows配置nginx实现负载均衡集群 -请求分流
windows配置nginx实现负载均衡集群 一.windows上安装nginx 1.下载nginx的windows版本http://nginx.org/en/download.html 2.把压缩文 ...
- CodeForces Round #554 Div.2
A. Neko Finds Grapes 代码: #include <bits/stdc++.h> using namespace std; ; int N, M; int a[maxn] ...
- wince可用的7-zip
7-zip下载 7-zip
- icpc 南昌邀请赛网络赛 Subsequence
题目链接:https://nanti.jisuanke.com/t/38232 就是判断输入是不是子序列 没想到贡献了将近十几次罚时..........可以说是菜的真实了 用cin cout超时了 改 ...
- LODOP不同电脑打印效果不同排查
1.位置不同,偏移问题.详细的相关偏移问题的博文:LODOP不同打印机出现偏移问题 2.样式问题. 本机浏览器解析样式不同 ,相关超文本样式博文:Lodop打印控件传入css样式.看是否传入正确样式 ...
- Word技巧设置
1.如何在Word中快速输入上下标 下标~~同时按住:Ctrl 和 =键: 上标~~同时按住:Ctrl 和 Shift 和 +键: 2.如何删除Word中产生的空白页 设置 段落 中的 固定值 ...
- 【ML】从特征分解,奇异值分解到主成分分析
1.理解特征值,特征向量 一个对角阵\(A\),用它做变换时,自然坐标系的坐标轴不会发生旋转变化,而只会发生伸缩,且伸缩的比例就是\(A\)中对角线对应的数值大小. 对于普通矩阵\(A\)来说,是不是 ...
- Java技术栈思维导图
Java技术栈思维导图 Java IO流体系 设计模式
- spring boot下使用logback或log4j生成符合Logstash标准的JSON格式
spring boot下使用logback或log4j生成符合Logstash标准的JSON格式 一.依赖 由于配置中使用了json格式的日志输出,所以需要引入如下依赖 "net.logst ...
- BAT面试经验分享——iOS高级开发工程师的自我总结!
序言 目前形势,参加到iOS队伍的人是越来越多,甚至已经到供过于求了. 今年,找过工作人可能会更深刻地体会到今年的就业形势不容乐观,随着各大公司秋招的开始,很多小伙伴都行动起来了,我也有幸获得了一份不 ...