POJ 3321 Apple Tree(树状数组)

图的深搜+树状数组练习 POJ 3321(JAVA) NULL 博文链接:https://128kj.iteye.com/blog/1745671 立即下载

Apple Tree
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 20671 Accepted: 6257

Description

There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree.

The tree has N forks which are connected by branches. Kaka numbers the forks by 1 to N and the root is always numbered by 1. Apples will grow on the forks and two apple won't grow on the same fork. kaka wants to know how many apples are there in a sub-tree, for his study of the produce ability of the apple tree.

The trouble is that a new apple may grow on an empty fork some time and kaka may pick an apple from the tree for his dessert. Can you help kaka?

Input

The first line contains an integer N (N ≤ 100,000) , which is the number of the forks in the tree.
The following N - 1 lines each contain two integers u and v, which means fork u and fork v are connected by a branch.
The next line contains an integer M (M ≤ 100,000).
The following M lines each contain a message which is either
"C x" which means the existence of the apple on fork x has been changed. i.e. if there is an apple on the fork, then Kaka pick it; otherwise a new apple has grown on the empty fork.
or
"Q x" which means an inquiry for the number of apples in the sub-tree above the fork x, including the apple (if exists) on the fork x
Note the tree is full of apples at the beginning

Output

For every inquiry, output the correspond answer per line.

Sample Input

3
1 2
1 3
3
Q 1
C 2
Q 1

Sample Output

3
2

Source

POJ Monthly--2007.08.05, Huang, Jinsong


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef __int64 ll;



using namespace std;

#define INF 0x3f3f3f3f

#define N 100005

struct stud{          //坑爹的地方是用vector会超时
  int to,next;
}e[N*2];
int e_num;
int head[N];

int c[N];
int n;
int le[N],ri[N];
int num;
int a[N];
int vis[N];

inline int lowbit(int x)
{
    return x & (-x);
}

void dfs(int x)
{
    le[x]=++num;
    int i;
    for(i=head[x];i!=-1;i=e[i].next)
    {
        int to=e[i].to;
        dfs(to);
    }
   ri[x]=num;
}

void update(int x,int va)
{
    while(x<=n)
    {
        c[x]+=va;
        x+=lowbit(x);
    }
}

int sum(int x)
{
    int s=0;
    while(x>0)
    {
        s+=c[x];
        x-=lowbit(x);
    }
  return s;
}



int main()
{
    int i,j;
    scanf("%d",&n);
    {
        for(i=0;i<=n;i++)
             c[i]=0;
        int u,v;
        i=n-1;
        e_num=0;
        memset(head,-1,sizeof(head));
        while(i--)
        {
            scanf("%d%d",&u,&v);
            e[e_num].to=v;
            e[e_num].next=head[u];
            head[u]=e_num++;
        }

        for(i=1;i<=n;i++)
        {
            a[i]=1;
            update(i,1);
        }

        num=0;
        dfs(1);
        scanf("%d",&u);

        char ch[10];
        while(u--)
        {
          scanf("%s%d",ch,&v);
          if(ch[0]=='Q')
            {
                int lee=sum(le[v]-1);
                int rii=sum(ri[v]);
                printf("%d\n",rii-lee);
            }

          else
          {
              if(a[le[v]]) update(le[v],-1);
              else update(le[v],1);
              a[le[v]]=!a[le[v]];
          }

        }

    }

   return 0;
}




等时替代模型( Isotemporal Substitution Model) 文献阅读:Associations of sedentary time and physical activity with adverse health conditions: Outcome-wide analyses using isotemporal substitution model(该论文于2022年4月28日发表在柳叶刀子刊 EClinicalMedicine 上) 阅读详情

相关推荐

Windows安装ros2(Jazzy版本+win10/win11)-4.构建ros2结点

这个位置也是ros2搜索package以及搜索node的路径, 如果把构建好的可执行文件乱动位置的话就找不到节点了,当然如果复制一个可执行文件以及可执行文件-script.py文件, ros2也就会认为你有一个新的节点了,运行这个拷贝的可执行文件程序都是ok的。由于上一个文章给powershell7也增加了启动执行ros2的环境初始化文件local_setup.ps1, 所以我们也可以直接在vscode中运行节点, 此种方式没有经由ros2托管, 缺少其他ros2的附加能力。

weixin_48398592的博客 7113

poj3321(dfs序+树状数组)

题目链接:https://vjudge.net/problem/POJ-3321 题意:给一个普通树(不是二叉树),并且已经编号,每个结点为1或0,有两种操作,对单个结点修改和查询一个结点的子树的所有结点的值。 思路:操作为单点操作和区间查询,很适合用树状数组或线段树来解,但是这里的区间查询并不具备减法规则,一个结点的子数的所有结点编号也不连续。    所以需要我们自己来重新给结点编号。这里...

