Codeforces 877E - Danil and a Part-time Job 线段树+dfs序
1e5操作:
1.将一个点的子树上所有点权值取反
2.查询一个点的子树的权值和
等于把树拍扁成一个数列,每次操作从就对点变成了对区间
然后就是裸线段树
#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#define fi first
#define se second
#define pii pair<int,int>
#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<int>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<segnode> 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.l) return ;
if(s<=nd.l&&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.l) return 0;
if(s<=nd.l&&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<<tree.query(seg[x].fi,seg[x].se)<<endl;
else tree.update(seg[x].fi,seg[x].se);
}
return 0;
}
Codeforces 877E - Danil and a Part-time Job 线段树+dfs序的更多相关文章
- Codeforces 571D - Campus(并查集+线段树+DFS 序,hot tea)
Codeforces 题目传送门 & 洛谷题目传送门 看到集合的合并,可以本能地想到并查集. 不过这题的操作与传统意义上的并查集不太一样,传统意义上的并查集一般是用来判断连通性的,而此题还需支 ...
- Codeforces 343D WaterTree - 线段树, DFS序
Description Translated by @Nishikino_Maki from Luogu 行吧是我翻的 Mad scientist Mike has constructed a roo ...
- Codeforces 877E - Danil and a Part-time Job(dfs序+线段树)
877E - Danil and a Part-time Job 思路:dfs序+线段树 dfs序:http://blog.csdn.net/qq_24489717/article/details/5 ...
- codeforces 877 E. Danil and a Part-time Job(线段树(dfs序))
题目链接:http://codeforces.com/contest/877/problem/E 题解:显然一看就感觉要么树链剖分要么线段树+dfs序,题目要求的操作显然用线段树+dfs序就可以实现. ...
- [Codeforces 464E] The Classic Problem(可持久化线段树)
[Codeforces 464E] The Classic Problem(可持久化线段树) 题面 给出一个带权无向图,每条边的边权是\(2^{x_i}(x_i<10^5)\),求s到t的最短路 ...
- CodeForces 877E Danil and a Part-time Job(dfs序+线段树)
Danil decided to earn some money, so he had found a part-time job. The interview have went well, so ...
- Codeforces 877E Danil and a Part-time Job(dfs序 + 线段树)
题目链接 Danil and a Part-time Job 题意 给出一系列询问或者修改操作 $pow$ $x$表示把以$x$为根的子树的所有结点的状态取反($0$变$1$,$1$变$0$ ...
- [Codeforces 877E] Danil and a Part-time Job
[题目链接] https://codeforces.com/contest/877/problem/E [算法] 首先求出这棵树的DFS序 一棵子树的DFS序为连续的一段 , 根据这个性质 , 用线段 ...
- Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq
B. Prison Transfer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...
随机推荐
- Java 控制语句
Java 控制语句
- LOJ2831 JOISC2018 道路建设 LCT、树状数组
传送门 题目的操作大概是:求某个点到根的链的逆序对,然后对这条链做区间赋值 求某个点到根的链,就是LCT中的access操作,所以我们每一次把access过后的链打上标记,就可以做到区间赋值了. 计算 ...
- 基于struts2、hibernate、spring、shiro、MySQL的项目开发
使用maven管理jar包的依赖 < project xmlns = “ http://maven.apache.org/POM/4.0.0 ” xmlns :xsi = “ http://ww ...
- Python——接口类、抽象类
建立一个接口类.抽象类的规范 from abc import abstractmethod,ABCMeta class Payment(metaclass=ABCMeta): # 元类 默认的元类 t ...
- js DOM操作 容易犯的错误
这样一段html片段 <select class="form-control" id="course_chapter" onchange="fi ...
- poj2778(AC自动机+矩阵快速幂)
题意:给你n个字符串,问你长度为m的字符串且字符串中不含有那n个子串的字符串的数量 解题思路:这道题一开始就不太懂,还以为是组合数学的题目,后面看了别人的博客,才知道这是属于AC自动机的另一种用法,是 ...
- python基础之小数据池、代码块、编码和字节之间换算
一.代码块.if True: print(333) print(666) while 1: a = 1 b = 2 print(a+b) for i in '12324354': print(i) 虽 ...
- ICPC中国南昌国家邀请赛和国际丝绸之路规划大赛预选赛 I J
I. Max answer 链接:https://nanti.jisuanke.com/t/38228 思路: 枚举最小值,单调栈确定最小值的边界,用线段树+前缀和维护最小值的左右区间 实现代码: # ...
- python yield 理解与用法
1.一句话快速理解 yield 等于 return 这么简单理解 2.详细说明: yield和return的关系和区别了,带yield的函数是一个生成器,而不是一个函数了 这个生成器有一个函数就是n ...
- FreeNAS-9.10虚拟机测试安装
虚拟机安装NreeNAS-9.10步骤 需求:网络监控磁盘要扩容 测试环境: CPU 内存 系统盘 共享盘 网卡 2核 2G 20G 20G 桥接 系统版本:FreeNAS-9.10 一.配置虚拟机 ...