Codeforces Round #442 (Div. 2) E Danil and a Part-time Job (dfs序加上一个线段树区间修改查询)
题意:
给出一个具有N个点的树,现在给出两种操作:
1.get x,表示询问以x作为根的子树中,1的个数。
2.pow x,表示将以x作为根的子树全部翻转(0变1,1变0).
思路:dfs序加上一个线段树区间修改查询。
AC代码:
#include<iostream>
#include<vector>
#include<string.h>
using namespace std;
const int maxn=2e5+5;
int sum[maxn<<2],lazy[maxn<<2],a[maxn],num[maxn],lft[maxn],rght[maxn],tot;
char s[10];
vector<int>map[maxn];
void build(int k,int l,int r){
if(l==r){
sum[k]=a[num[l]];
return ;
}
int mid=(l+r)/2;
build(k*2,l,mid);
build(k*2+1,mid+1,r);
sum[k]=sum[k*2]+sum[k*2+1];
}
void pushdown(int k,int l,int r){
if(lazy[k]){
int mid=(l+r)/2;
sum[k*2]=mid-l+1-sum[k*2];
sum[k*2+1]=r-mid-sum[k*2+1];
//sum[k]=sum[k*2]+sum[k*2+1];
lazy[k*2]^=1;
lazy[k*2+1]^=1;
lazy[k]=0;
}
}
void update(int k,int l,int r,int L,int R){
if(L<=l&&r<=R){
sum[k]=r-l+1-sum[k];
lazy[k]^=1;
return;
}
pushdown(k,l,r);
int mid=(l+r)/2;
if(mid>=L)update(k*2,l,mid,L,R);
if(mid<R)update(k*2+1,mid+1,r,L,R);
sum[k]=sum[k*2]+sum[k*2+1];
return ;
}
int query(int k,int l,int r,int L,int R){
if(L<=l&&r<=R)return sum[k];
int ans=0;
pushdown(k,l,r);
int mid=(l+r)/2;
if(mid>=L)ans+=query(k*2,l,mid,L,R);
if(mid<R)ans+=query(k*2+1,mid+1,r,L,R);
//sum[k]=sum[k*2]+sum[k*2+1];
return ans;
}
//pushdown和query的pushup可以不要,但是在update中pushdown之后需要pushup
void dfs(int k){
lft[k]=++tot;
for(int i=0;i<map[k].size();i++){
dfs(map[k][i]);
}
rght[k]=tot;
}
//每个点的left序号就是在线段树中的序号
//每个点对应的left[i]-right[i]+1就是每个点的字节点的个数(加上自己)
int main(){
int n,x;
while(cin>>n){
for(int i=1;i<=n;i++)map[i].clear();
for(int i=2;i<=n;i++){
cin>>x;
map[x].push_back(i);
}
memset(sum,0,sizeof(sum));
memset(lazy,0,sizeof(lazy));
tot=0;
dfs(1);
/*for(int i=1;i<=n;i++){
cout<<lft[i]<<' '<<rght[i]<<endl;
}*/
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++){
num[lft[i]]=i;
//每个点在线段树中的序号和本身不是一样的
//注意看build函数
}
build(1,1,n);
int q;
cin>>q;
for(int i=0;i<q;i++){
cin>>s>>x;
if(s[0]=='p'){
update(1,1,n,lft[x],rght[x]);
}
else cout<<query(1,1,n,lft[x],rght[x])<<endl;
}
}
return 0;
}

