软件包管理器
Time Limit: 10 Sec Memory Limit: 512 MB
Description
Linux用户和OSX用户一定对软件包管理器不会陌生。通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖(即下载安装这个软件包的安装所依赖的其它软件包),完成所有的配置。Debian/Ubuntu使用的apt-get,Fedora/CentOS使用的yum,以及OSX下可用的homebrew都是优秀的软件包管理器。
你决定设计你自己的软件包管理器。不可避免地,你要解决软件包之间的依赖问题。如果软件包A依赖软件包B,那么安装软件包A以前,必须先安装软件包B。同时,如果想要卸载软件包B,则必须卸载软件包A。现在你已经获得了所有的软件包之间的依赖关系。而且,由于你之前的工作,除0号软件包以外,在你的管理器当中的软件包都会依赖一个且仅一个软件包,而0号软件包不依赖任何一个软件包。依赖关系不存在环(若有m(m≥2)个软件包A1,A2,A3,…,Am,其中A1依赖A2,A2依赖A3,A3依赖A4,……,Am−1依赖Am,而Am依赖A1,则称这m个软件包的依赖关系构成环),当然也不会有一个软件包依赖自己。
现在你要为你的软件包管理器写一个依赖解决程序。根据反馈,用户希望在安装和卸载某个软件包时,快速地知道这个操作实际上会改变多少个软件包的安装状态(即安装操作会安装多少个未安装的软件包,或卸载操作会卸载多少个已安装的软件包),你的任务就是实现这个部分。注意,安装一个已安装的软件包,或卸载一个未安装的软件包,都不会改变任何软件包的安装状态,即在此情况下,改变安装状态的软件包数为0。
Input
输入文件的第1行包含1个正整数n,表示软件包的总数。软件包从0开始编号。
随后一行包含n−1个整数,相邻整数之间用单个空格隔开,分别表示1,2,3,…,n−2,n−1号软件包依赖的软件包的编号。
接下来一行包含1个正整数q,表示询问的总数。
之后q行,每行1个询问。询问分为两种:
installx:表示安装软件包x
uninstallx:表示卸载软件包x
你需要维护每个软件包的安装状态,一开始所有的软件包都处于未安装状态。对于每个操作,你需要输出这步操作会改变多少个软件包的安装状态,随后应用这个操作(即改变你维护的安装状态)。
Output
输出文件包括q行。
输出文件的第i行输出1个整数,为第i步操作中改变安装状态的软件包数。
Sample Input
7
0 0 0 1 1 5
5
install 5
install 6
uninstall 1
install 4
uninstall 0
Sample Output
3
1
3
2
3
【样例1说明】
一开始所有的软件包都处于未安装状态。
安装 5 号软件包,需要安装 0,1,5 三个软件包。
之后安装 6 号软件包,只需要安装 6 号软件包。此时安装了 0,1,5,6 四个软件包。
卸载 1 号软件包需要卸载 1,5,6 三个软件包。此时只有 0 号软件包还处于安装状态。
之后安装 4 号软件包,需要安装 1,4 两个软件包。此时 0,1,4 处在安装状态。
最后,卸载 0 号软件包会卸载所有的软件包。
【数据范围】
解析:
树链剖分。
把每个点是否安装标记在线段树中,修改时区间标记就行了,记得标记下传与答案上传。复杂度O(N (log N) ^ 2)。
代码:
#include <bits/stdc++.h>
using namespace std;
const int Max=100010;
int n,m,s=0,ans,tot,q;
int first[Max],size[Max],rev[Max],seg[Max],top[Max],father[Max],depth[Max],son[Max];
int tree1[Max<<2],tree2[Max<<2];
struct bian{int to,next;};
bian edge[Max<<1];
int cov[Max<<2];
inline int get_int()
{
int x=0,f=1;
char c;
for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
if(c=='-') {f=-1;c=getchar();}
for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
return x*f;
}
inline void build(int x,int y)
{
edge[++s].next=first[x];
first[x]=s;
edge[s].to=y;
}
inline void dfs1(int point,int fa)
{
size[point] = 1;
depth[point] = depth[fa] + 1;
father[point] = fa;
for(int u=first[point];u;u=edge[u].next)
{
int to=edge[u].to;
if(to == fa) continue;
dfs1(to,point);
size[point] += size[to];
if(size[to] > size[son[point]]) son[point] = to;
}
}
inline void dfs2(int point,int fa)
{
if(son[point])
{
rev[son[point]] = ++tot;
seg[tot] = son[point];
top[son[point]] = top[point];
dfs2(son[point],point);
}
for(int u=first[point];u;u=edge[u].next)
{
int to=edge[u].to;
if(!top[to])
{
rev[to] = ++tot;
seg[tot] = to;
top[to] = to;
dfs2(to,point);
}
}
}
inline void update(int root) //答案上传更新
{
tree1[root] = tree1[root<<1] + tree1[root<<1|1];
tree2[root] = tree2[root<<1] + tree2[root<<1|1];
}
inline void buildtree(int root,int l,int r)
{
if(l == r)
{
tree1[root] = 0;
tree2[root] = 1;
return;
}
int mid = l + r >> 1;
buildtree(root<<1,l,mid);
buildtree(root<<1|1,mid+1,r);
update(root);
}
inline void pushdown(int root,int l,int r,int mid) //标记下传
{
if(cov[root]==-1) return; //如果没有做标记则返回
cov[root<<1] = cov[root<<1|1] = cov[root];
if(cov[root] == 1)
{
tree1[root<<1] = mid-l+1,tree1[root<<1|1] = r-mid;
tree2[root<<1] = tree2[root<<1|1] = 0;
}
else
{
tree2[root<<1] = mid-l+1,tree2[root<<1|1] = r-mid;
tree1[root<<1] = tree1[root<<1|1] = 0;
}
cov[root]=-1;
}
inline void Add(int root,int l,int r,int L,int R,int flag)
{
if(L <= l && R >= r)
{
if(flag) tree2[root] = 0,tree1[root] = r-l+1,cov[root] = 1;
else tree1[root] = 0,tree2[root] = r-l+1,cov[root] = 0;
return;
}
int mid = l + r >> 1;
pushdown(root,l,r,mid);
if(L <= mid) Add(root<<1,l,mid,L,R,flag);
if(R >= mid+1) Add(root<<1|1,mid+1,r,L,R,flag);
update(root);
}
inline void Q(int root,int l,int r,int L,int R,int flag)
{
if(L <= l && R >= r)
{
flag ? ans += tree1[root] : ans += tree2[root];
return;
}
int mid = l + r >> 1;
pushdown(root,l,r,mid);
if(L <= mid) Q(root<<1,l,mid,L,R,flag);
if(R >= mid+1) Q(root<<1|1,mid+1,r,L,R,flag);
}
inline void ask(int x,int y)
{
int fax = top[x],fay = top[y];
while(fax != fay)
{
if(depth[fax] < depth[fay]) swap(x,y),swap(fax,fay);
Q(1,1,tot,rev[fax],rev[x],0);
Add(1,1,tot,rev[fax],rev[x],1);
x = father[fax],fax = top[x];
}
if(depth[x] > depth[y]) swap(x,y);
Q(1,1,tot,rev[x],rev[y],0);
Add(1,1,tot,rev[x],rev[y],1);
}
inline void print(int x)
{
if(x<0) {putchar('-'),x=-x;}
if(x>9) print(x/10);
putchar(x%10+'0');
}
int main()
{
n=get_int();
for(int i=1;i<=n-1;i++)
{
int x=get_int() + 1;
build(x,i + 1);
build(i + 1,x);
}
top[1] = rev[1] = seg[1] = tot = 1;
dfs1(1,0);
dfs2(1,0);
buildtree(1,1,tot);
memset(cov,-1,sizeof(cov));
char ch[10];
q=get_int();
while(q--)
{
ans=0;
scanf("%s",ch);
int x=get_int()+1;
if(ch[0] == 'i') ask(1,x);
else
{
Q(1,1,tot,rev[x],rev[x] + size[x] - 1,1);
Add(1,1,tot,rev[x],rev[x] + size[x] - 1,0);
}
print(ans);
putchar('\n');
}
return 0;
}