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]$ ...
随机推荐
- Python_自定义递归的最大深度
自定义递归的最大深度 python默认的最大递归深度为998,在有些情况下是不够用,需要我们自行设置.设置方式如下: import sys sys.setrecursionlimit(num) # n ...
- 微信支付之02------整个微信支付功能----------Java实现
先来看下微信支付官方文档: 1.在官方文档上有很多种支付方式,由于目前我只做过JSAPI和微信扫码支付二种,其他的就不说了. >>>>>第一种微信扫码支付>> ...
- java关键字保留字
Here is a list of keywords in the Java programming language. You cannot use any of the following as ...
- vue项目笔记
参考了很多网上其他人的 1.安装 npm与cnpm:npm(node package manager)是nodejs的包管理器,用于node插件管理(包括安装.卸载.管理依赖等):npm可以在node ...
- HBuilderX——编译失败:HBuilderX 安装目录不能包括 ( 等特殊字符
前言 编译小程序的时候报错,原因其实很简单,安装目录的问题! 解决 我安装到了D:\Program Files (x86),放到D盘的根目录下或者D:\Program Files只要不包含一些特殊字符 ...
- JQ高级
一.选择器 css语法选择器 $('css3 选择器位‘) 索引匹配 $('div:eq(0)') $('div').eq(0) 内容 $('div:contains(标签文本内容)') // 注:采 ...
- 制作OSGB数据索引
[干货]教你用.S3C文件制作OSGB数据索引 [干货]教你用.S3C文件制作OSGB数据索引_搜狐汽车_搜狐网 S3C是ContextCapture(原Smart 3D)的一种数据格式,.S3C格式 ...
- Magento 2 自带模态的应用
Modal widget in Magento 2 Magento 2 自带模态的应用 使用magento 2 的自带模态组件,以下代码只供参考使用. 1,DOM >模态块与触发元素 .pthm ...
- <TCP/IP原理> (四) IP编址
1.IP地址的基本概念:作用.结构.类型 2.特殊地址:作用.特征 网络地址.广播地址(直接.受限) 0.0.0.0 环回地址 3.单播.多播.广播地址:特征 4.专用地址:作用.范围 5.计算和应用 ...
- django 2.接口之工作原理
1.创建应用程序有两种方法,第一种就是在新建项目的时候,在最初使的时填入应用程序名称,第二种就是进入目录下面,输入 python manage.py startapp appName 就会自动生成一个 ...