weixin_33851604的博客 454

告别手动输入!用Access批量导入307种土壤数据到SWAT模型(附完整MDB操作流程)

本文详细介绍了如何利用Microsoft Access批量导入307种土壤数据到SWAT模型,显著提升数据录入效率。通过解析usersoil表结构、提供Access操作指南和VBA自动化脚本,帮助环境工程师避免手动输入错误,实现高效数据管理。文章还包含性能优化技巧和常见错误解决方案,是SWAT模型用户的实用指南。

weixin_28366053的博客 324

poj 3321 Apple Tree

点击打开poj 3321 思路: 树状数组 分析: 1 题目给定一棵树,然后有n个树枝,每个树枝上面初始化有1个苹果,现在有m个操作 2 题目给定的是一棵树,我们应该考虑怎么把这棵树映射成一个数组,并且跟节点和儿子节点的编号是连续的。这一步我们可以利用dfs来做,利用时间撮的概念,第一次到达的时间作为起始的时间,第二次到达的时间为终点的时间,下图就是一个例子 、 3 这一题的时间...

我是传奇 282

从零构建NVENC编码器:一个游戏开发者的实战手册

本文详细介绍了如何从零构建NVENC编码器,特别针对游戏开发者提供实战指南。内容涵盖NVENC基础概念、DirectX纹理捕获、异步编码优化及高级特性配置,帮助开发者高效集成硬件编码技术到游戏引擎中,实现高性能游戏录制和直播功能。重点解析了NVIDIA专用编码芯片的优势与API使用技巧。

weixin_29292431的博客 365

如何优化Silvaco二极管仿真网格?这些参数设置技巧让你的结果更精确

本文详细介绍了如何优化Silvaco二极管仿真网格,通过精细调整网格参数提升仿真精度。重点解析了网格密度与计算精度的权衡、关键区域识别与网格强化策略,以及掺杂分布与网格适配技术,帮助工程师在半导体器件仿真中获得更准确的结果。

t8u9v0w1x的博客 660

POJ3321——树状数组_POJ树状数组初探

本文出自:http://blog.youkuaiyun.com/svitter

svitter的专栏 2068

ElectrolyteGPT:首个电池电解液逆向设计的生成式 AI 系统 —— 算法原理与复现解析

数据驱动的重要性:1100 万→100 万的精心筛选,比单纯增大模型更有效领域适配:fLine 表示法、正则分词、化学感知的数据增强(SMILES randomization)都是关键小模型的力量:6M 参数在特定化学空间的表现,远超通用大模型在相同任务上的零样本能力条件生成的极限:10 维条件导致模型崩溃,提示我们需要更好的条件编码策略(如 diffusion guidance、RLHF-style 偏好优化)未来方向配方实验验证。

ASKCOS博客 122

POJ3321 树状数组,多叉树转线性结构,经典基础题

1 题意 对于一个多叉树的,有两种操作,在某个点上摘一个苹果或者生成一个苹果(每个点要么有一个苹果要么有0个苹果);输出某个点作为root的子树上的苹果之和(包括root)。 2 分析 将多叉树用邻接表存储下来,通过DFS将多叉树转换为线性结构并且记录子树所在区间,然后构造树状数组,进行改动和查询。 3 #include #include #include #include u

他的博客 794

【漏洞复现】CVE-2024-44902 Thinkphp反序列化漏洞

Thinkphp v6.1.3至v8.0.4版本中存在反序列化漏洞,攻击者可利用此漏洞执行任意代码。

渗透之路,攻防之间皆学问 6705

9篇2章2节:GBD 数据库的数据申请详解

无论您是初入公共卫生领域的学生、资深的科研工作者,还是政策决策者,GBD数据库都将成为您数据分析与决策支持中不可或缺的重要工具。

MD Code 2598

【POJ 3321】 Apple Tree 树状数组 + DFS序(详解)

Description There is an apple tree outside of kaka’s house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree. The ...

待到山花烂漫时 657

Python 爬虫实战:爬虫与 ECharts 结合 —— 前端可视化展示

本文介绍了爬虫技术与ECharts可视化结合的完整实现流程。首先使用Python爬取京东商品数据(价格、评价数等),通过BeautifulSoup解析和pandas处理转换为JSON格式。然后构建前端页面,利用ECharts实现价格分布柱状图、价格-评价数散点图和店铺占比饼图三种可视化效果。文章详细解析了爬虫数据提取、格式转换和前端图表配置等关键技术点,并提供了完整的代码示例。最后提出了基于API接口、数据库存储等扩展方向,为数据采集到可视化展示提供了完整的解决方案。

2503_91057718的博客 630

【树状数组】POJ 3321 Apple Tree

POJ 3321 DFS+树状数组

小do在努力 666

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

POJ 3321 Apple Tree【树状数组】

