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]$ ...
随机推荐
- iOS App上架流程(2016详细版)来源DeveloperLY
一.前言: 作为一名iOSer,把开发出来的App上传到App Store是必要的.下面就来详细讲解一下具体流程步骤. 二.准备: 一个已付费的开发者账号(账号类型分为个人(Individual).公 ...
- Java 关于cannot resolve symbol 'log'报错问题
我用的是IDEA,报错的内容是:cannot resolve symbol 'log' 如图所示: 解决方法: 1.安装插件:Settings→Plugins,输入lom回车: 2.然后选择Insta ...
- git和github的学习
摘要:Git是个实用而流行的工具,我在网上找了很多教程,发现很多扯来扯去的,难消化,难吸收,而廖雪峰老师的这个教程最好,由浅入深,一步一步跟着做,记录巩固下.原作网址:https://www.liao ...
- Asp.Net Core配置的知识总结
配置在Asp.Net Core中由四个核心的对象组成: IConfiguration:配置的最终产出物,它代表了整个asp.net core应用的配置树,这棵树有根节点,子节点和叶子节点,根节点由IC ...
- 【MySQL 读书笔记】当我们在使用索引的时候我们在做什么
我记得之前博客我也写过关于索引使用的文章,但是并不全面,这次尽量针对重点铺全面一点. 因为索引是数据引擎层的结构我们只针对最常见使用的 Innodb 使用的 B+Tree 搜索树结构进行介绍. 每一个 ...
- 回忆曾经的SSM框架实现文件上传
近期在使用springboot实现文件上传的功能,想到曾经用SSM做过这个功能,在这里记录一下过去实现的方式 maven添加文件上传所需的依赖 springMVC的配置文件配置一下文件上传 我实现的是 ...
- ionic3使用moment.js
安装npm模块 $ npm install moment 例如在/home/home.ts文件里 import { Component } from '@angular/core'; import { ...
- sts 创建springMVC项目---- maven和tomcat 错误处理
今天学习spring的时候,学到了springMVC, 因为springMVC 就是beginning spring 书籍的第三章,为了更深入或更简单的起步学习springMVC, 我又找了另外一本书 ...
- Leetcode 4.28 string
1. 38. Count and Say 就是对于前一个数,找出相同元素的个数,把个数和该元素存到新的string里.数量+字符 class Solution { public String coun ...
- 使用ffmpeg将Mp4转gif
视频转动图,是个强需求,家大业大的微博相册只可上传图片,进而基于微博相册的生态也是如此.目前,网络上有许多转换.压缩的网站,多数执行速度慢或者收费,体验较差. ffmpeg是一个开源的音频处理软件,支 ...