题目链接   Danil and a Part-time Job

题意    给出一系列询问或者修改操作

   $pow$ $x$表示把以$x$为根的子树的所有结点的状态取反($0$变$1$,$1$变$0$)

   $get$  $x$表示求以$x$为根的子树中状态为$1$的结点数。

首先大力$dfs$序,然后线段树操作一下。

具体问题转化为:区间翻转,区间求和。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i)
#define ls i << 1
#define rs i << 1 | 1
#define lson ls, L, mid
#define rson rs, mid + 1, R const int N = 2e5 + 10; struct node{
int s[2];
int len;
} t[N * 3]; int in[N], out[N], dc[N * 3], dt[N * 3], f[N], a[N];
int n, q, ti; vector <int> v[N]; void dfs(int x){
in[x] = ++ti;
f[ti] = x;
for (auto u : v[x]) dfs(u);
out[x] = ti;
} node update(node x, node y){
node ans;
ans.len = x.len + y.len;
rep(i, 0, 1) ans.s[i] = x.s[i] + y.s[i];
return ans;
} void build(int i, int L, int R){
if (L == R){
int z = a[f[L]];
t[i].len = t[i].s[z] = 1;
t[i].s[z ^ 1] = 0;
dc[i] = -1;
dt[i] = 0;
return ;
} int mid = (L + R) >> 1;
build(lson);
build(rson);
t[i] = update(t[ls], t[rs]);
dc[i] = -1;
dt[i] = 0;
} void cover(int i, int z){
dc[i] = z;
dt[i] = 0;
t[i].s[z] = t[i].len;
t[i].s[z ^ 1] = 0;
} void turn(int i, int z){
if (~z){
dc[i] ^= 1;
t[i].s[z] = 0;
t[i].s[z ^ 1] = t[i].len;
} else{
dt[i] ^= 1;
swap(t[i].s[0], t[i].s[1]);
}
} void pushdown(int i){
if (~dc[i]){
cover(ls, dc[i]);
cover(rs, dc[i]);
dc[i] = -1;
} if (dt[i]){
turn(ls, dc[ls]);
turn(rs, dc[rs]);
dt[i] = 0;
}
} void Turn(int i, int L, int R, int x, int y){
if (x <= L && R <= y){
turn(i, dc[i]);
return;
} pushdown(i);
int mid = (L + R) >> 1;
if (x <= mid) Turn(lson, x, y);
if (y > mid) Turn(rson, x, y);
t[i] = update(t[ls], t[rs]);
} int query(int i, int L, int R, int x, int y){
int ret = 0;
if (x <= L && R <= y) return t[i].s[1];
int mid = (L + R) >> 1;
pushdown(i);
if (x <= mid) ret += query(lson, x, y);
if (y > mid ) ret += query(rson, x, y);
return ret;
} int main(){ scanf("%d", &n);
rep(i, 2, n){
int x;
scanf("%d", &x);
v[x].push_back(i);
} dfs(1);
rep(i, 1, n) scanf("%d", a + i);
build(1, 1, n); for (scanf("%d", &q); q--; ){
char op[10]; int x;
scanf("%s%d", op, &x);
if (op[0] == 'g') printf("%d\n", query(1, 1, n, in[x], out[x]));
else Turn(1, 1, n, in[x], out[x]);
} return 0;
}

  

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

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

  2. Codeforces Round #225 (Div. 2) E. Propagating tree dfs序+-线段树

    题目链接:点击传送 E. Propagating tree time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

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

  4. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

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

  6. Codeforces 838B - Diverging Directions - [DFS序+线段树]

    题目链接:http://codeforces.com/problemset/problem/838/B You are given a directed weighted graph with n n ...

  7. Educational Codeforces Round 6 E dfs序+线段树

    题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...

  8. Codeforces 343D Water Tree(DFS序 + 线段树)

    题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...

  9. Codeforces 620E New Year Tree(DFS序 + 线段树)

    题目大概说给一棵树,树上结点都有颜色(1到60),进行下面两个操作:把某结点为根的子树染成某一颜色.询问某结点为根的子树有多少种颜色. 子树,显然DFS序,把子树结点映射到连续的区间.而注意到颜色60 ...

随机推荐

  1. day24 02 单继承(派生)

    day24 02 单继承(派生) 1.首先来看一个简单的例子 比如: 狗类的属性有:吃,喝,看门 鸟类的属性有:吃,喝,下蛋 看门和下蛋就是这两种动物不同的属性,而吃喝是两个共同的属性 以下代码实现了 ...

  2. Relu的缺点

    Relu不适合梯度过大的的输入 Relu是我们在训练网络时常用的激活函数之一(对我而言没有之一).然而最近发现Relu太脆弱了,经常由于输入的函数梯度过大导致网络参数更新后,神经元不再有激活功能.特别 ...

  3. java web项目(spring项目)中集成webservice ,实现对外开放接口

    什么是WebService?webService小示例 点此了解 下面进入正题: Javaweb项目(spring项目)中集成webservice ,实现对外开放接口步骤: 准备: 采用与spring ...

  4. IDEA常用快捷键(不全)

    这里使用的是默认的idea快捷键,如果修改了keymap为其他,那么不适用. 1.格式化代码:Ctrl+Alt+L(可能与QQ的冲突,建议QQ只保留方便的截图,皮) 2.在当前行最后添加分号,或自动补 ...

  5. JAVA基础篇—Servlet小结

    一.get请求和post请求的区别: 1.get请求是通过url传递参数,post请求是通过请求体传递参数的 2.get请求最多允许传递255个字符,对长度有限制,所以数据比较大的时候我们使用post ...

  6. Meteor Shower POJ - 3669 (bfs+优先队列)

    Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26455   Accepted: 6856 De ...

  7. vmware10下载地址

    https://download3.vmware.com/software/wkst/file/VMware-Workstation-Full-10.0.1-1379776.x86_64.bundle ...

  8. Linux学习-X Server 配置文件解析与设定

    X server 的配置 文件都是预设放置在 /etc/X11 目录下,而相关的显示模块或上面提到的总总模块,则主要放置在/usr/lib64/xorg/modules . 比较重要的是字型文件与芯片 ...

  9. goalng导出excel(csv格式)

    最近项目中有个小需求,需要将查询结果导出到excel.之间前java比较容易,使用POI很容易就能实现,查了下golang的文档,发现golang下边并没有导出excel的包,但是却有一个encodi ...

  10. 一个通用的Makefile框架

    先做一个简单的记录,后续有时间再慢慢完善补充细节. 先上一个整体图片: 其中,最重要的文件就是:program_template.mk. 下面是program_template.mk最重要的内容: $ ...