POJ 3321 Apple Tree【树状数组】

DafeiACMer 863

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

[POJ3321] Apple Tree 苹果树 - 树状数组

题目描述有一棵N个结点的树,一开始每个结点上都有一个苹果,每次有两种操作:    (1)C x:如果x结点上有一个苹果,那么摘下它,否则x节点上会再生出一个苹果;    (2)Q x:询问以x结点为根的子树中苹果的个数; 你要对于每个Q操作,输出对应的答案。 输入格式第一行为一个数N,表示树的结点个数,默认以1为根; 接下来N-1行,每行两个数Ui,Vi,表示一条树边; 然后一行是一个数

Bill_Yang_2016的博客 763

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

【树状数组】 poj3321 Apple Tree

Apple Tree 题目:http://poj.org/problem?id=3321 题意:一棵有n个结点的树,初始时每个结点都有一个苹果,之后有m个操作,C操作:如果x结点有苹果,执行操作后x结点无苹果;如果x结点没有苹果,执行操作后x结点出现苹果。Q操作:问x结点及其子树上一共有多少苹果。 题解:单点更新,区间查询,一看就是线段树类型的题目。但是这个是树形结构,需要转换成线性的才能计

Ted's 游乐园 840

Poj3321 Apple Tree dfs序 + 树状数组

Poj3321 Apple Tree dfs序 + 树状数组 单点加 区间询问 1 #include <cstdio> 2 #include <cmath> 3 #include <cstdlib> 4 #include <cstring> 5 #include <algorithm> 6 #i...

weixin_33842328的博客 157

【poj3321】Apple Tree【树状数组】

描述 卡卡家门外有一棵苹果树。每年秋天,树上都会种很多苹果。卡卡非常喜欢苹果,因此他一直在精心培育大苹果树。 该树有N个分支,这些分支由分支连接。Kaka用1到N对叉进行编号,并且根始终用1编号。在叉上将生长苹果,而在同一叉上将不会生长两个苹果。卡卡想了解子树中有多少苹果,以便研究苹果树的生产能力。 问题在于,有时空叉上会长出一个新苹果,卡卡可能会从树上摘下一个苹果作为甜点。你能帮卡卡吗? 输入 第一行包含一个整数Ñ(Ñ ≤100,000),这是树中的叉的数量。 接下来的N -1行分别包含两个整数u和v,

ssllyr 1009

POJ 3321 Apple Tree(树状数组模板)

原题链接:POJ3321 解析:这题我按照郭炜老师的讲义来练习树状数组的模板,但是也遇到了一些问题: 如果对一些图或树来用树状数组解题,就需要给他每个节点一个从1到nCount的编号,因为树状数组只能从1~n求和 刚开始我用vector&lt;int&gt; G[maxn]来存图,但是超时了,改用vector&lt;vector&lt;int&gt; &gt; G(maxn)就过了,可见以后...

帅比王的博客 1万+

POJ3321 Apple tree【树状数组】

Apple tree【树状数组】

JA_yichao欢迎你 cnblogs账号:https://www.cnblogs.com/mayichao/ 261

POJ3321:Apple Tree(树状数组)

Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree.

ACM!荣耀之路! 7135

【树状数组】 ◆POJ 3321◆ Apple Tree

◆POJ 3321◆(2007 POJ Monthly Contest, Aug.) Apple Tree ◆题目◆ Description There is an apple tree outside of kaka’s house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple

Lucky_Glass的C++之旅 690

【POJ3321】Apple Tree【树状数组】

Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 44586 Accepted: 13153 Description There is an apple tree outside of kaka’s house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurtur

在人间贩卖声音 227

【poj3321】【树状数组】Apple Tree

传送门 Description There is an apple tree outside of kaka’s house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree. The tree has N forks which are connected by branches. Ka

蒟蒻的OI路 172

【POJ 3321】Apple Tree【树状数组&DFS】

苹果树 时限: 2000MS 内存限制: 65536K 描述 卡卡家门外有一棵苹果树。每年秋天,树上都会种很多苹果。卡卡非常喜欢苹果,因此他一直在精心培育大苹果树。 该树有N个分支,这些分支由分支连接。Kaka用1到N对叉进行编号,并且根始终用1编号。在叉上将生长苹果,而在同一叉上将不会生长两个苹果。卡卡想了解子树中有多少苹果,以便研究苹果树的生产能力。 问题在于,有时空叉上会长出一个新苹果,卡卡可能会从树上摘下一个苹果作为甜点。你能帮卡卡吗? 输入项 第一行包含一个整数Ñ(Ñ ≤100,000),这是树

绫 645
上一篇: CF# 149 D Coloring Brackets(区间dp)
下一篇: CSU 1563: Lexicography (数学计数问题)
左眼皮跳跳~
博客等级 码龄13年 17粉丝 400原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值