树状数组 || POJ 3321 Apple Tree
一道dfs序+树状数组的题
因为并没有get到dfs序以及对树状数组也不熟练卡了很久orz
dfs序:
in和out是时间戳
dfs序可以将树转化成为一个序列,满足区间 -> 子树
然后就可以用树状数组之类的维护序列的东东来维护了
int idx = ;
void dfs(int u, int fa)
{
seq[++idx] = u;
in[u] = idx;
for(int i = head[u];i;i = nxt[i])
{
int v = l[i];
if(v != fa)
{
dfs(v, u);
}
}
out[u] = idx;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
const int SZ = ;
const int INF = 1e9+;
int head[SZ], nxt[SZ], l[SZ], tot = ;
int in[SZ], out[SZ], seq[SZ], a[SZ];
void build(int f, int t)
{
l[++tot] = t;
nxt[tot] = head[f];
head[f] = tot;
}
int idx = ;
void dfs(int u, int fa)
{
seq[++idx] = u;
in[u] = idx;
for(int i = head[u];i;i = nxt[i])
{
int v = l[i];
if(v != fa)
{
dfs(v, u);
}
}
out[u] = idx;
}
int d[SZ], n;
void add(int i, int x)
{
for(; i <= n; i += (i & -i))
d[i] += x;
}
int get(int i)
{
int ans = ;
for(;i;i -= (i & -i))
ans += d[i];
return ans;
}
int query(int l, int r)
{
return get(r) - get(l - );
}
int main()
{
scanf("%d", &n);
for(int i = ; i < n-; i++)
{
int x, y;
scanf("%d %d", &x, &y);
build(x, y);
build(y, x);
}
dfs(, );
/*for(int i = 0; i < 11; i++) printf("%d ", in[i]); printf("\n");
for(int i = 0; i < 11; i++) printf("%d ", out[i]); printf("\n");
for(int i = 0; i < 25; i++) printf("%d ", seq[i]); printf("\n");*/
int m;
scanf("%d", &m);
for(int i = ; i <= n; i++)
add(i, ), a[i] = ;
while(m--)
{
char op;
int x;
scanf("%c", &op);
scanf("%c", &op);
scanf("%d", &x);
if(op == 'Q')
printf("%d\n", query(in[x], out[x]));
if(op == 'C')
{
if(a[in[x]]) {a[in[x]] = ; add(in[x], -); }
else {a[in[x]] = ; add(in[x], ); }
}
}
return ;
}
树状数组 || POJ 3321 Apple Tree的更多相关文章
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)
id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...
- 树状数组(Binary Indexed Tree,BIT)
树状数组(Binary Indexed Tree) 前面几篇文章我们分享的都是关于区间求和问题的几种解决方案,同时也介绍了线段树这样的数据结构,我们从中可以体会到合理解决方案带来的便利,对于大部分区间 ...
- 树状数组(Binary Indexed Tree)
树状数组(Binary Indexed Tree,BIT) 是能够完成下述操作的数据结构. 给一个初始值全为 0 的数列 a1, a2, ..., an (1)给定 i,计算 a1+a2+...+ai ...
- POJ 3321 Apple Tree 【树状数组+建树】
题目链接:http://poj.org/problem?id=3321 Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submiss ...
- poj 3321:Apple Tree(树状数组,提高题)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18623 Accepted: 5629 Descr ...
- (简单) POJ 3321 Apple Tree,树链剖分+树状数组。
Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow ...
- POJ 3321 Apple Tree(树状数组)
Apple Tree Time Limit: 2000MS Memory Lim ...
- POJ 3321 Apple Tree (树状数组+dfs序)
题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...
随机推荐
- 021--python装饰器
一.装饰器含义 装饰器本质就是函数,为其它函数添加附加功能 二.装饰器原则 1.不修改被修饰函数的代码 2.不修改被修饰函数的调用方式 三.装饰器知识 装饰器 = 高阶函数 + 函数嵌套 + 闭包 四 ...
- html5盒子模型
相关博客: Flex 布局教程:语法篇:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html Flex 布局教程:实例篇:http://ww ...
- hdoj5003【wa水】
蜜汁wa,蜜汁wa,少了个\n------ #include<bits/stdc++.h> using namespace std; typedef long long LL; typed ...
- hdoj1027【STL系列。。。?】
这个太夸张了...感觉是有别的方法,但是觉得再说吧...以后碰到全排列应该也是用STL嗨的吧...嗯,,,就是这样的....?再说,再说.. 还有杭电支持c艹11,很棒 #include <bi ...
- js的NaN变量
js中,我们经常在parseInt函数的时候遇到NaN变量,这个变量到底是什么呢? w3c上这样解释: NaN 属性是代表非数字值的特殊值.该属性用于指示某个值不是数字.可以把 Number 对象设置 ...
- IT兄弟连 JavaWeb教程 转发和重定向的区别
转发是在服务器端完成的:而重定向是在客户端完成的. 转发的速度快:而重定向的速度慢. 转发的是同一次请求:而重定向是两次不同的请求. 转发不会执行转发后的代码:而重定向会执行重定向后的代码. 转发地址 ...
- PJzhang:centos7上LNMP方式安装dvwa漏洞测试环境
猫宁!!! 参考链接:https://www.jianshu.com/p/5491ce5bfbac https://www.cnblogs.com/wujuntian/p/8183952.html h ...
- tcp聊天交互
#****setver端 import socket sk = socket.socket() adress = ('127.0.0.1', 8032) sk.bind(adress) sk.list ...
- [已读]Sass与Compass实战
介绍了Sass基础语法与Compass框架,这个网上参考文档就OK了,另外介绍了compass生成图片精灵和相应的css,貌似现在单纯用sass和compass的挺少,要不grunt,要不FIS,而g ...
- 前端打印console
很多时候,我们都想知道,是否已经选中或得到数据时,我们可以利用console 打印出来.console有几种方式使用.具体有: console.log($scope.getParkId); conso ...