"Tree chain split" "NOI 2015" "Bzoj 4196" package Manager

Source: Internet
Author: User
Tags split time limit
4196: [Noi2015] Package Manager
Time Limit:10 Sec  Memory limit:512 MB
submit:122  solved:97

Description

Linux users and OSX users must not be unfamiliar with the package manager. With the Package Manager, you can install a package from a single line of commands, and then the Package Manager will help you download the package from the software source, and automatically resolve all dependencies (that is, other packages that are dependent on downloading the installation of this package) to complete all configurations. The Apt-get,fedora/centos used by Debian/ubuntu and the homebrew available under OSX are excellent package managers.

You decide to design your own package Manager. Inevitably, you need to solve the dependencies between packages. If package a relies on package B, you must install package B before you install package A. Also, if you want to uninstall package B, you must uninstall package a. Now you've got all the dependencies between the packages. And, because of your previous work, in addition to the No. 0 package, the packages in your manager will depend on one and only one package, and the No. 0 package does not depend on any one package. Dependency does not exist ring (if there is M (m≥2) package a1,a2,a3,..., am, where A1 relies on a2,a2 dependency a3,a3 dependency a4,......,am−1 dependent am, and am relies on A1, it is said that the dependencies of M packages form a ring), Of course there will not be a package that relies on itself.

Now you need to write a dependency resolver for your package manager. Based on feedback, users want to quickly know when a package is installed and uninstalled how many packages are installed (that is, how many packages are installed, or how many installed packages are uninstalled), and your task is to implement this section. Note that installing an installed package, or uninstalling a package that is not installed, will not change the installation state of any packages, in which case the number of packages changing the installation state is 0.

Input

The 1th line of the input file contains 1 positive integer n, which indicates the total number of packages. The package is numbered starting from 0.

The following line contains n−1 integers, separated by a single space between adjacent integers, representing the number of packages on which the,..., n−2,n−1 package depends.
The next line contains 1 positive integer q, which indicates the total number of queries.
After Q line, 1 queries per line. There are two types of inquiries:

INSTALLX: Indicates installation package x
UNINSTALLX: means uninstall package X

You need to maintain the installation status of each package and all packages are not installed at the beginning. For each operation, you need to output this step to change the installation state of how many packages, and then apply this action (that is, change the installation state of your maintenance).

Output

The output file includes the Q line.

The output file of line I outputs 1 integers, the number of packages that change the installation state for step I operations.

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

HINT

All packages are not installed at the beginning.

Installing the 5th package requires the installation of 0,1,5 three packages.

After installing the 6th package, you only need to install the number 6th package. 0,1,5,6 Four packages are installed at this time.

Uninstalling the 1th package requires uninstalling 1,5,6 three packages. Only the No. 0 package is in the installation state at this time.

After installing the 4th package, you need to install 1, 42 packages. At this point the 0,1,4 is in the installation state.

Finally, uninstalling the NO. 0 package will uninstall all packages.

n<=100000
q<=100000

The following:

The title is long and useless.
Discrimination test instructions, in fact, is to give a tree, and then ask the chain information and subtree information, the tree chain can be cut off.
PS: I heard the Toptree written by CA. Kneel down on your knees

Code:

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath
> #include <algorithm> using namespace std;  #define N 200100 #define ROOT 1,1,tot #define LCH rt<<1,l,mid #define RCH rt<<1|1,mid+1,r struct edge{int
V,next;
}edge[n<<1]; struct tree{int sum,lazy;}
tree[n<<2];
int n,q,num=0,tot=0,head[n],de[n],fa[n],sz[n],son[n],top[n],w[n],en[n];
    inline int in () {int x=0; char Ch=getchar ();
    while (ch< ' 0 ' | | ch> ' 9 ') Ch=getchar ();
    while (ch>= ' 0 ' && ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar ();
return x; } inline void Add (int u,int v) {edge[++num].v=v; edge[num].next=head[u]; head[u]=num;} inline void dfs1 (int x) {s Z[x]=1;
    son[x]=0;
        for (int i=head[x]; i; i=edge[i].next) {int v=edge[i].v;
        if (v==fa[x]) continue; de[v]=de[x]+1;
        Fa[v]=x; DFS1 (v);
        SZ[X]+=SZ[V];
    if (!son[x] | | sz[son[x]]<sz[v]) son[x]=v; }} inline voidDFS2 (int x,int st) {w[x]=++tot; top[x]=st;
    if (Son[x]) DFS2 (son[x],st);
        for (int i=head[x]; i; i=edge[i].next) {int v=edge[i].v;
        if (V==son[x] | | v==fa[x]) continue;
    DFS2 (V,V);
} En[x]=tot;
    } inline void push_up (int rt,int l,int R) {if (tree[rt].lazy!=-1) tree[rt].sum=tree[rt].lazy* (r-l+1);
else tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum; } inline void Push_down (int rt,int l,int R) {if (tree[rt].lazy!=-1) {tree[rt<<1].lazy=tree[rt<<1|1
        ].lazy=tree[rt].lazy;
    Tree[rt].lazy=-1;
    }}: inline void build (int rt,int l,int r) {tree[rt].sum=0; tree[rt].lazy=-1;
    if (l==r) return;
    int mid= (L+R) >>1; Build (LCH);
Build (RCH); } inline void Change (int rt,int l,int r,int ll,int rr,int p) {if (ll<=l && r<=rr) {Tree[rt].lazy
        =p;
        PUSH_UP (RT,L,R);
    Return
    } int mid= (L+R) >>1;
    Push_down (RT,L,R); if (ll<=mid) Change (LCH,LL,RR,P);
    else Push_up (LCH);
    if (rr>mid) change (rch,ll,rr,p);
    else Push_up (RCH);
PUSH_UP (RT,L,R); } inline int query (int rt,int l,int r,int ll,int rr,int p) {if (ll<=l && r<=rr) {if (P) return r
        -l+1-tree[rt].sum;
    return tree[rt].sum;
    } int mid= (L+R) >>1,k=0;
    Push_down (RT,L,R);
    Push_up (LCH), push_up (RCH);
    if (ll<=mid) k+=query (lch,ll,rr,p);
    if (rr>mid) k+=query (rch,ll,rr,p);
return k;
    } inline void Install (int x,int y) {int ans=0;
        while (Top[x]!=top[y]) {if (de[top[x]]<de[top[y])) swap (x, y);
        Ans+=query (root,w[top[x]],w[x],1);
        Change (root,w[top[x]],w[x],1);
    X=FA[TOP[X]];
    } if (De[x]<de[y]) swap (x, y);
    Ans+=query (root,w[y],w[x],1);
    Change (root,w[y],w[x],1);
printf ("%d\n", ans);
    } inline void Uninstall (int x) {int ans=query (root,w[x],en[x],0);
    Change (root,w[x],en[x],0);
printf ("%d\n", ans); } int main () {N=in ();
        for (int i=1; i<n; i++) {int v=in ();
    Add (V,i), add (I,V);
    } q=in (); DFS1 (0); DFS2 (0,0);
    Build (root);
        while (q--) {char opt=getchar ();
        while (opt!= ' i ' && opt!= ' u ') Opt=getchar ();
        int X=in ();
        if (opt== ' I ') install (0,x);
    else uninstall (x);
} return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.