"Dfs sequence" "Tree array" bzoj1103 [POI2007] Metropolitan Meg

Source: Internet
Author: User

Preprocessing each point to the root node of the soil, inserted into a tree array, and then each modification will only affect the nodes in the subtree, thus equivalent to the interval modification, point query.

#include <cstdio>using namespace std; #define N 250001int n,en,v[n<<1],next[n<<1],first[n],m;void Addedge (const int &u,const int &v) {v[++en]=v;next[en]=first[u];first[u]=en;} int now,ls[n],rs[n],fa[n],a[n];void dfs (int U) {ls[u]=++now;for (int i=first[u];i;i=next[i]) if (V[i]!=fa[u]) {fa[v [I]]      =u;      a[v[i]]=a[u]+1;    DFS (V[i]); }rs[u]=now;} int d[n];void add_node (int p,const int &v) {for (;p <=n;p+= (-P)) p&;} void Add_range (const int &AMP;L,CONST int &r,const int &v) {Add_node (l,v), if (r!=n) Add_node (r+1,-v);} int query (int p) {int res=0;for (;p; p-= (-P)) Res+=d[p];return res;}  int main () {int A, B, Char op[2];scanf ("%d", &n), for (int i=1;i<n;++i) {scanf ("%d%d", &a,&b);  Addedge (A, b);  Addedge (B,a); }dfs (1), for (int i=2;i<=n;++i) Add_range (Ls[i],ls[i],a[i]), scanf ("%d", &m), and for (int i=1;i<n+m;++i) {scanf ("  %s%d ", op,&a);  if (op[0]== ' W ') printf ("%d\n", Query (Ls[a]));    else {scanf ("%d", &b); IF (fa[a]==b) Add_range (ls[a],rs[a],-1);    else Add_range (ls[b],rs[b],-1); }}return 0;}

Dfs sequence "tree array" bzoj1103 [POI2007] Metropolitan Meg

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.