POJ-3321 Apple Tree (array of trees)

Source: Internet
Author: User

Link to original topic

Test instructions

Give you a tree of n nodes, each node has an apple at the beginning, and now there are two operations:

Q x: Query the x node and how many apples there are on its subtree;

C x: If there is no Apple on the X node, then one is added, and the other is reduced by one;

Ideas:

This is an interval query, single-point modification of the problem, we will think of using a tree array to do, but how to maintain the tree interval is a problem;

So the thought of the DFS sequence, the record of each node left and right is also transformed into an interval problem;

1 /*2 * @Author: Windystreet3 * @Date: 2018-08-02 22:10:444 * @Last Modified by:windystreet5 * @Last Modified time:2018-08-02 22:56:036 */7#include <stdio.h>8#include <string.h>9#include <algorithm>Ten#include <vector> One  A using namespacestd; -  - #defineX First the #defineY Second - #defineEPS 1e-2 - #defineGCD __GCD - #definePB Push_back + #definePI ACOs (-1.0) - #defineLowbit (x) (x) & (-X) + #defineBug printf ("!!!!! \ n "); A #defineMem (x, y) memset (x,y,sizeof (x)) at  -typedefLong LongLL; -typedefLong DoubleLD; -typedef pair<int,int>PII; -typedef unsignedLong LongULL; -  in Const intMAXN = 1e5+7; - ConstLL INF =1<< -; to Const intMoD = 1e9+7; +typedef vector<int>Ve; -Vector<ve>G (MAXN); the intLeft[maxn],right[maxn],cnt=1, N; * intTREE[MAXN],VIS[MAXN]; $ Panax Notoginseng intQueryintx) { -     intres =0; the      while(x) { +res+=Tree[x]; Ax-=lowbit (x); the     } +     returnRes; - } $ voidAddintXintv) { $      for(inti=x;i<=n;i+=lowbit (i)) { -tree[i]+=v; -     } the     return ; - }Wuyi voidDfsintx) { theLEFT[X] = cnt;//record the ID of the left endpoint -     intSZ =g[x].size (); Wu      for(intI=0; i<sz;i++){ -cnt++; About DFS (G[x][i]); $     } -right[x]=cnt;//record the ID of the right endpoint - } -  A voidSolve () + { theCNT =1; -     intx,y,m; $     Charop[3]; the      for(intI=0; i<=n;i++) {tree[i]=0; vis[i]=1; G[i].clear ();} the      for(intI=1; i<n;i++){ thescanf"%d%d",&x,&y); theG[X].PB (y);//Save Diagram -     } inDfs1);//DFS sequence the      for(intI=1; i<=n;i++){ theAdd (I,1);//each point has an Apple at the beginning About     } thescanf"%d",&m); the      for(intI=0; i<m;i++){ thescanf"%s%d",op,&x); +         if(op[0]=='Q'){//interval Query -printf"%d\n", query (Right[x])-query (left[x]-1)); the}Else{Bayi             if(Vis[x]) Add (left[x],-1);//single-point modification the             ElseAdd (Left[x],1); theVIS[X]=!VIS[X];//Take counter -         } -     } the     return ; the } the  the intMain () - { the //freopen ("In.txt", "R", stdin); the //freopen ("OUT.txt", "w", stdout); the //Ios::sync_with_stdio (false);94      while(SCANF ("%d", &n)! =EOF) { the     //printf ("Case%d:", cas++); the solve (); the     }98     return 0; About}

POJ-3321 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.