当前位置:网站首页>[noi2015] package manager
[noi2015] package manager
2022-07-25 18:22:00 【Soup key】
[NOI2015] Package manager
Background
Linux Users and OSX Users must be familiar with the package manager .
Through the package manager , You can install a package with a single command , Then the package manager will help you download packages from the source , Automatically resolve all dependencies at the same time ( That is, download and install other software packages that the installation of this software package depends on ), Complete all configuration .
Debian/Ubuntu The use of apt-get,Fedora/CentOS The use of yum, as well as OSX The next available homebrew Are excellent package managers .
Title Description
You decide to design your own package manager . inevitably , You have to solve the dependency problem between software packages .
If the package a a a Dependent packages b b b, Then install the package a a a before , You must first install the package b b b.
meanwhile , If you want to uninstall the package b b b, You must uninstall the package a a a.
Now you have all the dependencies between packages .
and , Because of your previous work , except 0 0 0 Outside package No , Packages in your manager will depend on one and only one package , and 0 0 0 Package No. 1 does not depend on any package .
And there is no ring in the dependency ( That is, there will be no m m m Software package a 1 , a 2 , … , a m a_1,a_2, \dots , a_m a1,a2,…,am, about i < m i<m i<m, a i a_i ai rely on a i + 1 a_{i+1} ai+1, and a m a_m am rely on a 1 a_1 a1 The situation of ).
Now you have to write a dependency resolver for your package manager .
Based on feedback , Users want to install and uninstall a package , Quickly know how many packages the installation state will actually be changed by this operation ( That is, how many uninstalled packages will be installed during the installation operation , Or how many installed packages will be uninstalled ), Your task is to realize this part .
Be careful , Install an installed package , Or uninstall an uninstalled package , Will not change the installation status of any software package , In this case , The number of packages changing the installation state is 0 0 0.
Input format
First line a positive integer n n n, Indicates the number of software packages , from 0 0 0 Numbered starting .
The second line has n − 1 n-1 n−1 It's an integer , The first i i i A means i i i Package number on which package number depends .
Then a positive integer on each line q q q, Represents the number of operations , The format is as follows :
install xSaid the installation x x x Software package Nouninstall xMeans uninstall x x x Software package No
At first, all software packages were not installed .
For each operation , You need to output how many packages' installation status will be changed by this operation , Then apply this operation ( That is, change the installation state you maintain ).
Output format
Output q q q That's ok , One integer per row , Indicates the answer to each inquiry .
Examples #1
The sample input #1
7
0 0 0 1 1 5
5
install 5
install 6
uninstall 1
install 4
uninstall 0
Sample output #1
3
1
3
2
3
Examples #2
The sample input #2
10
0 1 2 1 3 0 0 3 2
10
install 0
install 3
uninstall 2
install 7
install 5
install 9
uninstall 9
install 4
install 1
install 9
Sample output #2
1
3
2
1
3
1
1
1
0
1
Tips
At first, all software packages are not installed .
install 5 5 5 Software package No , Need to install 0 , 1 , 5 0,1,5 0,1,5 Three packages .
After the installation 6 6 6 Software package No , Just install 6 6 6 Software package No . At this time 0 , 1 , 5 , 6 0,1,5,6 0,1,5,6 Four packages .
uninstall 1 1 1 Package No. needs to be uninstalled 1 , 5 , 6 1,5,6 1,5,6 Three packages . At this time only 0 0 0 Package is still installed .
After the installation 4 4 4 Software package No , Need to install 1 , 4 1,4 1,4 Two packages . here 0 , 1 , 4 0,1,4 0,1,4 In installation state . Last , uninstall 0 0 0 Package No. 1 will uninstall all packages .
// Every time the software is installed , Just connect the root node to x All the values on the software path become 1;
// Empathy , Every time you uninstall the software , Just put x And the value of its subtree becomes 0;
// Then use interval addition and subtraction to obtain the change quantity
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int head[N],cnt;
int fa[N],size[N],deep[N],son[N],top[N],dfsxu[N],id[N],k;
//fa[i] node i Parent node size[i] With i Is the total number of nodes in the subtree of the root
//deep[i] node i The depth of the son[i] node i My dear son
//top[i] node i The top element of the heavy chain
//dfsxu Initial value of each point id Time stamp
//dfs Order can reflect the continuous information of points on the tree , The chain is dfs order
int n,v[N],m,r,ss[N];
struct stu{
int l,r,sum,lz;
}tree[N*4];
struct stuu{
int to,nt;
}ed[N*2];
void add(int u,int v){
// Chain forward star edge
ed[cnt]=stuu{
v,head[u]};
head[u]=cnt++;
}
void dfs1(int u,int f){
//u Is the current node ,f Is the parent node of the current node ; initialization 1
fa[u]=f;//u The parent of the node is f
deep[u]=deep[f]+1;// depth +1
size[u]=1;
int maxsize=-1;// It is a temporary variable to judge whether it is a heavy son
for(int i=head[u];i!=-1;i=ed[i].nt){
// Traverse all the sons , Keep updating and find your son
int v=ed[i].to;
if(v==f) continue;// My father must have skipped it directly
dfs1(v,u);// Depth traversal , The current node becomes the parent node , The found son becomes the current node and continues to traverse
size[u]+=size[v];// After traversal , Let the size of the current node plus the size of the son
if(size[v]>maxsize){
// If the size of the son is larger than the temporary variable
maxsize=size[v];// Just assign it to a temporary variable
son[u]=v;// Change the heavy son of the current node
}
}
}
void dfs2(int u,int t){
// initialization 2
id[u]=++k;// Each node is timestamped
top[u]=t;// node u The top of the heavy chain is t
dfsxu[k]=u;// Save dfs order
if(!son[u]) return;// If there is no heavy son, there will be no son. Go back directly
dfs2(son[u],t);// Continue to walk towards your son
for(int i=head[u];i!=-1;i=ed[i].nt){
int v =ed[i].to;
if(v==fa[u]||v==son[u]) continue;// If it is a parent node or a duplicate son, skip directly
dfs2(v,v);// Some heavy chains start with light sons and go down , The top is light son
}
}
void pushup(int u){
// Update up
tree[u].sum=tree[u<<1].sum+tree[u<<1|1].sum;// The interval sum of the left and right sub segments is fed back to the top
}
void pushdown(int u){
// Update down , take lazy Mark push down
if(tree[u].lz!=-1){
tree[u<<1].sum=tree[u].lz*(tree[u<<1].r-tree[u<<1].l+1);
tree[u<<1|1].sum=tree[u].lz*(tree[u<<1|1].r-tree[u<<1|1].l+1);
tree[u<<1].lz=tree[u].lz;
tree[u<<1|1].lz=tree[u].lz;
tree[u].lz=-1;
}
}
void build(int l,int r,int u){
// Build up trees
tree[u].l=l,tree[u].r=r,tree[u].lz=-1;
if(l==r){
// Reach the leaf node , return
return ;
}// Otherwise, this node represents more than one point , It also has two sons
int mid=(l+r)>>1;
build(l,mid,u<<1);// Recursive left subtree
build(mid+1,r,u<<1|1);// Recursive right subtree
pushup(u);// to update
}
void update(int l,int r,int u,int b){
if(l<=tree[u].l&&tree[u].r<=r){
// Included as a subinterval , Then make an overall modification , Don't go further
tree[u].sum=b*(tree[u].r-tree[u].l+1);
tree[u].lz=b;
return ;
}// Otherwise, it is staggered , You need to continue to go deeper into the lower sub interval
pushdown(u);// Update down
int mid=(tree[u].l+tree[u].r)>>1;
if(l<=mid) update(l,r,u<<1,b);// Recursive left subtree
if(r>=mid+1) update(l,r,u<<1|1,b);// Recursive right subtree
pushup(u);// to update
}
void updatecc(int a,int b,int c){
while(top[a]!=top[b]){
// look for LCA: When x,y Not in the same heavy chain , Compare x,y The depth of the head of the heavy chain
if(deep[top[a]]<deep[top[b]]) swap(a,b);// Ensure deeper
update(id[top[a]],id[a],1,c);// By climbing lca Every time you jump the chain head to modify or query the interval
a=fa[top[a]];// The deeper one jumps along the heavy chain to the father at the head of the chain
}
if(deep[a]>deep[b]) swap(a,b);// It can be operated directly on a heavy chain
update(id[a],id[b],1,c);
}
int main(){
int kk;
memset(head,-1,sizeof(head));
cin>>n;
for(int i=2;i<=n;i++){
scanf("%d",&kk);
kk++;
add(kk,i);
}
dfs1(1,0);
dfs2(1,1);
build(1,n,1);
scanf("%d",&m);
while(m--){
int s1=tree[1].sum,s2;
string p;
cin>>p>>kk;
kk++;
if(p[0]=='i'){
updatecc(1,kk,1);
s2=tree[1].sum;
printf("%d\n",abs(s1-s2));
}
else if(p[0]=='u'){
update(id[kk],id[kk]+size[kk]-1,1,0);
s2=tree[1].sum;
printf("%d\n",abs(s1-s2));
}
}
}
边栏推荐
- Repair process of bad blocks of primary standby database
- CVE-2022-33891 Apache spark shell 命令注入漏洞复现
- Could not stop Cortex-M device! Please check the JTAG cable solution
- ORB_ Slam3 recurrence - Part I
- Practice of RTC performance automation tool in memory optimization scenario
- Safe operation instructions for oscilloscope probe that must be read by engineers
- [web page performance optimization] what about the slow loading speed of the first screen of SPA (single page application)?
- Detailed introduction and application of GaN (comprehensive and complete)
- 这是一张机器&深度学习代码速查表
- Use of join function in MATLAB
猜你喜欢
![Why is the index in [mysql] database implemented by b+ tree? Is hash table / red black tree /b tree feasible?](/img/1f/a2d50ec6bc97d52c1e7566a42e564b.png)
Why is the index in [mysql] database implemented by b+ tree? Is hash table / red black tree /b tree feasible?

