题目链接:http://codeforces.com/contest/877/problem/E

题解:显然一看就感觉要么树链剖分要么线段树+dfs序,题目要求的操作显然用线段树+dfs序就可以实现。然后就敲一下线段树+dfs序就行挺简单的只要dfs一遍记录当前节点的下表然后再加一个leng数组来存子树最多到达几然后更新或者求值的时候只要查询(pos[x],leng[x])即可。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#define lson (i << 1)
#define rson ((i << 1) | 1)
using namespace std;
const int M = 2e5 + ;
int a[M] , pos[M] , leng[M] , pre[M] , cnt;
vector<int> vc[M];
struct TnT {
int l , r , sum , lazy;
}T[M << ];
void push_up(int i) {
T[i].sum = T[lson].sum + T[rson].sum;
}
void push_down(int i) {
if(T[i].lazy) {
T[lson].sum = T[lson].r - T[lson].l + - T[lson].sum;
T[rson].sum = T[rson].r - T[rson].l + - T[rson].sum;
T[lson].lazy ^= ;
T[rson].lazy ^= ;
T[i].lazy = ;
}
}
void build(int l , int r , int i) {
int mid = (l + r) >> ;
T[i].l = l , T[i].r = r , T[i].sum = , T[i].lazy = ;
if(l == r) {
T[i].sum = a[pre[l]];
return ;
}
push_down(i);
build(l , mid , lson);
build(mid + , r , rson);
push_up(i);
}
void update(int l , int r , int i) {
int mid = (T[i].l + T[i].r) >> ;
if(T[i].l == l && T[i].r == r) {
T[i].sum = (T[i].r - T[i].l + ) - T[i].sum;
T[i].lazy ^= ;
return ;
}
push_down(i);
if(mid < l) {
update(l , r , rson);
}
else if(mid >= r) {
update(l , r , lson);
}
else {
update(l , mid , lson) , update(mid + , r , rson);
}
push_up(i);
}
int query(int l , int r , int i) {
int mid = (T[i].l + T[i].r) >> ;
if(T[i].l == l && T[i].r == r) {
return T[i].sum;
}
push_down(i);
if(mid < l) {
return query(l , r , rson);
}
else if(mid >= r) {
return query(l , r , lson);
}
else {
return query(l , mid , lson) + query(mid + , r , rson);
}
}
void dfs(int u) {
int len = vc[u].size();
for(int i = ; i < len ; i++) {
int v = vc[u][i];
pos[v] = ++cnt;
pre[cnt] = v;
dfs(v);
leng[v] = cnt;
}
}
int main() {
int n , x , q;
char s[];
scanf("%d" , &n);
for(int i = ; i <= n ; i++) {
scanf("%d" , &x);
vc[x].push_back(i);
}
for(int i = ; i <= n ; i++) {
scanf("%d" , &a[i]);
}
scanf("%d" , &q);
cnt = ;
pos[] = ++cnt;
pre[cnt] = ;
leng[] = n;
dfs();
build( , n , );
while(q--) {
scanf("%s" , s);
if(s[] == 'g') {
scanf("%d" , &x);
printf("%d\n" , query(pos[x] , leng[x] , ));
}
else {
scanf("%d" , &x);
update(pos[x] , leng[x] , );
}
}
return ;
}

codeforces 877 E. Danil and a Part-time Job(线段树(dfs序))的更多相关文章

  1. Codeforces 571D - Campus(并查集+线段树+DFS 序,hot tea)

    Codeforces 题目传送门 & 洛谷题目传送门 看到集合的合并,可以本能地想到并查集. 不过这题的操作与传统意义上的并查集不太一样,传统意义上的并查集一般是用来判断连通性的,而此题还需支 ...

  2. Codeforces 877E - Danil and a Part-time Job 线段树+dfs序

    给一个有根树,1e5个节点,每个节点有权值0/.1,1e5操作:1.将一个点的子树上所有点权值取反2.查询一个点的子树的权值和   题解: 先深搜整颗树,用dfs序建立每个点对应的区间,等于把树拍扁成 ...

  3. Codeforces 343D WaterTree - 线段树, DFS序

    Description Translated by @Nishikino_Maki from Luogu 行吧是我翻的 Mad scientist Mike has constructed a roo ...

  4. Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)

    Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...

  5. Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树

    C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...

  6. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树

    题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...

  7. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线

    D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...

  8. Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)

    题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...

  9. Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

    题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...

随机推荐

  1. springBoot的过滤器,监听器,拦截器

    概述 在开发中,我们经常要考虑一些问题,对敏感词进行过滤,用户是否已经登录,是否需要对他的请求进行拦截,或者领导问现在在线人数有多少人?我们如何实现这些功能哪 @WebFilter package c ...

  2. "A valid provisioning profile for this executable was not found"问题

    时间:2015年8月14日 初接触iOS,这两天真机调试的时候遇到了这个问题.如图所示: 上网查后发现,解决方法大致有以下两种: 1. provisioning profile没有被找到,需要重新导入 ...

  3. Ubuntu下python安装mysqldb(驱动)

    最近在学习Django框架,需要使用到数据库,我使用的是mysql,跟java一样,需要安装驱动,这是驱动的下载网址http://sourceforge.net/projects/mysql-pyth ...

  4. 代码生成java连接数据库的所需代码(超详细)

    开始学习: round 1:(一开始学习当然还是要一步一步学习的啦,哪有什么一步登天!!!) a.准备工作:1.eclipse,mysql(这两个软件肯定要的啦,不然学什么把它们连接起来) 2.加载驱 ...

  5. spark shuffle写操作三部曲之UnsafeShuffleWriter

    前言 在前两篇文章 spark shuffle的写操作之准备工作 中引出了spark shuffle的三种实现,spark shuffle写操作三部曲之BypassMergeSortShuffleWr ...

  6. Java爬虫框架 | 爬小说

    Jsoup,Java爬虫解决方案,中文文档:jsoup   不得不说Java的生态真的好,原来我以为爬虫是只能用Pyhton来写的,结果发现Java的爬虫框架不要太多……       一分钟你就可以写 ...

  7. Unity经典游戏编程之:球球大作战

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  8. 【Java例题】3.6 计算arcsin(x)的值

    6.使用泰勒展开式计算arcsin(x)的值. arcsin(x)=x+x^3/(2*3)+1*3*x^5/(2*4*5)+...+ (2n)!*x^(2n+1)/(2^2n)*(n!)^2*(2n+ ...

  9. win10 我的电脑下面的六个文件夹的隐藏

      第一步   第二步     第三步 修改注册表,要隐藏那个文件夹,ThisPCPolicy 改为 "Hide" 修改我的文档的注册表值,使我的文档文件夹隐藏     <w ...

  10. Web前端开发工程师课程大纲

    PHP程序员雷雪松整理出来的一套独一无二的Web前端开发课程.本套Web前端开发课程专门为想励志成为优秀web前端工程师的学习者而总结归纳的,本套Web前端课程舍弃了一些不常用的即将废弃的HTML标签 ...