POJ3321 Apple Tree (array of trees)

Source: Internet
Author: User

First, DFS evaluates each node as the root of the subtree in the tree array of the starting and ending values, and then the tree array to do interval query and single point update.


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <cmath>
#include <utility>
usingnamespaceStd
typedefLongLongLL;
ConstintN =100008, INF =0x3f3f3f3f;
#defineMS (A, num) memset (A, num, sizeof (a))
intLeftid[n], rightid[n];
structnode{
intTo,next;
}edge[2* N];
intHead[n],tot;
voidInit () {
Memset (Head,-1,sizeof(head));
tot =0;
}
InlinevoidAddedge (intUintTo) {
Edge[tot].to=to;
Edge[tot].next=head[u];
head[u]=tot++;
}
intN, M;
BOOLSta[n];
intC[n];
InlineintLowbit (intx) {
returnx&-x;
}
InlinevoidAddintXintVal) {
for(intI=x;i<=n;i+=lowbit (i)) {
C[i] + = val;
}
}
InlineintSumintx) {
intRET =0;
for(intI=x;i>0; I-=lowbit (i)) {
Ret+=c[i];
}
returnRet
}

intCNT =0;

voidDfsintUintFA) {
Leftid[u] = cnt +1;
for(inti = Head[u]; ~i; i = edge[i].next) {
intv = edge[i].to;
if(v! = FA) {
DFS (v, u);
}
}
Rightid[u] = ++cnt;
}

intMain () {
while(~SCANF ("%d", &n) && N) {
MS (C,0);
Init ();
for(inti =1; I <= N; i++) {
Add (I,1);
Sta[i] =1;
}
intU, v;
for(inti =0; I < n-1; i++) {
scanf"%d%d", &u, &v);
Addedge (U, v);
Addedge (V, u);
}
CNT =0;
Dfs1, -1);
cin>>m;
CharOpe

while(m--) {
scanf"%c%d", &ope, &u);
intL = Leftid[u], r = Rightid[u];
if(Ope = ='C'){
if(Sta[r]) {
STA[R] =0;
Add (R,-1);
}Else{
STA[R] =1;
Add (R,1);
}
}Else{
intTP = SUM (r)-SUM (L-1);
printf"%d\n", TP);
}
}

}
return0;
}

POJ3321 Apple Tree (array of trees)

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.