POJ3321---Apple Tree(树状数组)

对数周期天线hfss建模_Ansoft HFSS 在设计对数周期天线时的仿真方法-HFSS教程 AnsoftHFSS在设计对数周期天线时的仿真方法文章来源: ANSYS 2011中国用户大会优秀论文 录入: mweda.com 点击数:【摘要】 本文通过 ANSOFT HFSS 设计了一个对数周期天线,在仿真分析时,发现随着求解频率的不同,天线的求解结果差别较大,求解误差较大。通过在 HFSS 中尝试不同的求解设置方法, 最终通过将天线模型剖分网格最大长度限定在 1/50λ的... 阅读详情

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

dfs改时间戳+树状数组

/*************************************************************************
    > File Name: poj3321.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年03月05日 星期四 16时17分37秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int N = 100100;
int tree[N];
int l[N];
int r[N];
int head[N];
int tot;
struct node
{
    int next;
    int to;
}edge[N << 1];

void addedge (int from, int to)
{
    edge[tot].to = to;
    edge[tot].next = head[from];
    head[from] = tot++;
}

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

void add (int x, int val)
{
    for (int i = x; i <= N; i += lowbit (i))
    {
        tree[i] += val;
    }
}

int sum (int x)
{
    int ans = 0;
    for (int i = x; i; i -= lowbit (i))
    {
        ans += tree[i];
    }
    return ans;
}

void dfs (int u, int &cnt, int fa)
{
    l[u] = ++cnt;
    for (int i = head[u]; ~i; i = edge[i].next)
    {
        int v = edge[i].to;
        if (v == fa)
        {
            continue;
        }
        dfs (v, cnt, u);
    }
    r[u] = cnt;
}

int main ()
{
    int n;
    while (~scanf("%d", &n))
    {
        int u, v;
        memset (tree, 0, sizeof(tree));
        memset (head, -1, sizeof(head));
        tot = 0;
        for (int i = 1; i <= n - 1; ++i)
        {
            scanf("%d%d", &u, &v);
            addedge (u, v);
            addedge (v, u);
        }
        int cnt = 0;
        dfs (1, cnt, -1);
        for (int i = 1; i <= n; ++i)
        {
            add (l[i], 1);
        }
        int m;
        char str[3];
        scanf("%d", &m);
        while (m--)
        {
            scanf("%s%d", str, &u);
            if (str[0] == 'Q')
            {
                printf("%d\n", sum (r[u]) - sum(l[u] - 1));
            }   
            else
            {
                int sta = sum (l[u]) - sum (l[u] - 1);
                if (sta)
                {
                    add (l[u], -1);
                }
                else
                {
                    add (l[u], 1);
                }
            }
        }
    }
    return 0;
}
磁感应强度B、磁场强度H、矢量磁位A的study 学完电磁场很久了但是对电磁场中磁感应强度B⃗\vec{B}B、磁场强度HHH、矢量磁位A⃗\vec{A}A,这几个变量所表征的含义一直很模糊,所以就总结了一下 阅读详情

相关推荐

在中国大陆使用 LiveKit 集成阿里云语音交互服务

通过集成阿里云的语音和模型服务,我们可以快速构建出适合中国用户的 LiveKit 智能语音应用。LiveKit Agents 框架的模块化设计使得替换底层服务变得异常简单,只需要更换相应的插件实例即可。使用 WebSocket 版本的阿里云 ASR 接口以获得更低的延迟。利用通义千问的 Function Calling 能力对接智能家居或企业内部 API。结合 LiveKit 的视频处理能力,打造具备视觉交互的数字人。希望这篇博客能为你在实时音视频与 AI 结合的探索之路上提供帮助!

星空漫野 1245

【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

livox mid360 + fast-livo

更改livox_ros_driver2/launch_ROS1/rviz_MID360.launch文件内参数。更改livox_ros_driver2/launch_ROS1/msg_MID360.launch文件内参数。更改livox_ros_driver2/config/MID360_config.json文件内参数。ip改为雷达的ip,192.168.1.1** 最后两位为雷达S/N码最后两位数字。雷达的广播码在雷达后面,可以在雷达机身的QR 码下面查看,共15位数。

z1872385的博客 4906

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

tomjobs的博客 690

Python + Google AI 自动修复 Sonar Bug 实践

在工作中总会遇到种种不期而至的需求,比如前段时间突然要修复所有 Sonar Bug,涉及各种琐碎的代码风格问题,包括但不限于语法不规范、废弃注释等问题。这些项目都已经持续开发几年了,Sonar 上的问题层出不穷,各种问题已经累积到了上万个,手动修复不但费时费力,而且过程异常枯燥。

子洋 Blog 2420

史上最全的Logisim运算器实验教程(基于华中科技大学计算机组成原理实验)

本博客是实现了Logisim的运算器设计,有具体的运算器设计的思路和运算器设计的电路,适合一步一步的按照思路做。

qq_62325622的博客 4万+

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

aaHua_ 1525

大华摄像头GB28181协议接入EasyCVR全流程(附常见错误排查)

本文详细阐述了大华摄像头通过GB28181协议接入EasyCVR视频管理平台的全流程,涵盖从核心原理、前期参数规划、Web界面配置到平台侧验证的完整步骤。重点针对网络端口、SIP参数配置等关键环节提供了深度排错指南,并总结了设备无法注册、在线不稳定、拉流失败等常见问题的排查思路与解决方案,旨在帮助安防工程师与系统集成商高效完成设备接入与调试。

weixin_29279153的博客 311

POJ3321 Apple Tree

树状数组+DFS   1.    题意:有一颗苹果树,每个每个节点都能长1个苹果,初始时,苹果是满的,每次可能读取的事件, a)     询问在某个节点为根的子树的苹果总 b)     摘取某节点的苹果 c)     在某个某个节点长出苹果 2.    首先自己写的时候想直接用父子关系来更新每个节点的苹果,结果超时了,后来看了题解才知道还能转化为树状数组,原来树状数组的使用也是很灵活的

A_Mango 464

MCU-内部flash模拟成EepRom(重新梳理)

通过fee写数据Data的过程中,数据会mapping一个虚拟地址,这个虚拟地址就是你的身份,在向flash写数据的过程中会写入很多相同虚拟地址的不同数据,在读取或切页的过程中,遍历数据只会认为最下面第一个的虚拟地址所持有的数据是有效数据,其他的都是老数据,可以丢弃。模拟成eeprom的两个eep_page写数据逻辑就是,先在eep_page0写数据,eep_page0写满后,将eep_page0的有效数据copy到eep_page1,之后擦eep_page0;

xiandang8023的博客 715

芒果YOLOv8改进16:检测头Head篇:即插即用|新颖轻量化非对称多级压缩LADH检测头,原创改进适配YOLOv8高效检测头,检测头新颖性改进,全网首发独一份

YOLOv8代码改进教程:即插即用|新颖轻量化非对称多级压缩LADH检测头🎈

包括YOLOv5、YOLOv7、YOLOv8等模型改进 5622

Apple Tree【树链剖分模板题】

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

既然弱小,就只顾变强就是了 624

银行监管报送系统系列介绍(二十一):银行监管报送系统总览

为保护法院案件当事人、利害关系人的合法权益,全面落实《最高人民法院、中国银行业监督管理委员会关于人民法院与银行业金融机构开展网络执行查控和联合信用惩戒工作的意见》(法【2014】266号),最高人民法院和银监会联合下发了《人民法院、银行业金融机构网络执行查控工作规范》(法【2015】321号),要求银行金融机构配合最高人民法院开展 “总对总”网络执行查控工作,并建立相应的业务系统,实现与法院的网络对接,协助法院完成对被执行人银行账户存款及其他金融资金的网络查询、冻结与扣划工作。

银行信息系统应用架构详解 2876

【树状数组】POJ 3321 Apple Tree

POJ 3321 DFS+树状数组

小do在努力 666

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

POJ 3321 Apple Tree【树状数组】

POJ 3321 Apple Tree【树状数组】

DafeiACMer 865

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

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

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

Bill_Yang_2016的博客 764

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

【树状数组】 poj3321 Apple Tree

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

Ted's 游乐园 842

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的博客 158

【poj3321】Apple Tree【树状数组】

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

ssllyr 1011

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!荣耀之路! 7137

【树状数组】 ◆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

在人间贩卖声音 228

【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),这是树

绫 648
上一篇: ZOJ3228---Searching the String(AC自动机)
下一篇: hdu2457---DNA repair(AC自动机+dp)
tokers
博客等级 码龄12年 71粉丝 700原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值