HDU 1166 enemy army deployment

Source: Internet
Author: User

Analysis: line Segment tree templates, drawing analysis when learning, try not to look at others' templates, analyze them by yourself, understand the principles, and then write it out with a sense of accomplishment, that is, creating and updating. and query operations,

 #include<stdio.h>  #define N 50005  struct node{      int l,r;      int sum;  }tree[N<<2];  int a[N];  int ans,n;  void create(int x,int l,int r){      tree[x].l=l;      tree[x].r=r;      if(l==r){          tree[x].sum=a[l];          return;      }      int mid=(l+r)/2;      create(x*2,l,mid);      create(x*2+1,mid+1,r);      tree[x].sum=tree[x*2].sum+tree[x*2+1].sum;  }  void update(int x,int p,int num){      int l=tree[x].l;      int r=tree[x].r;      int mid=(l+r)/2;      if(l==r){          tree[x].sum+=num;          return;      }      if(p<=mid)          update(x*2,p,num);      else          update(x*2+1,p,num);      tree[x].sum=tree[x*2].sum+tree[x*2+1].sum;  }  void query(int x,int l,int r){      if(tree[x].l==l&&tree[x].r==r){          ans+=tree[x].sum;          return;      }      int mid=(tree[x].l+tree[x].r)/2;      if(r<=mid)          query(x*2,l,r);      else if(l>mid)          query(x*2+1,l,r);      else{          query(x*2,l,mid);          query(x*2+1,mid+1,r);      }  }  int main(){      int t,x,y,k=1;      char str[10];      scanf("%d",&t);      while(t--){          printf("Case %d:\n",k);k++;          scanf("%d",&n);          for(int i=1;i<=n;i++)              scanf("%d",&a[i]);          create(1,1,n);          while(scanf("%s",str)){              if(str[0]=='E')break;              if(str[0]=='A'){                  scanf("%d%d",&x,&y);                  update(1,x,y);              }              if(str[0]=='S'){                  scanf("%d%d",&x,&y);                  update(1,x,-y);              }              if(str[0]=='Q'){                  ans=0;                  scanf("%d%d",&x,&y);                  query(1,x,y);                  printf("%d\n",ans);              }          }      }      return 0;  }  

 

Related Article

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.