【poj 3321】Apple Tree 题意&题解&代码(C++)
题目链接:
http://poj.org/problem?id=3321
题意:
给你一颗n个节点的树,每个节点开始有一个苹果,然后m次修改,每次修改使得某个节点的苹果改变,有变成没有,没有变成有。询问的是某个节点及其子树的苹果数目。
题解:
dfs序之后,单点修改区间查询,线段树与树状数组都可以。
普通的vector在这道题中迷之超时,加了个 typedef 后就不超时了,真是神奇,手写边表的话应该没什么问题。
代码:
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<vector>
#define lson (id*2)
#define rson (id*2+1)
#define maxn (100005)
using namespace std;
int n,in[maxn],out[maxn],m,tot;
//神奇的定义方法
typedef vector<int> ve_int;
vector<ve_int> lin(100005);
void dfs(int x,int f)
{tot++;in[x]=tot;for (int i=0;i<lin[x].size();i++)if (lin[x][i]!=f)dfs(lin[x][i],x);out[x]=tot;
}
char p[3];
int tr[4*maxn];
void pushup(int id)
{tr[id]=tr[lson]+tr[rson];
}
void build(int id,int l,int r)
{if (l>r) return ;if (l==r) {tr[id]=1;return ;}int mid=(l+r)/2;build(lson,l,mid);build(rson,mid+1,r);pushup(id);
}
void exc(int id,int l,int r,int x)
{if (l>r) return ;if (l==r&&r==x){if (tr[id]==1) tr[id]=0;else tr[id]=1;return ;}int mid=(l+r)/2;if (x<=mid) exc(lson,l,mid,x);if (x>=mid+1) exc(rson,mid+1,r,x);pushup(id);
}
int ans;
void query(int id,int l,int r,int L,int R)
{if (l>r || L>r || R<l) return ;if (l>=L && r<=R) {ans+=tr[id];return ;}int mid=(l+r)/2;if (L<=mid) query(lson,l,mid,L,R);if (R>=mid+1) query(rson,mid+1,r,L,R);pushup(id);
}
int main()
{scanf("%d",&n);for (int i=1;i<=n-1;i++){int tmpa,tmpb;scanf("%d%d",&tmpa,&tmpb);lin[tmpa].push_back(tmpb);lin[tmpb].push_back(tmpa);}dfs(1,1);build(1,1,tot);scanf("%d",&m);for (int i=1;i<=m;i++){int tmp;scanf("%s",p);scanf("%d",&tmp);if (p[0]=='Q'){ans=0;//cout<<in[tmp]<<' '<<out[tmp]<<endl;query(1,1,tot,in[tmp],out[tmp]);printf("%d\n",ans);}elseexc(1,1,tot,in[tmp]);}
}
【poj 3321】Apple Tree 题意&题解&代码(C++)相关推荐
- 【线段树_DFS序】POJ 3321 Apple Tree
POJ 3321 Apple Tree 题意:给出一个N个结点的无根树.但是要将1看作根,也相当于是一个有根树.最初每个结点都有一个苹果.有两种操作,Q x: 以x为根的子树上有多少个苹果:C x: ...
- POJ 3321 Apple Tree 题解
POJ 3321 Apple Tree 题解 POJ 3321 题目 There is an apple tree outside of kaka's house. Every autumn, a l ...
- POJ 3321 Apple Tree【树状数组】
题目 There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tr ...
- poj 3321 Apple Tree(dfs序+树状数组求和模型)
题目链接:http://poj.org/problem?id=3321 解题思路: 先dfs求出序列,将子树转化到dfs序列的区间内,接下来就是简单的树状数组求和模型了.水题. #include< ...
- poj 3321 Apple Tree
树状数组 题意:一个树,以树枝连接两个点的形式给出,固定以1为整棵树的根.苹果长在树的节点上,节点上只可能0或1个苹果,一开始每个节点都有1个苹果 有两种操作,C表示更改某个节点的苹果数,0变1,1变 ...
- poj 3321 Apple Tree 树状数组
http://poj.org/problem?id=3321 一棵树,开始时每个结点都有一个苹果,输入C x表示更新x结点,若x结点有苹果,把该结点苹果摘掉,若该节点无苹果,在该节点上增加一个新的苹果 ...
- poj 3321 Apple Trie
/*poj 3321 Apple Trie这道题的关键是如何将一个树建成一个一维数组利用树状数组来解题!可以利用dfs()来搞定,我们在对一个节点深搜后,所经过的节点的数目就是该节点的子树的数目所以我 ...
- 【poj 2488】A Knight's Journey 中文题意题解代码(C++)
题目链接: http://poj.org/problem?id=2488 中文题意: 给出一个p行q列的国际棋盘,马可以从任意一个格子开始走,问马能否不重复的走完所有的棋盘.如果可以,输出按字典序排列 ...
- 【POJ - 3321】 Apple Tree(dfs序 + 线段树维护 或 dfs序 + 树状数组维护)
题干: There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the t ...
最新文章
- 通过案例对 spark streaming 透彻理解三板斧之一: spark streaming 另类实验
- 一步一步将自己的代码转换为观察者模式
- 中国广电设备行业十四五运营模式与投资机遇研究报告2022版
- 报名中 | 微软首席人工智能科学家邀你参加一场AI领域的私密聚会
- python算法与数据结构-插入排序算法(34)
- NLP预训练家族 | Text-To-Text范式统一NLP任务
- 【Web安全笔记】之【11.0 其他】
- c语言程序设计专题实验bmp,第九章 报表设计---Visual Foxpro 数据库程序设计教案1-9章全...
- Android DocumentFile基本使用
- Android使用DragAndDrop拖拽效果实现宫格位置变换
- CTGU·OJ 快乐
- 蜗牛星际A单开启WOL功能
- html+svg原理,SVG无功补偿的原理是什么
- Linux进入multiwfn软件,Multiwfn入门tips
- 每日分享,一个不错的数据分析实战案例【数据分析可视化】MySQL+Python
- Java学习—初入Java
- 密码管理神器-1Password,安全与否?
- 关于微信小程序学习的一些心得
- Python - 返回 1:n 中所有可能的 k 个数的组合
- springboot执行批量插入_springboot2.1.8+elasticsearch7.3.2(三),添加文档,批量添加文档...