题目: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(一维树状数组)的更多相关文章

  1. POJ 3321 Apple Tree 【树状数组+建树】

    题目链接:http://poj.org/problem?id=3321 Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submiss ...

  2. poj 3321:Apple Tree(树状数组,提高题)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 5629 Descr ...

  3. POJ 3321 Apple Tree (DFS + 树状数组)

    题意: 一棵苹果树有N个分叉,编号1---N(根的编号为1),每个分叉只能有一颗苹果或者没有苹果. 现在有两种操作: 1.某个分叉上的苹果从有变无或者从无边有. 2.需要统计以某个分叉为根节点时,它的 ...

  4. POJ 3321 Apple Tree(树状数组)

    点我看题目  题意 : 大概是说一颗树有n个分岔,然后给你n-1对关系,标明分岔u和分岔v是有边连着的,然后给你两个指令,让你在Q出现的时候按照要求输出. 思路 :典型的树状数组.但是因为没有弄好数组 ...

  5. POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)

    id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...

  6. (简单) POJ 3321 Apple Tree,树链剖分+树状数组。

    Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow ...

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

  8. POJ3321 Apple Tree (树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16180   Accepted: 4836 Descr ...

  9. NYOJ 231 Apple Tree (树状数组)

    题目链接 描述 There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in t ...

随机推荐

  1. android 获取手机号

    android 获取手机号码,由于运营商的管理方式的不同,所以获取手机号码的方式也可能完全相同.现在很多运营商并不会把手机号码存入sim卡中. 最简单的,比如说中国移动并不将手机号保存在sim卡中,只 ...

  2. CentOS6.5下 yum安装LAMP

    CentOS下yum安装LAMP   1. 用yum安装Apache,Mysql,PHP. 1.1安装Apache yum install httpd httpd-devel 安装完成后,用/etc/ ...

  3. Catalyst揭秘 Day4 analyzer解析

    Catalyst揭秘 Day4 analyzer解析 今天继续解析catalyst,主要讲一下analyzer,在sql语句的处理流程中,analyzer是在sqlparse的基础上,把unresol ...

  4. Linux恢复删除文件

    一.介绍extundelete 1.extundelete的文件恢复工具,该工具最给力的一点就是支持ext3/ext4双格式分区恢复. 2. 在实际线上恢复过程中,切勿将extundelete安装到你 ...

  5. Linux vi 中搜索关键字

    当你用vi打开一个文件后,因为文件太长,如何才能找到你所要查找的关键字呢? 在vi里可没有菜单-〉查找 不过没关系,可以在命令模式下敲斜杆( / )这时在状态栏(也就是屏幕左下脚)就出现了 “/” 然 ...

  6. CR0,CR3寄存器

    驱动在hook系统函数的时候通常要将只读属性暂时的屏蔽掉,主要有三种方法 1.修改CR0寄存器的WP位,使只读属性失效(这是网上用的最多的方法),切忌使用完之后立马修改回来 2.只读的虚拟地址,通过C ...

  7. hadoop可能遇到的问题

    1.hadoop运行的原理? 2.mapreduce的原理? 3.HDFS存储的机制? 4.举一个简单的例子说明mapreduce是怎么来运行的 ? 5.面试的人给你出一些问题,让你用mapreduc ...

  8. go语言实现的目录共享程序

    其实程序很小,只不过是想写点东西了.后天晚上要回学校考试了,转眼已经出来了69天了,2个月多一点.工资加上老妈赞助的钱,不知道能不能买台电脑,作为程序员一直用着i3-3217u实在难受.回去找同学拷点 ...

  9. 1057: [ZJOI2007]棋盘制作 - BZOJ

    Description 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑白相间的方阵,对应八八六十四卦,黑白对应阴 ...

  10. SQL SERVER数据导入

    我的博客已好久没有文字方面的记载了,好歹昨天已经结束软件设计师的考试了,今天怎么说也需要锻炼自己的写作能力.不然真怕自己又像上一年一样,一停就一年多了. 想好好学习数据库(SQL SERVER)方面的 ...