poj 3321 Apple Tree(一维树状数组)
题目:http://poj.org/problem?id=3321
题意:
苹果树上n个分叉,Q是询问,C是改变状态。。。。
开始的处理比较难,参考了一下大神的思路,构图成邻接表 并 用DFS编号
白书上一维树状数组模板:
int lowbit(int x)
{
return x&(-x);
}
void add(int x,int d) //c[]的下标要从 1开始。
{
while(x <= n)
{
c[x] += d;
x +=lowbit(x);
}
}
int sum(int x) //前x项的和。
{
int ret = ;
while(x > )
{
ret += c[x];
x -= lowbit(x);
}
return ret;
}
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxn = ;
int s[maxn],e[maxn],c[maxn],num;
int head[maxn],n,cnt;
bool vis[maxn];
struct node
{
int v,next;
}g[maxn]; void init()
{
int i;
memset(head,-,sizeof(head));
cnt = ; num = ;
for(i=; i<=n; i++)
{
c[i] = ;
vis[i] = false;
}
}
void add_e(int u,int v)
{
g[cnt].v = v;
g[cnt].next = head[u];
head[u] = cnt++;
}
int lowbit(int x)
{
return x&(-x);
} void add(int x,int d)
{
while(x <= n)
{
c[x] += d;
x +=lowbit(x);
}
}
int sum(int x)
{
int ret = ;
while(x > )
{
ret += c[x];
x -= lowbit(x);
}
return ret;
}
void dfs(int pos)
{
s[pos] = ++num;
for (int i = head[pos]; i != -; i = g[i].next)
{
int v = g[i].v;
dfs(v);
}
e[pos] = num;
} int main()
{
int x,y,i,q;
char op[];
scanf("%d",&n);
init();
for(i = ; i < n-; i++)
{
cin>>x>>y;
add_e(x,y); //邻接表构图
}
dfs(); //编号
for (i = ; i <= n; i++)
add(i,);
cin>>q;
while (q--)
{
cin>>op>>x;
if (op[] == 'Q')
printf("%d\n",sum(e[x]) - sum(s[x] - )); //输出连续的和
else //改变状态
{
if (!vis[x])
{
add(s[x],-);
vis[x] = true;
}
else
{
add(s[x],);
vis[x] = false;
}
}
}
return ;
}
//今天帮师兄做的笔试题,一个数组,求每个数前面比它大的个数
#include <iostream>
#include <cstring>
const int maxn = 2e5 + ;
using namespace std; int c[maxn], n = maxn; int lowbit(int x)
{
return x&(-x);
}
void add(int x,int d)
{
while(x <= n) //这里的n指c数组总数
{
c[x] += d;
x +=lowbit(x);
}
}
int sum(int x) //求c[1]到c[x]的和
{
int ret = ;
while(x > )
{
ret += c[x];
x -= lowbit(x);
}
return ret;
} int main()
{
int i, t, n1;
while(cin>>n1)
{
memset(c, , sizeof(c));
for(i = ; i <= n1; i++) //c数组从1开始
{
cin>>t;
add(t, );
if(i != n1)
cout<<sum(maxn-)-sum(t)<<" ";
else
cout<<sum(maxn-)-sum(t)<<endl;
}
}
return ;
}
poj 3321 Apple Tree(一维树状数组)的更多相关文章
- 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 (DFS + 树状数组)
题意: 一棵苹果树有N个分叉,编号1---N(根的编号为1),每个分叉只能有一颗苹果或者没有苹果. 现在有两种操作: 1.某个分叉上的苹果从有变无或者从无边有. 2.需要统计以某个分叉为根节点时,它的 ...
- POJ 3321 Apple Tree(树状数组)
点我看题目 题意 : 大概是说一颗树有n个分岔,然后给你n-1对关系,标明分岔u和分岔v是有边连着的,然后给你两个指令,让你在Q出现的时候按照要求输出. 思路 :典型的树状数组.但是因为没有弄好数组 ...
- POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)
id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...
- (简单) POJ 3321 Apple Tree,树链剖分+树状数组。
Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow ...
- Apple Tree POJ - 3321 dfs序列构造树状数组(好题)
There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. ...
- POJ3321 Apple Tree (树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16180 Accepted: 4836 Descr ...
- NYOJ 231 Apple Tree (树状数组)
题目链接 描述 There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in t ...
随机推荐
- 安装 php 转
一 安装 php 命令: sudo apt-get install libapache2-mod-php5 php5 出现了如下错误: 按照方案一 解决了此问题. 一下 from http://w ...
- 1078. Hashing (25)
时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of this problem is simp ...
- javascript看你能够做对几题
http://ourjs.com/detail/52fb82e13bd19c4814000001
- 用上新的电脑装上了VS2013了
今天老魏终于把配置好的电脑拿回来了,16G的内存,I7 4770CPU.这回啊,老魏终于可以舍弃我的本本了,装上了SQL Server,虚拟机等等运行高内存的程序,感觉就是爽.明天老魏就可以用VS20 ...
- HDU 3487 Splay
给定两种操作,一种是把一个数列的某一段切下来插到剩余数列的某一个位置上. 一种是翻转操作,把数列的某一段进行翻转. 都是Splay的基本操作.标准的Rotateto调整出 [a,b]区间.然后对[a, ...
- 使用struts的模型驱动注意的问题
注意实体对象的属性命名一定要规范, 例如: private String fName; 添加时模型驱动取不到值 private String fname; 这个可以
- 在scrollView中使用pageControl
在scrollView中使用pageControl 要这样才能效果好,合理而且人性化 -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ if ...
- Contest2037 - CSU Monthly 2013 Oct (Problem J: Scholarship)
http://acm.csu.edu.cn/OnlineJudge/problem.php?cid=2037&pid=9 [题解]: 这题卡了一下,卡在负数的情况,负数输出 0 这题主要找到一 ...
- uva 10791
还算比较水的一个数学题 求因子的最小和 总是用小的数去除 注意特判 是用int不行哦........ #include <cstdio> #include <cmath> ...
- 宏 #,##,_ _VA_ARGS_ _
宏里面使用: 一.# 转为字符串 #define PSQR(x) printf("the square of" #x "is %d.\n",(x)*(x)) ...