http://codeforces.com/contest/877/problem/E

真的菜的不行,自己敲一个模板,到处都是问题。哎

 #include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+;
#define lson (q<<1)
#define rson ((q<<1)|1)
struct node
{
int l,r,mid;
int v,lazy;
}tree[maxn*];
int L[maxn],R[maxn],index;
int vis[maxn] = {};
int a[maxn];
vector<int> g[maxn];
void dfs(int x)
{
L[x] = index;
for(int i=;i<g[x].size();i++)
{
int v = g[x][i];
if(!vis[v])
{
vis[v] = ;
index++;
dfs(v);
}
}
R[x] = index;
}
void push_up(int q)
{
tree[q].v = tree[lson].v+tree[rson].v;
}
void build(int l,int r,int q)
{
tree[q].l = l,tree[q].r = r,tree[q].mid = (l+r)/;
tree[q].lazy = ;
if(l==r)
{
tree[q].v = a[l];
//printf("%d %d\n",l,tree[q].v);
return;
}
build(l,tree[q].mid,lson);
build(tree[q].mid+,r,rson);
push_up(q);
}
void push_down(int q)
{
if(tree[q].lazy)
{
tree[lson].v = (tree[lson].r-tree[lson].l+)-tree[lson].v;
tree[rson].v = (tree[rson].r-tree[rson].l+)-tree[rson].v;
tree[q].lazy ^= ;
tree[lson].lazy ^= ;
tree[rson].lazy ^= ;
}
}
void update(int l,int r,int q)
{
if(tree[q].l>=l&&tree[q].r<=r)
{
tree[q].lazy ^= ;
tree[q].v = (tree[q].r-tree[q].l+)-tree[q].v;
return;
}
push_down(q);
if(l<=tree[q].mid) update(l,r,lson);
if(r>=tree[q].mid+) update(l,r,rson);
push_up(q); ///v值由下往上更新
}
int query(int l,int r,int q) ///由上及下
{
if(tree[q].l>=l&&tree[q].r<=r)
{
// printf("%d %d %d %d\n",tree[q].l,tree[q].r,tree[q].v,tree[q].lazy);
return tree[q].v;
}
push_down(q);
int sum = ;
if(l<=tree[q].mid) sum += query(l,r,lson);
if(r>=tree[q].mid+) sum += query(l,r,rson);
return sum;
}
int main()
{
int n;scanf("%d",&n);
for(int i=;i<=n;i++)
{
int x;scanf("%d",&x);
g[x].push_back(i);
g[i].push_back(x);
}
index = ;vis[] = ;
dfs();
for(int i=;i<=n;i++)
{
int cc;
scanf("%d",&cc);
a[L[i]] = cc; ///按dfs序来赋值
}
build(,n,);
int q;scanf("%d",&q);
while(q--)
{
char s[];
int v;
scanf("%s %d",s,&v);
if(s[]=='g')
{
printf("%d\n",query(L[v],R[v],));
}
else
{
update(L[v],R[v],);
}
}
return ;
}
/*
10
1 2 3 3 5 5 7 7 8
0 0 0 0 1 1 1 1 0 0
10
pow 3
get 3
*/

Codeforces Round #442 (Div. 2) Danil and a Part-time Job的更多相关文章

  1. 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序加上一个线 ...

  2. Codeforces Round #442 Div.2 A B C D E

    A. Alex and broken contest 题意 判断一个字符串内出现五个给定的子串多少次. Code #include <bits/stdc++.h> char s[110]; ...

  3. Codeforces Round #442 (Div. 2)

    A. Alex and broken contest time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  4. 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 ...

  5. 【Codeforces Round #442 (Div. 2) A】Alex and broken contest

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 注意是所有的名字里面,只出现了其中某一个名字一次. [代码] #include <bits/stdc++.h> usin ...

  6. 【Codeforces Round #442 (Div. 2) D】Olya and Energy Drinks

    [链接] 我是链接,点我呀:) [题意] 给一张二维点格图,其中有一些点可以走,一些不可以走,你每次可以走1..k步,问你起点到终点的最短路. [题解] 不能之前访问过那个点就不访问了.->即k ...

  7. 【Codeforces Round #442 (Div. 2) C】Slava and tanks

    [链接] 我是链接,点我呀:) [题意] 有n个位置,每个位置都可能有不定数量的tank; 你每次可以选择一个位置投掷炸弹. 并且,这个位置上的所有tank都会受到你的攻击. 并且失去一点体力. 然后 ...

  8. 【Codeforces Round #442 (Div. 2) B】Nikita and string

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举中间那一段从哪里开始.哪里结束就好 注意为空的话,就全是a. 用前缀和优化一下. [代码] #include <bits/ ...

  9. Codeforces Round #442 (Div. 2) B题【一道模拟题QAQ】

    B. Nikita and string One day Nikita found the string containing letters "a" and "b&qu ...

随机推荐

  1. Linux-Java安装

    安装 yum -y install java-1.8.0-openjdk* Tomcat安装:到官网http://tomcat.apache.org/,然后找到Tomcat9下载地址 http://m ...

  2. 反爬虫之搭建IP代理池

    反爬虫之搭建IP代理池 听说你又被封 ip 了,你要学会伪装好自己,这次说说伪装你的头部.可惜加了header请求头,加了cookie 还是被限制爬取了.这时就得祭出IP代理池!!! 下面就是requ ...

  3. linux下安装mysql并设置远程连接

    腾讯云环境为Centos7.4   mysql版本为5.6 本次安装使用yum安装 检查是否已有mysql: yum list installed | grep mysql 下载yum源文件: wge ...

  4. HDU - 4027 Can you answer these queries?(线段树)

    给定一个长度为n的序列,m次操作. 每次操作 可以将一个区间内的所有数字变为它的根号. 可以查询一个区间内所有元素的和. 线段树的初级应用. 如果把一个区间内的元素都改为它的根号的话,是需要每个数字都 ...

  5. selenium2设置浏览器窗口

    1.窗口最大化 //设置窗口最大化driver.manage().window().maximize(); 2.指定设置窗口大小 //指定呀设置窗口的宽度为:800,高度为600Dimension d ...

  6. 基于EPOLL模型的局域网聊天室和Echo服务器

    一.EPOLL的优点 在Linux中,select/poll/epoll是I/O多路复用的三种方式,epoll是Linux系统上独有的高效率I/O多路复用方式,区别于select/poll.先说sel ...

  7. linux 复制部分文件到另外的文件夹

    show the command: |xargs -i cp {} ../ 或者指定目录 |xargs -i cp {} /home/peter

  8. 聊聊、Highcharts 动态数据

    最近项目中需要用到图表,找了几个开源框架,最后选择 Highcharts,原因是 Highcharts 功能强大,稳定,方便,而且开源,社区比较成熟. 首先下载 Highcharts,导入项目. 在 ...

  9. leetcode with python -> tree

    100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two bi ...

  10. [python][oldboy]python涉及的几种编码

    1 python文件代码中中文的识别 (换言之,python代码的中文不显示乱码)和解析u"中文"这样的unicode对象 # coding=utf8 2 python运行环境(I ...