[题目链接]

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

[算法]

首先求出这棵树的DFS序

一棵子树的DFS序为连续的一段 , 根据这个性质 , 用线段树维护区间中1的个数即可

时间复杂度 : O(NlogN)

[代码]

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + ;
typedef long long ll;
typedef long double ld; struct edge
{
int to , nxt;
} e[MAXN << ]; int n , q , timer , tot;
int val[MAXN] , l[MAXN] , r[MAXN] , dfn[MAXN] , size[MAXN] , head[MAXN] , loc[MAXN] , par[MAXN]; struct Segment_Tree
{
struct Node
{
int l , r , cnt;
bool tag;
} a[MAXN << ];
inline void update(int index)
{
a[index].cnt = a[index << ].cnt + a[index << | ].cnt;
}
inline void build(int index , int l , int r)
{
a[index].l = l , a[index].r = r;
a[index].tag = false;
if (l == r)
{
a[index].cnt = (val[loc[l]] == );
return;
}
int mid = (l + r) >> ;
build(index << , l , mid);
build(index << | , mid + , r);
update(index);
}
inline void pushdown(int index)
{
int l = a[index].l , r = a[index].r;
int mid = (l + r) >> ;
if (a[index].tag)
{
a[index << ].cnt = (mid - l + ) - a[index << ].cnt;
a[index << | ].cnt = (r - mid) - a[index << | ].cnt;
a[index << ].tag ^= ;
a[index << | ].tag ^= ;
a[index].tag = false;
}
}
inline void modify(int index , int l , int r)
{
if (a[index].l == l && a[index].r == r)
{
a[index].cnt = (r - l + ) - a[index].cnt;
a[index].tag ^= ;
return;
}
pushdown(index);
int mid = (a[index].l + a[index].r) >> ;
if (mid >= r) modify(index << , l , r);
else if (mid + <= l) modify(index << | , l , r);
else
{
modify(index << , l , mid);
modify(index << | , mid + , r);
}
update(index);
}
inline int query(int index , int l , int r)
{
if (a[index].l == l && a[index].r == r)
return a[index].cnt;
pushdown(index);
int mid = (a[index].l + a[index].r) >> ;
if (mid >= r) return query(index << , l , r);
else if (mid + <= l) return query(index << | , l , r);
else return query(index << , l , mid) + query(index << | , mid + , r);
}
} SGT; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void addedge(int u , int v)
{
++tot;
e[tot] = (edge){v , head[u]};
head[u] = tot;
}
inline void dfs(int u)
{
l[u] = dfn[u] = ++timer;
loc[timer] = u;
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v == par[u]) continue;
dfs(v);
}
r[u] = timer;
} int main()
{ scanf("%d" , &n);
for (int i = ; i <= n; i++)
{
scanf("%d" , &par[i]);
addedge(par[i] , i);
}
for (int i = ; i <= n; i++) scanf("%d" , &val[i]);
dfs();
SGT.build( , , n);
scanf("%d" , &q);
while (q--)
{
char type[];
scanf("%s" , type);
if (type[] == 'p')
{
int u;
scanf("%d" , &u);
SGT.modify( , l[u] , r[u]);
} else
{
int u;
scanf("%d" , &u);
printf("%d\n" , SGT.query( , l[u] , r[u]));
}
} return ; }

[Codeforces 877E] Danil and a Part-time Job的更多相关文章

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

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

    题目链接   Danil and a Part-time Job 题意    给出一系列询问或者修改操作 $pow$ $x$表示把以$x$为根的子树的所有结点的状态取反($0$变$1$,$1$变$0$ ...

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

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

  5. CodeForces 877E DFS序+线段树

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

  6. codeforces 877e

    E. Danil and a Part-time Job time limit per test 2 seconds memory limit per test 256 megabytes input ...

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

  8. Codeforces Round #442 (Div. 2) Danil and a Part-time Job

    http://codeforces.com/contest/877/problem/E 真的菜的不行,自己敲一个模板,到处都是问题.哎 #include <bits/stdc++.h> u ...

  9. codeforces 877 E. Danil and a Part-time Job(线段树(dfs序))

    题目链接:http://codeforces.com/contest/877/problem/E 题解:显然一看就感觉要么树链剖分要么线段树+dfs序,题目要求的操作显然用线段树+dfs序就可以实现. ...

随机推荐

  1. jmeter源码编译

    转载:http://blog.csdn.net/wanglha/article/details/42004943 一.下载源码 git clone git://github.com/apache/jm ...

  2. java 图片加水印,设置透明度。说明非常具体

    package com.yidao.common; import java.awt.AlphaComposite; import java.awt.Graphics2D; import java.aw ...

  3. 百度 BAE 项目部署

    转载:http://www.cnblogs.com/shamoyuu/p/node_bae.html 百度有一个应用引擎,价格非常便宜,Java的tomcat每天4毛钱,node每天2毛钱,我以前在上 ...

  4. MySQL获得指定数据表中auto_increment自增id值的方法及实例

    http://kb.cnblogs.com/a/2357592/很多情况下,我们要提前用到当前某个表的auto_increment自增列id,可以通过执行sql语句来查询到这个id值. show ta ...

  5. 笔记08 WPF导航

    如何在winform中做导航,如何重定向界面,就产生了争执. 是用window还是Page还是UserControl? 先不管用啥.我们先比较一下各自的优缺点. 在WPF中使用导航,内容被组织在Pag ...

  6. webpack打包问题

    最近项目里需要替换一个logo,原先的logo打包后生成的静态文件里有对应的图片,替换了新的的图片打包后并没有生成相应的静态文件,两个图片都在同一个文件目录下,而且图片是直接引入并不会出现打包不到图片 ...

  7. PHP debug_backtrace() 函数

    PHP Error 和 Logging 函数 实例 生成 PHP backtrace: <?php function a($txt) { b("Glenn"); } func ...

  8. linux查看是否安装JDK(转载)

    rpm -qa |grep java 看是否安装了java echo $PATH 看环境变量是否配置了java路径 find / -name java 查找java文件 一般linux服务器会自带jd ...

  9. MonoTouch.Dialog简介

    新建一个Single View Application项目 添加程序集 MonoTouch.Dialog.dll引用 删除 MainStoryboard.storyboard 添加空类Task.cs ...

  10. uGUI动态加载控件位置错误(转自:https://www.cnblogs.com/mezero/p/4542939.html)

    最近在使用uGUI时遇到了一个问题,在此记录一下.在Canvas的Render Mode设置为Screen Space-Overlay模式时,动态加载控件是不会发生问题的.但是在Screen Spac ...