题目: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. Asp.net Response.Redirect with post data

    string url = String.Format("{0}://{1}/{2}", Request.Url.Scheme, Request.Url.Authority, &qu ...

  2. 怎么在spring官网上下载spring的jar包, 源代码和文档?

    现在 http://repo.spring.io/release/org/springframework/spring/第一种,简单粗暴直接 1 http://repo.springsource.or ...

  3. AngularJS快速开始

    Hello World! 开始学习AngularJS的一个好方法是创建经典应用程序“Hello World!”: 使用您喜爱的文本编辑器,创建一个HTML文件,例如:helloworld.html. ...

  4. OS/400相关介绍

    OS/400是IBM公司为其AS/400以及AS/400e系列商业计算机开发的操作系统,由于OS/400的设计充分考虑了AS/400的硬件设计,而且通常作为AS/400的一个基本组件被提供,因此几乎没 ...

  5. Qt+MinGW+OpenCV开发环境在win7系统下的搭建(最新20140423)

    1 搭建环境 (1)联想Y470笔记本电脑,win7操作系统 (2)Qt 5.2.1 Open Source :(Qt Online installer for Window(9MB),即下载页面最上 ...

  6. 《WPF程序设计指南》读书笔记——第4章 按钮与其他控件

    1.Button类 using System; using System.Windows; using System.Windows.Media; using System.Windows.Input ...

  7. 微信公众号jssdk使用的惨痛经历

    最近一直在做微信公众号开发,遇到个DT的问题: 大家都知道使用jssdk的时候开发人员必须在后台按照官方文档给定的规则生成签名,我前前个月就写好了这个测试demo页面,而且完全正常能用,像分享等这些功 ...

  8. SCRUM,一个采用迭代,增量方法来优化可预见控制风险

    Scrum是一个用于开发和维持复杂产品的框架,是一个增量的,迭代的开发过程.在这个框架中,整个开发过程是由若干个短的迭代周期组成,一个短的迭代周期称为一个Sprint,每个Sprint的建议长度是2到 ...

  9. 如何使用 XSD

    如何使用 XSD 一个简单的 XML 文档: 请看这个名为 "note.xml" 的 XML 文档: <?xml version="1.0"?> & ...

  10. eclipse(Version: Neon Release (4.6.0))安装hibernate tools

    这里需要说明的是,hibernate tools是jboss推出的. eclipse——>Eclipse Marketplace... 输入jboss-tools进行搜索 选择jboss too ...