题目大意:对树进行m次操作,有两类操作,一种是改变一个点的权值(将0变为1,1变为0),另一种为查询以x为根节点的子树点权值之和,开始时所有点权值为1。

分析:

对树进行dfs,将树变为序列,记录每个点进栈出栈的时间戳作为其对应区间的左右端点,那么其子节点对应区间必在该区间内,对这段区间求和即可,用树状数组进行修改与求和。

代码:

program apple;
type
point=^node;
node=record
x:longint; next:point;
end;
var
a:array[..]of point;
bit:array[..]of longint;
b,v1,v2:array[..]of longint;
g:array[..]of boolean;
n,i,m,d,x,y:longint; c:char;
procedure add(x,y:longint);
var p:point;
begin
new(p); p^.x:=y; p^.next:=a[x]; a[x]:=p;
end;
procedure dfs(x:longint);
var p:point;
begin
inc(d); v1[x]:=d; g[x]:=true; new(p); p:=a[x];
while p<>nil do
begin
if g[p^.x]=false then dfs(p^.x); p:=p^.next;
end;
inc(d); v2[x]:=d;
end;
procedure change(x:longint);
var i,y:longint;
begin
y:=x;
if b[x]= then i:= else i:=-;
x:=v1[x];
while (x<=*n) do
begin inc(bit[x],i); inc(x,x and (-x));end;
b[y]:=-b[y];
end;
function query(x:longint):longint;
var s:longint;
begin
s:=;
while x> do
begin inc(s,bit[x]); dec(x,x and (-x)); end;
exit(s);
end;
begin
readln(n);
for i:= to n- do
begin
readln(x,y);
add(x,y); add(y,x);
end;
d:=; dfs();
for i:= to n do change(i);
readln(m);
for i:= to m do
begin
readln(c,x);
if c='C' then change(x)
else writeln(query(v2[x])-query(v1[x]-));
end;
end.

POJ 3321:Apple Tree(dfs序+树状数组)的更多相关文章

  1. POJ 3321 Apple Tree DFS序 + 树状数组

    多次修改一棵树节点的值,或者询问当前这个节点的子树所有节点权值总和. 首先预处理出DFS序L[i]和R[i] 把问题转化为区间查询总和问题.单点修改,区间查询,树状数组即可. 注意修改的时候也要按照d ...

  2. [poj3321]Apple Tree(dfs序+树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26762   Accepted: 7947 Descr ...

  3. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  4. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  5. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  6. POJ 3321 Apple Tree DFS序+fenwick

    题目大意:有一颗长满苹果的苹果树,有两个操作. 1.询问以一个点为根的子树中有多少个苹果. 2.看看一个点有没有苹果,假设没有苹果.那么那里就立即长出一个苹果(= =!):否则就把那个苹果摘下来. 思 ...

  7. POJ3321Apple Tree Dfs序 树状数组

    出自——博客园-zhouzhendong ~去博客园看该题解~ 题目 POJ3321 Apple Tree 题意概括 有一颗01树,以结点1为树根,一开始所有的结点权值都是1,有两种操作: 1.改变其 ...

  8. [Split The Tree][dfs序+树状数组求区间数的种数]

    Split The Tree 时间限制: 1 Sec  内存限制: 128 MB提交: 46  解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 You are given ...

  9. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

  10. Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs序+树状数组

    D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

随机推荐

  1. C语言中的异常处理机制

    #define try if(!setjmp(Jump_Buffer)) 返回try现场后重新执行判断,所以有两次执行. http://blog.csdn.net/tian_dao_chou_qin/ ...

  2. vue组件 $children,$refs,$parent的使用

    如果项目很大,组件很多,怎么样才能准确的.快速的寻找到我们想要的组件了?? 1)$refs 首先你的给子组件做标记.demo :<firstchild ref="one"&g ...

  3. pymysql 简单操作数据库

    #!/usr/bin/env python #-*- coding:utf-8 -*- # author:leo # datetime:2019/4/24 15:22 # software: PyCh ...

  4. java设计模式——建造者模式

    一. 定义与类型 定义:将一个复杂对象的构建与它的表示分离,使用同样的构建过程可以创建不同的表示 用户只需制定需要建造的类型就可以得到它们,建造过程以及细节不需要知道 类型:创建型 建造者模式与工厂模 ...

  5. Redis学习记录(一)

    在学习Redis之前,要知道什么是NoSQL? 1.NoSQL 1.1. 什么是NoSQL NoSQL(NoSQL = Not Only SQL),表示“不仅仅是SQL”,泛指非关系型数据库. 1.2 ...

  6. python模块;opencv安装

    http://www.lfd.uci.edu/~gohlke/pythonlibs/ 1. 步骤1. 下载Python2.73, 安装, 并配置Python环境变量:".\Program F ...

  7. 零基础快速入门SpringBoot2.0教程 (三)

    一.SpringBoot Starter讲解 简介:介绍什么是SpringBoot Starter和主要作用 1.官网地址:https://docs.spring.io/spring-boot/doc ...

  8. java算法面试题:排序都有哪几种方法?请列举。用JAVA实现一个快速排序。选择冒泡快速集合至少4种方法排序

    package com.swift; import java.util.ArrayList; import java.util.Collections; import java.util.Compar ...

  9. Everything Be True-freecodecamp算法题目

    Everything Be True 1.要求 完善every函数,如果集合(collection)中的所有对象都存在对应的属性(pre),并且属性(pre)对应的值为真.函数返回ture.反之,返回 ...

  10. 垂直居中一个img

    { display:table-cell; text-align:center; vertical-align:middle; }