Oracle导入出错:IMP-00038: 无法转换为环境字符集句柄

What are the advantages of real-time cloud rendering

Ch582 ble 5.0 uses Le coded broadcast and connection

Leetcode 101. symmetric binary tree & 100. same tree & 572. Subtree of another tree

ORB_SLAM3复现——上篇

如何将exe文件添加到开机启动

Safe operation instructions for oscilloscope probe that must be read by engineers

乐观锁解析

LeetCode 101. 对称二叉树 && 100. 相同的树 && 572. 另一棵树的子树
随机推荐
Talking about Devops monitoring, how does the team choose monitoring tools?
Leetcode 101. symmetric binary tree & 100. same tree & 572. Subtree of another tree
Construction of Huffman tree
The new version of 3dcat v2.1.3 has been released. You can't miss these three function updates!
Bl602 development environment setup
Linux启动mysql报错
1--- electronic physical cognition
[HAOI2015]树上操作
C language -- 25 minesweeping game
Oracle使用impdp导入报错:ORA-39001: 参数值无效 ORA-39000: 转储文件说明错误 ORA-39088: 文件名不能包含路径说明
二叉树的相关操作
Introduction to cloud XR and development opportunities of cloud XR in 5g Era
GAN的详细介绍及其应用(全面且完整)
STM8S003F3 内部flash调试
Could not stop Cortex-M device! please check the JTAG cable的解决办法
遍历数组的方法有哪些?for循环 forEach for/in for/of map的性能又有什么差别 该如何选择?
win11下vscode 自动升级失败 There was an error while marking a file for deletion
MySQL page lock
文件基础知识
Recognized by international authorities! Oceanbase was selected into the Forrester translational data platform report