poj3321 Apple Tree
2016-07-25 09:11
447 查看
dfs搜索记录每个节点的dfs序l,以及其子节点最大的dfs序r,dfs序在区间[l,r]之间的就是dfs序为l的节点的子孙节点。查询节点P的果实个数,即使查询P对应的dfs序区间的和。
要把用树状数组维护的区间上的某一点改为num,可以先记录每个位置的值,计算出增量,再update。
有点坑的是卡vector,TLE
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#define LL long long
#define maxn 110000
#define maxm 110000
using namespace std;
//vector<int>head[maxn];//超时
typedef vector<int>ve;
vector<ve>head(maxn);//1300MS
struct VOL
{
int l,r;
void set(int l,int r){this->l=l,this->r=r;}
}_hash[maxn];
int depth;
bool vis[maxn];
void dfs(int st)
{
int ldep=depth;
vector<int>&vec=head[st];
for(int i=0,len=vec.size();i<len;++i)
{
if(!vis[vec[i]])
{
depth++;
vis[vec[i]]=true;
dfs(vec[i]);
}
}
int rdep=depth;
_hash[st].set(ldep,rdep);
}
int tree[maxn];
int lowbit(int t)
{
return t&(-t);
}
int getsum(int end)
{
int sum=0;
while(end>0)
{
sum+=tree[end];
end-=lowbit(end);
}
return sum;
}
void update(int pos,int num,int n)
{
while(pos<=n)
{
tree[pos]+=num;
pos+=lowbit(pos);
}
return ;
}
int get(int pos)
{
return getsum(pos)-getsum(pos-1);
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<n;++i)
{
int a,b;
scanf("%d%d",&a,&b);
head[a].push_back(b);
head[b].push_back(a);
}
depth=1;
memset(vis,false,sizeof(vis));
vis[1]=true;
dfs(1);
memset(tree,0,sizeof(tree));
for(int i=1;i<=n;++i)
{
// cout<<_hash[i].l<<" "<<_hash[i].r<<endl;
update(i,1,n);
}
int m,pos;
scanf("%d",&m);
char op[10];
memset(vis,false,sizeof(vis));
for(int i=0;i<m;++i)
{
scanf("%s%d",op,&pos);
if(op[0]=='Q')
printf("%d\n",getsum(_hash[pos].r)-getsum(_hash[pos].l-1));
else
{
if(get(_hash[pos].l)==1)
update(_hash[pos].l,-1,n);
else
update(_hash[pos].l,1,n);
}
}
}
要把用树状数组维护的区间上的某一点改为num,可以先记录每个位置的值,计算出增量,再update。
有点坑的是卡vector,TLE
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#define LL long long
#define maxn 110000
#define maxm 110000
using namespace std;
//vector<int>head[maxn];//超时
typedef vector<int>ve;
vector<ve>head(maxn);//1300MS
struct VOL
{
int l,r;
void set(int l,int r){this->l=l,this->r=r;}
}_hash[maxn];
int depth;
bool vis[maxn];
void dfs(int st)
{
int ldep=depth;
vector<int>&vec=head[st];
for(int i=0,len=vec.size();i<len;++i)
{
if(!vis[vec[i]])
{
depth++;
vis[vec[i]]=true;
dfs(vec[i]);
}
}
int rdep=depth;
_hash[st].set(ldep,rdep);
}
int tree[maxn];
int lowbit(int t)
{
return t&(-t);
}
int getsum(int end)
{
int sum=0;
while(end>0)
{
sum+=tree[end];
end-=lowbit(end);
}
return sum;
}
void update(int pos,int num,int n)
{
while(pos<=n)
{
tree[pos]+=num;
pos+=lowbit(pos);
}
return ;
}
int get(int pos)
{
return getsum(pos)-getsum(pos-1);
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<n;++i)
{
int a,b;
scanf("%d%d",&a,&b);
head[a].push_back(b);
head[b].push_back(a);
}
depth=1;
memset(vis,false,sizeof(vis));
vis[1]=true;
dfs(1);
memset(tree,0,sizeof(tree));
for(int i=1;i<=n;++i)
{
// cout<<_hash[i].l<<" "<<_hash[i].r<<endl;
update(i,1,n);
}
int m,pos;
scanf("%d",&m);
char op[10];
memset(vis,false,sizeof(vis));
for(int i=0;i<m;++i)
{
scanf("%s%d",op,&pos);
if(op[0]=='Q')
printf("%d\n",getsum(_hash[pos].r)-getsum(_hash[pos].l-1));
else
{
if(get(_hash[pos].l)==1)
update(_hash[pos].l,-1,n);
else
update(_hash[pos].l,1,n);
}
}
}
相关文章推荐
- Android Studio 后台执行的定时操作
- 【转】移动端轮播插件,zepto轮播插件
- Android EditText 取消输入框弹出
- 关于actionMode 6.0适配问题
- cocos2d - JS Rect 相关
- Swift基础(十七)UITextView
- iOS中集成ijkplayer视频直播框架
- 做一款仿映客的直播App?看这篇就够了
- [置顶] 【微信平台】艺萌管家APP技术总结(一)——动态分页加载图片
- Android数据绑定框架DataBinding,堪称解决界面逻辑的黑科技
- android图片三级缓存的思路
- 关于IOS开发常见的循环引用问题
- Android ormlite更改数据库默认位置
- iOS里CALayer的使用
- Xcode各版本官方下载及百度云盘下载, Mac和IOS及Xcode版本历史. (2015-07-17 07:05:06)
- Android 开发之验证手机格式
- nagios 通过微信发送监控信息
- iOS开发75-leaks will report false positives while the environment variable NSZombiesEnable is active的解
- Cocos Creator 视频学习笔记
- CocosCreator上手