HDU 1166 Enemy soldiers (tree-like array)

Source: Internet
Author: User

Main topic:

Give you a sequence of length n, and then you have the following actions:

Q: Query the and of the [x, Y] interval.

A: For A[x]+y

S: For A[x]-y

E: End Query

Problem Solving Ideas:

Because n is large, the number of inquiries is also very large, the worst case is that whenever I do a or S, I do a q, such a complexity is n*m,,, will definitely t, then we need to solve the problem of query and modification in the Logn time, think of course is the use of tree-like array, A template question for a bare-naked tree-like array.

Code:

# include<cstdio># include<iostream># include<fstream># include<algorithm># include<functional># include<cstring># include<string># include<cstdlib># include<iomanip># include<numeric># include<cctype># include<cmath># include<ctime># include<queue># include<stack># include<list># include<Set># include<map>using namespacestd;Const DoublePi=4.0*atan (1.0); typedefLong Longll;typedef unsignedLong Longull;# define INF999999999# define MAX50000+4intA[max];intTree[max];Charss[ at];intN;intRead (intPOS) {    intAns =0;  while(Pos >0) {ans+=Tree[pos]; POS-=pos& (-POS); }    returnans;}voidUpdateintPosintval) {     while(Pos <=N) {Tree[pos]+=Val; POS+=pos& (-POS); }}voidinit () { for(inti =1; I <= n;i++) {update (i,a[i]); }}intMainvoid){    intIcase =1; intT;SCANF ("%d",&t);  while(t--) {memset (A,0,sizeof(a)); memset (Tree,0,sizeof(tree)); scanf ("%d",&N);  for(inti =1; I <= n;i++) {scanf ("%d",&A[i]); }        //GetChar ();init (); printf ("Case %d:\n", icase++);  while(SCANF ("%s", SS)) {             if(ss[0] =='E' )                 Break; intX, Y;SCANF ("%d%d",&x,&y); if(ss[0] =='Q' )            {                intANS1 = Read (X-1); intAns2 =read (y); cout<<ans2-ans1<<Endl; }            Else if(ss[0] =='A'{update (x, y); }            Else if(ss[0] =='S') {Update (x, (-1)*y); }        }    }    return 0;}

HDU 1166 Enemy soldiers (tree-like array)

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.