Bzoj 4196 [noi2015] Software Package Manager (tree link splitting + line segment tree)

Source: Internet
Author: User
4196: [noi2015] Package Manager time limit: 10 sec memory limit: 512 MB
Submit: 2852 solved: 1668
[Submit] [Status] [discuss] Description

Linux users and OSX users must be familiar with the software package manager. Through the Package Manager, you can install a software package through a line of commands, and then the package manager will help you download the software package from the software source, at the same time, all dependencies are automatically solved (that is, other software packages on which the installation of this software package is downloaded), and all configurations are completed. Apt-get used by Debian/Ubuntu, yum used by fedora/centos, and homebrew available under OSX are both excellent software package managers.

You decide to design your own package manager. Inevitably, you need to solve the dependency problem between software packages. If Package A depends on Package B, Package B must be installed before Package A is installed. If you want to uninstall Package B, you must uninstall Package. Now you have obtained the dependency between all software packages. In addition, because of your previous work, except software package 0, the software package in your manager will depend on one and only one software package, while software package 0 does not depend on any software package. The dependency does not contain loops (if M (M ≥ 2) software packages A1, A2, A3 ,..., Am, where A1 depends on A2, A2 depends on A3, A3 depends on A4 ,......, Am-1 depends on AM, and am depends on A1, which is called the dependency of the M software package.) Of course, no software package depends on itself. Now you need to write a dependency solution for your package manager. According to feedback, the user wants to quickly know how many packages will actually change the installation status when installing and uninstalling a software package (that is, how many uninstalled software packages will be installed during the installation operation, or how many installed software packages will be uninstalled during the uninstallation operation). Your task is to implement this part. Note that installing or detaching an uninstalled software package does not change the installation status of any software package. In this case, the number of software packages that change the installation status is 0. Input

The first row of the input file contains a positive integer N, indicating the total number of packages. The software package starts from 0.

The next line contains n-1 integers separated by a single space, representing 1, 2, 3 ,..., The number of packages that n−2 and n−1 depend on. The next row contains a positive integer Q, indicating the total number of queries. Followed by Q rows, one query per line. There are two types of inquiries: installx: Installation Package xuninstallx: uninstall package x you need to maintain the installation status of each package. At the beginning, all the packages are not installed. For each operation, you need to output how many packages will change the installation status in this step, and then apply this operation (that is, change the installation status you maintain ). Output

The output file contains row Q.

The number of packages that change the installation status in step I. Sample input7
0 0 0 1 1 5
5
Install 5
Install 6
Uninstall 1
Install 4
Uninstall 0 sample output3
1
3
2
3 hint

 

All software packages are not installed at the beginning.


