LightOj 1112 Curious Robin Hood(線段樹||樹狀數組)

                                                                                         Curious Robin Hood

Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another trick. He keeps n sacks where he keeps this money. The sacks are numbered from 0 to n-1.

Now each time he can he can do one of the three tasks.

1)                  Give all the money of the ith sack to the poor, leaving the sack empty.

2)                  Add new amount (given in input) in the ith sack.

3)                  Find the total amount of money from ith sack to jth sack.

Since he is not a programmer, he seeks your help.


Input starts with an integer T (≤ 5), denoting the number of test cases.

Each case contains two integers n (1 ≤ n ≤ 105) and q (1 ≤ q ≤ 50000). The next line contains n space separated integers in the range [0, 1000]. The ith integer denotes the initial amount of money in the ith sack (0 ≤ i < n).

Each of the next q lines contains a task in one of the following form:

1 i        Give all the money of the ith(0 ≤ i < n) sack to the poor.

2 i v     Add money v (1 ≤ v ≤ 1000) to the ith(0 ≤ i < n) sack.

3 i j      Find the total amount of money from ith sack to jth sack (0 ≤ i ≤ j < n).


For each test case, print the case number first. If the query type is 1, then print the amount of money given to the poor. If the query type is 3, print the total amount from ith to jth sack.


1

5 6

3 2 1 4 5

1 4

2 3 4

3 0 3

1 2

3 0 4

1 1


Case 1:

5

14

1

13

2

題意:

   輸入T組測試數據:

   N個數據,Q個操作;

  1,i   將編號爲i的數變成0,並且輸出被改變的點的數值;

  2,i,v 將編號爲i的數加v;

  3,i,j 輸出i~j之間的總和;

思路:

       線段樹或者樹狀數組直接就over了;

      這個題和hdoj 1166 點擊打開鏈接思路基本一樣,單點更新

注意點:

        這是平時是訓練的一道題目,我有點遺憾,就在最後1秒鐘想到哪兒錯了抓狂。。。。。。。

       1 ,i  不是直接輸出,而是通過query或者getsum求,我把他當成直接輸出了

線段樹:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define lson l,mid,v<<1
#define rson mid+1,r,v<<1|1
typedef long long int LL;
const int MAX=100000;
int sum[4*MAX+20];
int num[MAX+5];

void build (int l,int r,int v)
{
	sum[v]=0;
	if(l==r)
	{
	  sum[v]=num[l];
	  return ;	
	}
	int mid=(l+r)>>1;
	build(lson);
	build(rson);
	sum[v]=sum[2*v]+sum[2*v+1] ;
}

void update(int l,int r,int v,int pos,int x)
{
	if(l==r)
	{
		sum[v]+=x;
		return ;
	}
	int mid=(l+r)>>1;
	if(pos<=mid)
	update(lson,pos,x);
	else
	update(rson,pos,x);
	sum[v]=sum[2*v]+sum[2*v+1] ;
}

int query(int l,int r,int v,int lpos,int rpos)
{
   if(lpos<=l&&r<=rpos)
    return sum[v];	
   int mid=(l+r)>>1;
   if(rpos<=mid)
   return query(lson,lpos,rpos);
   else if(lpos>mid)
   return query(rson,lpos,rpos);
   else 
   return query(lson,lpos,mid)+query(rson,mid+1,rpos);
} 

int main()  
{  
  int T,ca=1;  
  scanf("%d",&T);  
  while(T--)  
  {  
   int n,m;
   scanf("%d%d",&n,&m);  
   memset(num,0,sizeof(num));
   for(int i=1;i<=n;i++)     
    scanf("%d",&num[i]);  
   build(1,n,1);
   
   printf("Case %d:\n",ca++);
   while(m--)
   {
   	int opr;
   	int a=0,b=0,c=0;
    scanf("%d",&opr);
      if(opr==1)
      {
   	    scanf("%d",&a);
   	    a++;
   	    printf("%d\n",query(1,n,1,a,a));//這兒坑爹
	    update(1,n,1,a,-query(1,n,1,a,a));//注意
      }
      if(opr==2)
      {
	 scanf("%d%d",&a,&b);
	 a++;
	 update(1,n,1,a,b);
	 
      }
      if(opr==3)
      {
	 scanf("%d%d",&a,&b);
	 a++;b++;
	 printf("%d\n",query(1,n,1,a,b));
     }	
   }
  }   
return 0;  
}


樹狀數組:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long int LL;
LL num[500001];
int v[500001];
int n;

int lowbit(int x)
{
    return x&(-x);
}

LL getsum(int i)
{
    int sum=0;
    while(i>0)
    {
    sum+=num[i];
     i-=lowbit(i);
    }
    return sum;
}

void update(int i,int m)
{
  while(i<=n)
  {
      num[i]+=m;
      i+=lowbit(i);
  }
}

int main()
{
    int T,ca=1;
    scanf("%d",&T);
    while(T--)
    {
      int m;
	  scanf("%d%d",&n,&m);
      int i,x;
      memset(num,0,sizeof(num));
      for(i=1;i<=n;i++)
      {
        scanf("%d",&v[i]);
        update(i,v[i]);
      }
    
	  printf("Case %d:\n",ca++);
      while(m--)
      {
        int opr;
        scanf("%d",&opr);
		int l,r;
        if(opr==1)
         {
         	scanf("%d",&l);
         	l++;
         	printf("%d\n",getsum(l)-getsum(l-1));//注意
		update(l,getsum(l-1)-getsum(l)) ;//注意
	 }
        if(opr==2)
        {
          scanf("%d%d",&l,&r);
          l++,r;
          update(l,r);
	}
	if(opr==3)
	{
	  scanf("%d%d",&l,&r);
	   l++;r++;
	    printf("%d\n",getsum(r)-getsum(l-1));
	}   
      } 
    } 
    return 0;
}


發佈了221 篇原創文章 · 獲贊 17 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章