BZOJ4196 NOI2015 Package Manager

Source: Internet
Author: User

4196: [Noi2015] Package Manager time Limit:10 Sec Memory limit:512 mbdescription 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. The 1th line of the input file contains 1 positive integer n, which represents 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 queries: INSTALLX: Indicates the installation package XUNINSTALLX: Uninstall package x you need to maintain the installation state 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 (Change the installation status of your maintenance). Output file includes 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 Input70 0 0 1 1 55install 5install 6uninstall 1install 4uninstall 0Sample output31323hint 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=100000q=100000

Algorithm discussion:

Tree chain splits. We consider installing a software that turns all the path points on the root into 1. Uninstalling a software is the tree turning into 0.

Then we use line tree dyeing to maintain. Can. I have been a silly force of the original passed n marks.

Code:

#include <cstdlib> #include <iostream> #include <cstring> #include <cstdio> #include < Algorithm> using namespace Std;const int N = 100000 + 5;  Char ss[15];int N, M, Tim, Cnt;int Size[n], son[n], head[n], Fa[n];int dfn[n], seg[n], mx[n], depth[n], top[n];struct Edge {int from, to, next;} Edges[n << 1];    void Insert (int from, int to) {+ + cnt; Edges[cnt].from = from;    Edges[cnt].to = to; Edges[cnt].next = Head[from]; Head[from] = cnt;}    void dfs_1 (int u) {size[u] = 1;        for (int i = head[u]; i; i = edges[i].next) {int v = edges[i].to;            if (v = fa[u]) {Depth[v] = Depth[u] + 1;            Dfs_1 (v);            Size[u] + = Size[v];        if (!son[u] | | size[v] > SIZE[SON[U]]) son[u] = v;    }}} void Dfs_2 (int u, int ances) {+ + Tim;    Dfn[u] = Mx[u] = Tim;    Seg[tim] = u;    Top[u] = ances;    if (!son[u]) return;    Dfs_2 (Son[u], ances);    Mx[u] = max (Mx[u], Mx[son[u]]); for (int i =Head[u]; I        i = edges[i].next) {int v = edges[i].to;            if (v = fa[u] && v! = Son[u]) {dfs_2 (V, v);        Mx[u] = max (Mx[u], mx[v]); }}} struct Segtree {int L, r, Color, sum, ex, size;} Node[n * 4];    void pushup (int o) {if (node[o].l = = NODE[O].R) return; int L = o << 1, r = O << 1 |    1;    Node[o].sum = Node[l].sum + node[r].sum;    } void pushdown (int o) {if (node[o].l = = NODE[O].R) return; int L = o << 1, r = O << 1 |    1;        if (node[o].color! =-1) {Node[l].color = Node[r].color = Node[o].color;        Node[l].sum = node[l].size * NODE[L].COLOR;        Node[r].sum = node[r].size * NODE[R].COLOR;        Node[l].ex = Node[r].ex = Node[o].color;        Node[o].color =-1;    cout << o << "<< node[o].ex <<" * * * "<< Endl; }} void build (int o, int l, int r) {node[o].l = l;     NODE[O].R = R; node[o].sum = 0;    Node[o].color =-1;    if (L = = r) {    Node[o].size = 1;    Return    } int mid = (L + r) >> 1; Build (o << 1, L, mid);    Build (o << 1 | 1, mid + 1, R);    Pushup (o); Node[o].size = node[o << 1].size + node[o << 1 | 1].size;}  void update (int o, int l, int r, int v) {if (NODE[O].L = = L && NODE[O].R = = r) {//cout<< L <<        "<< R <<" "<< v <<"---------"<< Endl; Node[o].color = v;        Node[o].ex = v;        Node[o].sum = node[o].size * NODE[O].COLOR;    Return    } int mid = (node[o].l + node[o].r) >> 1;    Pushdown (o);    if (r <= mid) update (O << 1, L, R, v);    else if (L > Mid) Update (o << 1 | 1, L, R, v);        else {update (o << 1, L, Mid, V);    Update (o << 1 | 1, mid + 1, R, v); } pushup (o);}    int query (int o, int l, int r) {if (NODE[O].L = = L && NODE[O].R = = r) {return node[o].sum; } int mid = (node[o].l + NODE[O].R) >> 1;    Pushdown (o);    if (R <= mid) return query (o << 1, L, R);    else if (L > Mid) return query (o << 1 | 1, L, R);    else {return query (o << 1, L, mid) + query (o << 1 | 1, mid + 1, R); } pushup (o);}    int query_ex (int o, int pos) {if (NODE[O].L = = pos && NODE[O].R = = pos) return node[o].ex;    int mid = (node[o].l + node[o].r) >> 1;    Pushdown (o);    if (POS <= mid) return QUERY_EX (o << 1, POS);       else if (pos > Mid) return query_ex (o << 1 | 1, POS); Pushup (o);}    int Q (int x) {int res = 0;        while (x) {res + = query (1, dfn[top[x]], dfn[x]);    x = fa[top[x]]; } return res;        void U (int x) {while (x) {update (1, dfn[top[x]], dfn[x], 1);    x = fa[top[x]];    }} #define Stone_ int main () {#ifndef stone_ freopen ("manager.in", "R", stdin);    Freopen ("Manager.out", "w", stdout), #endif int __size__ = << 20; char* __p__ = (char*) malloc (__size__) + __size__;    __asm__ ("Movl%0,%%esp\n":: "R" (__p__));    int x, Flag;    scanf ("%d", &n);        for (int i = 1; i < n; + + i) {scanf ("%d", &fa[i]);        if (!fa[i]) fa[i] = n;    Insert (Fa[i], i);    } Depth[n] = 1; Dfs_1 (n);    Dfs_2 (n, N);    Build (1, 1, N);    scanf ("%d", &m);        for (int i = 1; I <= m; + + i) {scanf ("%s%d", SS, &x);        if (!x) x = n;        Flag = QUERY_EX (1, dfn[x]);            if (ss[0] = = ' I ') {if (flag) {puts ("0"); continue;}            printf ("%d\n", Depth[x]-Q (x));        U (x);            } else {if (!flag) {puts ("0"); continue;}            printf ("%d\n", query (1, dfn[x], mx[x]));        Update (1, dfn[x], mx[x], 0); }} #ifndef Stone_ fclose (stdout);   Fclose (stdout); #endif return 0; }

BZOJ4196 NOI2015 Package Manager

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.