题目链接: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. vue教程二 vue组件(1)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <script ...

  2. hdu 6397 Character Encoding (生成函数)

    Problem Description In computer science, a character is a letter, a digit, a punctuation mark or som ...

  3. RocketMQ中Broker的启动源码分析(二)

    接着上一篇博客  [RocketMQ中Broker的启动源码分析(一)] 在完成准备工作后,调用start方法: public static BrokerController start(Broker ...

  4. 如何阅读JDK源码

    JDK源码阅读笔记: https://github.com/kangjianwei/LearningJDK 如何阅读源码,是每个程序员需要面临的一项挑战. 为什么需要阅读源码?从实用性的角度来看,主要 ...

  5. ibatis 核心原理解析

    最近查找一个生产问题的原因,需要深入研究 ibatis 框架的源码.虽然最后证明问题的原因与 ibatis 无关,但是这个过程加深了对 ibatis 框架原理的理解. 这篇文章主要就来讲讲 ibati ...

  6. java并发编程(三)----线程的同步

    在现实开发中,我们或多或少的都经历过这样的情景:某一个变量被多个用户并发式的访问并修改,如何保证该变量在并发过程中对每一个用户的正确性呢?今天我们来聊聊线程同步的概念. 一般来说,程序并行化是为了获得 ...

  7. 【Kubernetes 系列三】Kubernetes 学习文档推荐

    标题 地址 备注 Kubernetes 官方文档 https://kubernetes.io/docs 英文文档,全面 Kubernetes Handbook ttps://jimmysong.io/ ...

  8. APP启动优化

    1. 去除启动黑屏 1.1 在style.xml中定义两种主题: <style name="AppTheme" parent="Theme.AppCompat.Da ...

  9. [转载]线程池ThreadPoolExecutor使用简介

    一.简介 线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为: ThreadPoolExecutor(int corePoolSize, int ...

  10. 一文了解java异常机制

    1.异常的概述 1.1什么是异常? 异常:程序在运行过程中发生由于外部问题导致的程序异常事件,发生的异常会中断程序的运行.(在Java等面向对象的编程语言中)异常本身是一个对象,产生异常就是产生了一个 ...