首页 技术 正文
技术 2022年11月10日
0 收藏 624 点赞 4,254 浏览 3313 个字

http://www.lydsy.com/JudgeOnline/problem.php?id=4196

https://www.luogu.org/problemnew/show/P2146

你决定设计你自己的软件包管理器。不可避免地,你要解决软件包之间的依赖问题。如果软件包A依赖软件包B,那么安装软件包A以前,必须先安装软件包B。同时,如果想要卸载软件包B,则必须卸载软件包A。现在你已经获得了所有的软件包之间的依赖关系。而且,由于你之前的工作,除0号软件包以外,在你的管理器当中的软件包都会依赖一个且仅一个软件包,而0号软件包不依赖任何一个软件包。依赖关系不存在环(若有m(m≥2)个软件包A1,A2,A3,⋯,Am,其中A1依赖A2,A2依赖A3,A3依赖A4,……,A[m-1]依赖Am,而Am依赖A1,则称这m个软件包的依赖关系构成环),当然也不会有一个软件包依赖自己。

现在你要为你的软件包管理器写一个依赖解决程序。根据反馈,用户希望在安装和卸载某个软件包时,快速地知道这个操作实际上会改变多少个软件包的安装状态(即安装操作会安装多少个未安装的软件包,或卸载操作会卸载多少个已安装的软件包),你的任务就是实现这个部分。注意,安装一个已安装的软件包,或卸载一个未安装的软件包,都不会改变任何软件包的安装状态,即在此情况下,改变安装状态的软件包数为0。

题面很长,耐心看完就知道这是道树剖的板子题。

(然而不会求子树和的我还是去看了题解)

我们把安装的权值为1,未安装为0。

对于下载操作只需要查询该点到根节点的路程-1的个数。

对于卸载操作只需要查询该点的子树的1的个数。

(当然别忘了二者的修改。)

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N=2e5+;
const int INF=;
inline int read(){
int X=,w=;char ch=;
while(ch<''||ch>''){w|=ch=='-';ch=getchar();}
while(ch>=''&&ch<='')X=(X<<)+(X<<)+(ch^),ch=getchar();
return w?-X:X;
}
struct node{
int to,nxt;
}edge[*N];
struct tree{
int lazy,sum;
}t[*N];
int head[N],cnt,tot,n;
inline void add(int u,int v){
edge[++cnt].to=v;edge[cnt].nxt=head[u];head[u]=cnt;
}
int fa[N],dep[N],size[N],son[N],top[N],pos[N],idx[N];
void dfs1(int u){
size[u]=;
for(int i=head[u];i;i=edge[i].nxt){
int v=edge[i].to;
if(v==fa[u])continue;
fa[v]=u;dep[v]=dep[u]+;
dfs1(v);
size[u]+=size[v];
if(!son[u]||size[v]>size[son[u]])son[u]=v;
}
return;
}
void dfs2(int u,int anc){
tot++;
pos[u]=tot;
idx[tot]=u;
top[u]=anc;
if(!son[u])return;
dfs2(son[u],anc);
for(int i=head[u];i;i=edge[i].nxt){
int v=edge[i].to;
if(v==fa[u]||v==son[u])continue;
dfs2(v,v);
}
return;
}
inline void pushdown(int a,int l,int r){
int mid=(l+r)>>;
if(t[a].lazy!=-){
t[a*].lazy=t[a*+].lazy=t[a].lazy;
t[a*].sum=t[a].lazy*(mid-l+);
t[a*+].sum=t[a].lazy*(r-mid);
t[a].lazy=-;
}
return;
}
void modify(int a,int l,int r,int l1,int r1,int v){
if(r1<l||r<l1)return;
if(l1<=l&&r<=r1){
t[a].sum=v*(r-l+);
t[a].lazy=v;
return;
}
int mid=(l+r)>>;
pushdown(a,l,r);
modify(a*,l,mid,l1,r1,v);
modify(a*+,mid+,r,l1,r1,v);
t[a].sum=t[a*].sum+t[a*+].sum;
return;
}
void pathmodify(int u,int v,int c){
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]]){int t=u;u=v;v=t;}
modify(,,n,pos[top[u]],pos[u],c);
u=fa[top[u]];
}
if(dep[u]>dep[v]){int t=u;u=v;v=t;}
modify(,,n,pos[u],pos[v],c);
return;
}
inline void nodemodify(int u,int c){
modify(,,n,pos[u],pos[u]+size[u]-,c);
}
int query(int a,int l,int r,int l1,int r1){//线段树区间和
if(r1<l||l1>r)return ;
if(l1<=l&&r<=r1)return t[a].sum;
int mid=(l+r)>>;
pushdown(a,l,r);
return query(a*,l,mid,l1,r1)+query(a*+,mid+,r,l1,r1);
}
int pathquery(int u,int v){//询问(u,v)这条路径的和
if(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]]){int t=u;u=v;v=t;}
return pos[u]-pos[top[u]]+
+pathquery(fa[top[u]],v)-query(,,n,pos[top[u]],pos[u]);
}
if(dep[u]>dep[v]){int t=u;u=v;v=t;}
return pos[v]-pos[u]+-query(,,n,pos[u],pos[v]);
}
inline int nodequery(int u){
return query(,,n,pos[u],pos[u]+size[u]-);
}
void init(){
dep[]=fa[]=;
dfs1();
top[]=idx[]=pos[]=;
tot=;
dfs2(,);
for(int i=;i<*n;i++)t[i].lazy=-;
return;
}
int main(){
n=read();
for(int i=;i<=n;i++){
int v=read()+;
add(i,v);add(v,i);
}
init();
int q=read();
while(q--){
char op[];
scanf("%s",op);
int u=read()+;
if(op[]=='i'){
printf("%d\n",pathquery(u,));
pathmodify(u,,);
}
else{
printf("%d\n",nodequery(u));
nodemodify(u,);
}
}
return ;
}

+++++++++++++++++++++++++++++++++++++++++++

+本文作者:luyouqi233。               +

+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+

+++++++++++++++++++++++++++++++++++++++++++

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898