题目: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. CentOS平台下为Python添加MongoDB支持PyMongo

    下载PyMongo [root@leezhen ~]# wget https://pypi.python.org/packages/source/p/pymongo/pymongo-2.6.3.tar ...

  2. PHP的$_SERVER['HTTP_HOST']获取服务器地址功能详解

    uchome的index文件中的二级域名功能判断,使用了php的$_SERVER['HTTP_HOST'],开始对这个不是很了解,所以百度了一下,发现一篇帖子有点意思,转发过来做个记录. 在php中, ...

  3. php中封装的curl函数(抓取数据)

    介绍一个封闭好的函数,封闭了curl函数的常用步骤,方便抓取数据. 代码如下: <?php /** * 封闭好的 curl函数 * 用途:抓取数据 * edit by www.jbxue.com ...

  4. 在win7上建立本地FTP站点详细步骤

    一.安装FTP组件点击:控制面板—>程序和功能—>打开或关闭Windows功能. 勾选“FTP服务器”及“FTP服务”“FTP扩展性”,点击“确定”,安装FTP组件. 勾选Web管理工具的 ...

  5. UVA 725

    Description   Write a program that finds and displays all pairs of 5-digit numbers that between them ...

  6. 九度OJ - 题目1481:Is It A Tree?

    题目描述: A tree is a well-known data structure that is either empty (null, void, nothing) or is a set o ...

  7. 【学习总结】【多线程】 多线程概要 & GDC & NSOperation

    基本需要知道的 :  进程 :  简单点来说就是,操作系统中正在运行的一个应用程序,每个进程之间是独立的,每个进程均运行在受保护的内存空间内 线程 :  一个进程(进程)想执行任务,必须有线程(所以, ...

  8. [原创] zabbix学习之旅一:源码安装

    zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案.zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系统管理员快速定位/解决存 ...

  9. C# DataTable

    http://www.cnblogs.com/xun126/archive/2010/12/30/1921557.html http://msdn.microsoft.com/zh-cn/librar ...

  10. 1036: [ZJOI2008]树的统计Count - BZOJ

    Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. Q ...