圆圈中的数字是每个节点的left值,每个点的right值就是每个点的子节点中left最大的那个
该图对应输入
10
1 2 3 4 2 4 1 7 8
1 1 0 1 1 0 0 0 1 1
10
pow 1
get 2
pow 2
pow 8
get 6
pow 6
pow 10
get 6
pow 8
pow 3
输出 3 0 1
(第一次看dfs序,看了好久才明白,还耽误了吴老狗睡觉的时间T^T,还是太菜了)
Codeforces Round #442 (Div. 2) E Danil and a Part-time Job (dfs序加上一个线段树区间修改查询)的更多相关文章
- Codeforces Round #169 (Div. 2) E. Little Girl and Problem on Trees dfs序+线段树
E. Little Girl and Problem on Trees time limit per test 2 seconds memory limit per test 256 megabyte ...
- Codeforces Round #442 (Div. 2)A,B,C,D,E(STL,dp,贪心,bfs,dfs序+线段树)
A. Alex and broken contest time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Codeforces Round #622 (Div. 2) A. Fast Food Restaurant(全排列,DFS)
Codeforces Round #622 (Div. 2) A. Fast Food Restaurant 题意: 你是餐馆老板,虽然只会做三道菜,上菜时还有个怪癖:一位客人至少上一道菜,且一种菜最 ...
- Codeforces Round #442 (Div. 2) Danil and a Part-time Job
http://codeforces.com/contest/877/problem/E 真的菜的不行,自己敲一个模板,到处都是问题.哎 #include <bits/stdc++.h> u ...
- Codeforces Round #442 Div.2 A B C D E
A. Alex and broken contest 题意 判断一个字符串内出现五个给定的子串多少次. Code #include <bits/stdc++.h> char s[110]; ...
- Codeforces Round #877 (Div. 2) E. Danil and a Part-time Job
E. Danil and a Part-time Job 题目链接:http://codeforces.com/contest/877/problem/E time limit per test2 s ...
- Codeforces Round #442 (Div. 2)
A. Alex and broken contest time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- 【Codeforces Round #442 (Div. 2) A】Alex and broken contest
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 注意是所有的名字里面,只出现了其中某一个名字一次. [代码] #include <bits/stdc++.h> usin ...
- 【Codeforces Round #442 (Div. 2) D】Olya and Energy Drinks
[链接] 我是链接,点我呀:) [题意] 给一张二维点格图,其中有一些点可以走,一些不可以走,你每次可以走1..k步,问你起点到终点的最短路. [题解] 不能之前访问过那个点就不访问了.->即k ...
随机推荐
- 路径规划(1)--连接GPS接收端
从淘宝上入手的GPS接收端U-BLOX M8N,带UART串口连接线. 一.打开树莓派上的UART串口通信 1.下载pi3-miniuart-bt-overlay.dtb,并将dtb文件拷贝到/boo ...
- 黑电平校正BLC
参考:https://www.cnblogs.com/zhangAlin/p/10661763.html
- debian shell脚本关联
懒得命令行一个个的输 设置,MIME类型编辑,搜索x-shellscript,默认的改成/bin/bash即可
- 爬虫-day02-抓取和分析
###页面抓取### 1.urllib3 是一个功能强大且好用的HTTP客户端,弥补了Python标准库中的不足 安装: pip install urllib3 使用: imp ...
- ES - dynamic field mapping
Dynamic field mapping 1.我们向es提交一个json对象进行索引,es会对json字段和索引字段进行字段类型适配. 规则如下: 2.string字段的转换规则 当date det ...
- 使用powerpoint的表对象
以下为basic范例,delphi使用需要加以修改 Table 对象 代表幻灯片上的表格形状.Table 对象是 Shapes 集合的成员.Table 对象包含 Columns 集合和 Rows 集合 ...
- 我的第二本译作《精通OpenStack》上架啦:前言、目录和样章
1. 前言 今天,随着新功能和子项目的增加,OpenStack已成为一个不断扩展的大型开源项目.随着数以百计大型企业采用并不断为OpenStack生态系统做出贡献,OpenStack必将成为下一代私有 ...
- Linux 学习目录
1 VIM 快捷键
- Two kinds of item classification model architecture
Introduction: Introduction to Fusing-Probability model: Cause the input has two parts, one is item i ...
- c#+CAD动态移动效果
public class MoveRotateScaleJig : DrawJig { public static List<Entity> entities = new List< ...