时间限制: 1 s
空间限制: 128000 KB
题目等级 : 钻石 Diamond
题解
题目描述 Description
在卡卡的房子外面,有一棵苹果树。每年的春天,树上总会结出很多的苹果。卡卡非常喜欢吃苹果,所以他一直都精心的呵护这棵苹果树。我们知道树是有很多分叉点的,苹果会长在枝条的分叉点上面,且不会有两个苹果结在一起。卡卡很想知道一个分叉点所代表的子树上所结的苹果的数目,以便研究苹果树哪些枝条的结果能力比较强。
卡卡所知道的是,每隔一些时间,某些分叉点上会结出一些苹果,但是卡卡所不知道的是,总会有一些调皮的小孩来树上摘走一些苹果。
于是我们定义两种操作:
C x
表示编号为x的分叉点的状态被改变(原来有苹果的话,就被摘掉,原来没有的话,就结出一个苹果)
G x
查询编号为x的分叉点所代表的子树中有多少个苹果
我们假定一开始的时候,树上全都是苹果,也包括作为根结点的分叉1。
输入描述 Input Description
第一行一个数N (n<=100000)
接下来n-1行,每行2个数u,v,表示分叉点u和分叉点v是直接相连的。
再接下来一行一个数M,(M<=100000)表示询问数
接下来M行,表示询问,询问的格式如题目所述Q x或者C x
输出描述 Output Description
对于每个Q x的询问,请输出相应的结果,每行输出一个
样例输入 Sample Input
3
1 2
1 3
3
Q 1
C 2
Q 1
样例输出 Sample Output
3
2
写在前面:为这不美好的世界献上祝福
思路:刚开始不知道用dfs序,因此不知道怎么搞线段树or树状数组
自己胡搞的90分代码,被一条100000长的链给卡死了:
#include "cstdio"
#include "cstring"
#include<iostream>
#include<algorithm>
using namespace std;
int tot,n,m,x,y;
int first[100010],father[100010],num[100010];
struct os
{
int fa,son,next;
}a[200010];
struct node
{
int root,sum;
}tree[400010];
inline void add(int x,int y)
{
a[++tot].fa=x;
a[tot].son=y;
a[tot].next=first[x];
first[x]=tot;
}
inline void build(int root)
{
int k=first[root];
tree[root].sum=1;
while (k)
{
if (!father[a[k].son])
{
father[a[k].son]=root;
build(a[k].son);
tree[root].sum+=tree[a[k].son].sum;
}
k=a[k].next;
}
}
inline void change(int root,int x)
{
if (root==0) return;
tree[root].sum+=x;
change(father[root],x);
}
inline int in()
{
int t=0;
char ch=getchar();
while (ch>'9'||ch<'0') ch=getchar();
while (ch>='0'&&ch<='9') t=t*10+ch-'0',ch=getchar();
return t;
}
main()
{
n=in();
for (int i=1;i<n;i++)
{
x=in();y=in();
add(x,y);
num[x]=num[y]=1;
add(y,x);
}
m=in();
father[1]=-1;
build(1);
for (int i=1;i<=m;i++)
{
char ch=getchar();
while (ch!='C'&&ch!='Q') ch=getchar();
x=in();
if (ch=='C')
{
num[x]=1-num[x];
if (num[x])change(x,1);
else change(x,-1);
}
else printf("%d\n",tree[x].sum);
}
}
100分dfs+线段树
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int tot,n,m,x,y,first[100010],l[200010],r[200010],sum,d[100010];
bool flag[100010];
struct os
{
int next,fa,son;
}a[200010];
struct node
{
int sum;
}tree[400010];
void add(int x,int y)
{
a[++tot].fa=x;
a[tot].son=y;
a[tot].next=first[x];
first[x]=tot;
}
void dfs(int x)
{
int k=first[x];
l[x]=++sum;
flag[x]=1;
while (k)
{
if (!flag[a[k].son]) dfs(a[k].son);
k=a[k].next;
}
r[x]=sum;
}
void build(int root,int begin,int end)
{
if (begin==end) {tree[root].sum=1;return;}
int mid=(begin+end)/2;
build(root*2,begin,mid);
build(root*2+1,mid+1,end);
tree[root].sum=tree[root*2].sum+tree[root*2+1].sum;
}
void update(int root,int begin,int end,int x,int num)
{
if (begin==end) {tree[root].sum+=num;return;}
int mid=(begin+end)/2;
if (mid>=l[x]) update(root*2,begin,mid,x,num);
else update(root*2+1,mid+1,end,x,num);
tree[root].sum=tree[root*2].sum+tree[root*2+1].sum;
}
int get(int root,int begin,int end,int x)
{
if (l[x]<=begin&&end<=r[x]) return tree[root].sum;
int mid=(begin+end)/2,ans=0;
if (mid>=l[x]) ans+=get(root*2,begin,mid,x);
if (mid<r[x]) ans+=get(root*2+1,mid+1,end,x);
return ans;
}
main()
{
scanf("%d",&n);
for (int i=1;i<n;i++)
scanf("%d%d",&x,&y),
add(x,y),
add(y,x);
dfs(1);
build(1,1,n);
scanf("%d",&m);
for (int i=1;i<=m;i++)
{
char ch=getchar();
while (ch!='C'&&ch!='Q') ch=getchar();
scanf("%d",&x);
if (ch=='Q') printf("%d\n",get(1,1,n,x));
else
{
flag[x]=1-flag[x];
if (flag[x]) update(1,1,n,x,1);
else update(1,1,n,x,-1);
}
}
}