CF877E.Danil and a Part-time Job
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Danil decided to earn some money, so he had found a part-time job. The interview have went well, so now he is a light switcher.
Danil works in a rooted tree (undirected connected acyclic graph) with n vertices, vertex 1 is the root of the tree. There is a room in each vertex, light can be switched on or off in each room. Danil's duties include switching light in all rooms of the subtree of the vertex. It means that if light is switched on in some room of the subtree, he should switch it off. Otherwise, he should switch it on.
Unfortunately (or fortunately), Danil is very lazy. He knows that his boss is not going to personally check the work. Instead, he will send Danil tasks using Workforces personal messages.
There are two types of tasks:
- pow v describes a task to switch lights in the subtree of vertex v.
- get v describes a task to count the number of rooms in the subtree of v, in which the light is turned on. Danil should send the answer to his boss using Workforces messages.
A subtree of vertex v is a set of vertices for which the shortest path from them to the root passes through v. In particular, the vertex v is in the subtree of v.
Danil is not going to perform his duties. He asks you to write a program, which answers the boss instead of him.
丹尼尔决定赚点钱,因此找到了一份兼职工作。面试很顺利,现在他成了一名“电灯开关员”。
丹尼尔在一颗有根树(即无向、连通、无环图)上工作,该树共有 $ n $ 个顶点,其中顶点 $ 1 $ 是树的根。每个顶点处都有一间房间,每间房间内的灯可以打开或关闭。丹尼尔的职责是切换顶点 $ v $ 的子树中所有房间的灯:即若某房间的灯处于开启状态,则将其关闭;否则将其打开。
不幸(或幸运)的是,丹尼尔非常懒惰。他知道老板不会亲自检查他的工作,而是会通过 Workforces 私信系统向他发送任务。
任务共分为两类:
pow v:表示切换顶点 $ v $ 的子树中所有房间的灯;get v:表示统计顶点 $ v $ 的子树中有多少间房间的灯处于开启状态;丹尼尔需将该数目通过 Workforces 私信回复给老板。
顶点 $ v $ 的子树是指:从该顶点到根节点的最短路径必经过 $ v $ 的所有顶点构成的集合。特别地,顶点 $ v $ 自身也属于其子树。
丹尼尔并不打算履行自己的职责。他请你编写一个程序,代替他回答老板的问题。
输入格式
The first line contains a single integer n (1 ≤ n ≤ 200 000) — the number of vertices in the tree.
The second line contains n - 1 space-separated integers _p_2, _p_3, ..., p__n (1 ≤ p__i < i), where p__i is the ancestor of vertex i.
The third line contains n space-separated integers _t_1, _t_2, ..., t__n (0 ≤ t__i ≤ 1), where t__i is 1, if the light is turned on in vertex i and 0 otherwise.
The fourth line contains a single integer q (1 ≤ q ≤ 200 000) — the number of tasks.
The next q lines are get v or pow v (1 ≤ v ≤ n) — the tasks described above.
第一行包含一个整数 n(1≤n≤200000)——树中顶点的数量。
第二行包含 n−1 个用空格分隔的整数 p2,p3,…,pn(1≤pi<i),其中 pi 表示顶点 i 的祖先。
第三行包含 n 个用空格分隔的整数 t1,t2,…,tn(0≤ti≤1),其中若顶点 i 处的灯处于开启状态,则 ti=1;否则 ti=0。
第四行包含一个整数 q(1≤q≤200000)——任务的数量。
接下来的 q 行,每行是一个形如 get v 或 pow v 的任务(1≤v≤n),含义如上所述。
输出格式
For each task get v print the number of rooms in the subtree of v, in which the light is turned on.
对于每个任务,给定节点 v,请输出以 v 为根的子树中亮着灯的房间数量。
输入输出样例
输入#1
4 1 1 1 1 0 0 1 9 get 1 get 2 get 3 get 4 pow 1 get 1 get 2 get 3 get 4
输出#1
2 0 0 1 2 1 1 0
说明/提示
The tree before the task pow 1.
The tree after the task pow 1.
执行任务 pow 1 前的树。
执行任务 pow 1 后的树。
输入解题思路,AI测评打分。不知道怎么写?