博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces 877E - Danil and a Part-time Job 线段树+dfs序
阅读量:4569 次
发布时间:2019-06-08

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

给一个有根树,1e5个节点,每个节点有权值0/.1,
1e5操作:
1.将一个点的子树上所有点权值取反
2.查询一个点的子树的权值和
 
题解:
先深搜整颗树,用dfs序建立每个点对应的区间,
等于把树拍扁成一个数列,每次操作从就对点变成了对区间
然后就是裸线段树
注意拍扁后的节点标号和原来的树节点标号是不等价的,要映射一下
#include 
#define endl '\n'#define ll long long#define fi first#define se second#define pii pair
#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)using namespace std;const int maxn=1e6+7;int casn,n,m,k;vector
g[maxn];pii seg[maxn];int vis[maxn];int a[maxn];int dfn;void dfs(int now){ seg[now].fi=++dfn; vis[dfn]=now; for(auto i:g[now]) dfs(i); seg[now].se=dfn;}class segtree{#define nd node[now]#define ndl node[now<<1]#define ndr node[now<<1|1] public: struct segnode { int l,r;int sum,tag; int mid(){return (r+l)>>1;} int len(){return r-l+1;} void update(){sum=len()-sum;tag^=1;} }; vector
node; int cnt; segtree(int n) {node.resize(n<<2|3);maketree(1,n);} void pushup(int now){nd.sum=ndl.sum+ndr.sum;} void pushdown(int now){ if(nd.tag){ ndl.update(); ndr.update(); nd.tag=0; } } void maketree(int s,int t,int now=1){ nd={s,t,0,0}; if(s==t){ nd.sum=a[vis[s]]; return ; } maketree(s,nd.mid(),now<<1); maketree(nd.mid()+1,t,now<<1|1); pushup(now); } void update(int s,int t,int now=1){ if(s>nd.r||t
=nd.r){nd.update();return ;} pushdown(now); update(s,t,now<<1); update(s,t,now<<1|1); pushup(now); } int query(int s,int t,int now=1){ if(s>nd.r||t
=nd.r) return nd.sum; pushdown(now); return query(s,t,now<<1)+query(s,t,now<<1|1); }};int main() { IO; cin>>n; rep(i,2,n){ int a;cin>>a; g[a].push_back(i); } dfs(1); rep(i,1,n) cin>>a[i]; segtree tree(n); cin>>m; string s; int x; while(m--){ cin>>s>>x; if(s=="get")cout<
<

 

转载于:https://www.cnblogs.com/nervendnig/p/10229956.html

你可能感兴趣的文章
2-4-1 元组
查看>>
476. Number Complement(补数)
查看>>
生成函数
查看>>
HTMl5的存储方式sessionStorage和localStorage详解
查看>>
BZOJ 4516: [Sdoi2016]生成魔咒——后缀数组、并查集
查看>>
《JAVA程序设计》实训第一天——《猜猜看》游戏
查看>>
普通用户 crontab 任务不运行
查看>>
第三次冲刺(三)
查看>>
android实现静默安装demo
查看>>
数据缓存方案
查看>>
HDU 1086:You can Solve a Geometry Problem too
查看>>
HIPO图
查看>>
工作日志2014-06-30
查看>>
稀疏矩阵
查看>>
OpenCV2马拉松第14圈——边缘检測(Sobel,prewitt,roberts)
查看>>
移动端事件点透问题
查看>>
P1896 [SCOI2005]互不侵犯
查看>>
ESP定律手工脱壳步骤
查看>>
wex5 教程 之 图文讲解 登陆,注册,页面跳转
查看>>
问题7:JavaScript 常用正则示例
查看>>