题目: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. tomcat源码解读(1)–tomcat热部署实现原理

    tomcat的热部署实现原理:tomcat启动的时候会有启动一个线程每隔一段时间会去判断应用中加载的类是否发生变法(类总数的变化,类的修改),如果发生了变化就会把应用的启动的线程停止掉,清除引用,并且 ...

  2. 在windows服务器中,将MongoDB服务化。

    将mongodb在windows中服务化,就是将其注册成一个服务组件,并可以设置成,手动/自动 启动. 一般的我们都会在command窗口运行如下: d:\mongodb\bin>mongod ...

  3. sql新感悟(where 1 = 1)

    花了好久把YII框架看完发现一本很不错的书:SQL案例解析(清华大学出版社),看到一些比较有用的东西,感觉应该把他记录下来,看了好多页发现书中一直有 where 1=1,这样的语句,查过发现“wher ...

  4. 1090. Highest Price in Supply Chain (25)

    时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A supply chain is a network of r ...

  5. 2014年辛星完全解读Javascript第七节 数组和对象

    由于Javascript是脚本语言,因此,使用起来非常方便,数组的使用也是比较简单的,下面我们就主要介绍一下Javascript中数组的介绍,以及上一节中没有完成的对象的介绍. *********** ...

  6. Xcode常用设置

    1.设置主题和字体大小 2.设置显示代码行号

  7. Java 8 vs. Scala(一): Lambda表达式

    [编者按]虽然 Java 深得大量开发者喜爱,但是对比其他现代编程语言,其语法确实略显冗长.但是通过 Java8,直接利用 lambda 表达式就能编写出既可读又简洁的代码.作者 Hussachai ...

  8. Maven的Dependency怎么找?

    用了Maven,所需的JAR包就不能再像往常一样,自己找到并下载下来,用IDE导进去就完事了,Maven用了一个项目依赖(Dependency)的概念,用俗话说,就是我的项目需要用你这个jar包,就称 ...

  9. socket选项自带的TCP异常断开检测

    TCP异常断开是指在突然断电,直接拔网线等等情况下,如果通信双方没有进行数据发送通信等处理的时候,无法获知连接已经断开的情况. 在通常的情况下,为了使得socket通信不受操作系统的限制,需要自己在应 ...

  10. xcode 树形管理 cocos2d-x的资源

    把资源以目录的形式加入xcode中, 同时, 在加入时, 选择"Create Folder References for any  added folders", 而不是默认的&q ...