Install software package 5. You must install software packages 0, 1, and 5.
Then install the software package 6. You only need to install the software package 6. Four software packages, 0, 1, 5, and 6, are installed.
To uninstall software package 1, you must uninstall software packages 1, 5, and 6. At this time, only the software package 0 is still in the installation status.
Install software package 4. You need to install software packages 1 and 4. At this time, and 4 are in the installation status.
Finally, uninstalling Software Package 0 uninstalls all software packages.
 
 
N = 100000
Q = 100000 question: Actually, two operations are provided for you: 1 .. The weight of a chain is changed to 12. when the weight of a sub-tree is changed to 0, the number of points in each operation has been changed. The idea is: the bare tree chain is split and the line segment tree is changed, because it is directly asked after modification, we can directly merge modifications and query operations. When querying a chain, we need to add a solve function to jump across different chains, the subtree is a continuous interval in the section of a subtree due to the features of tree link splitting. We can simply ask about the interval. I forgot my own bidirectional edge .. The array is opened in 1e5, And the RE is triggered, and it is closed. Implementation Code:
# Include <bits/stdc ++. h> using namespace STD; # define ll long # define lson L, M, RT <1 # define rson m + 1, R, RT <1 | 1 # define mid int M = (L + r)> 1 const int mod = 201314; const int M = 2e5 + 10; // two bidirectional edges x 2 struct nodes {int to, next;} e [m]; int CNT, cnt1, N; int son [m], siz [m], head [m], Fa [m], top [m], DEP [m], tid [m], MX [m], rk [m]; int sum [m <2], lazy [m <2]; void add (int u, int v) {e [++ CNT]. to = V; E [CNT]. next = head [u]; hea D [u] = CNT;} void dfs1 (int u, int Faz, int deep) {Dep [u] = deep; Fa [u] = FAZ; siz [u] = 1; for (INT I = head [u]; I; I = E [I]. next) {int v = E [I]. to; If (V = FAZ) continue; dfs1 (v, U, deep + 1); siz [u] + = siz [v]; if (siz [v]> siz [son [u] | son [u] =-1) Son [u] = V ;}} void dfs2 (int u, int t) {top [u] = T; MX [u] = cnt1; TID [u] = cnt1; rk [cnt1] = u; cnt1 ++; if (son [u] =-1) return; dfs2 (son [u], T), MX [u] = max (MX [u], MX [son [u]); For (INT I = head [u]; I; I = E [I]. next) {int v = E [I]. to; If (V! = Fa [u] & V! = Son [u]) dfs2 (V, V), MX [u] = max (MX [u], MX [v]) ;}} void pushup (int rt) {sum [RT] = sum [RT <1] + sum [RT <1 | 1];} void Pushdown (int l, int R, int RT) {If (lazy [RT] = 0) {sum [RT <1] = sum [RT <1 | 1] = 0; lazy [RT <1] = lazy [RT <1 | 1] = 0; lazy [RT] =-1;} else if (lazy [RT] = 1) {mid; sum [RT <1] = m-L + 1; sum [RT <1 | 1] = r-m; lazy [RT <1] = lazy [RT <1 | 1] = 1; lazy [RT] =-1 ;}} void build (int l, int R, int RT) {lazy [RT] = -1; if (L = r) {sum [RT] = 0; lazy [RT] =-1; return;} mid; build (lson ); build (rson);} int Update (int l, int R, int C, int L, int R, int RT) {If (L <= L & R> = r) {If (C = 0) {int CNT = sum [RT]; sum [RT] = 0; lazy [RT] = 0; return CNT;} else {int CNT = r-L + 1-sum [RT]; sum [RT] = r-L + 1; lazy [RT] = 1; return CNT ;}} Pushdown (L, R, RT); Mid; int ret = 0; If (L <= m) RET + = Update (L, R, C, lson); If (r> m) RET + = UPDA Te (L, R, C, rson); pushup (RT); return ret;} int solve (int x, int y) {int FX = top [X], FY = top [y]; int ans = 0; while (FX! = FY) {If (DEP [FX] <Dep [FY]) Swap (FX, FY), swap (x, y); ans + = Update (TID [FX], TID [X], 1,1, N, 1); X = Fa [FX]; FX = top [X];} If (DEP [x]> Dep [y]) swap (x, y); ans + = Update (TID [X], tid [Y], 100, N, 1); Return ans;} Char s []; int main () {int X, M; CNT = 1, cnt1 = 1; scanf ("% d", & N); memset (son,-1, sizeof (son); For (INT I = 2; I <= N; I ++) {scanf ("% d", & X); add (x + 1, i); add (I, x + 1);} dfs1 (1, 0, 1); dfs2 (1, 0); Build (1, n, 1 ); scanf ("% d", & M); For (INT I = 1; I <= m; I ++) {scanf ("% s", S ); if (s [0] = 'I') {scanf ("% d", & X); X ++; printf ("% d \ n ", solve (x, 1);} else {scanf ("% d", & X); X ++; printf ("% d \ n ", update (TID [X], MX [X], N, 1) ;}} return 0 ;}

 

Bzoj 4196 [noi2015] Software Package Manager (tree link splitting + line segment tree)

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.