hnoj beads

http://acm.hnu.cn/online/?action=problem&type=show&id=12515


Beads
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65366KB
Total submit users: 33, Accepted users: 29
Problem 12515 : No special judgement
Problem description
A game consists of putting beads in boxes. The rules of the game are too complex to describe here, but all you need to know is that keeping track of the number of beans in adjacent boxes are very important to the outcome of the game.
You are asked by a friend to write a program to help him win the game every time. At the start of a game, all boxes are empty.
Input
The first line of the input consists of a single number T, the number of games played. Each game start with a line describing B, P and Q, the number of boxes, put requests and query requests, respectively. Then follows P + Q lines with either P i a, saying a beads are put in box number i,or Q i j, a query request for the number of beads in boxes i through j, inclusive.
Output
For each query request, output the number of beads in boxes a through b, inclusive, that are in the boxes at this point of the game.
Sample Input
1
7 5 3
P 2 1
P 3 3
P 4 7
Q 1 4
P 7 6
Q 4 4
P 6 4
Q 1 7
Sample Output
11
7
21
Judge Tips
0 < T 100 0 < B 100000 0 < P 30000 0 < Q <= 30000 0 <= a <= 100 0 < i <= j <= B Note that boxes are 1-indexed. This is an I/O-heavy problem. For Java programmers, this means that you should use BufferedReader for input reading (not Scanner). It is also bene cial to build all output in a StringBuilder before printing in a single print statement.
Problem Source
IDIOPEN 2011

直接进行树状数组的调用就好了~~ 用g++过了~~~用C就过不了~~坑啊


post code:

#include<stdio.h>
#include<string.h>
int a[1010000],b;

int lowbit(int t)   //lowbit的进制转换 查找二的指数形式
{
     return t&(t^(t-1));   
}

void insert(int pos,int num)  //插入数据更新pos到b的节点的数据的值
{
     while(pos<=b)
     {
       a[pos]+=num;
       pos+=lowbit(pos);                         
     }
}

int query(int end)    //记录1到end的和
{
   int sum=0;
   while( end>0 )
   {
     sum+=a[end];
     end-=lowbit(end);       
   }    
   return sum;
}

int main()
{
      int x,p,q,i,pos,num,beg,end,sumbeg,sumend;
      char judge[3];
      scanf("%d",&x);
      while(x--)
      {
           memset( a, 0, sizeof(a) );
           scanf("%d %d %d",&b,&p,&q);
           for(i=1;i<=p+q;i++)
              {
              scanf("%s",judge);
              if( judge[0]=='P' ){ scanf("%d %d",&pos,&num); insert(pos,num); }
              if( judge[0]=='Q' ){
                                   scanf("%d %d",&beg,&end);  //这里要注意 要求beg到end的和 需要用query(end)-query(beg-1)来求解
                                   
                                   sumbeg=query(beg-1);
                                   sumend=query(end);
                                   printf("%d\n",sumend-sumbeg);
                                 } 
           
              }



                
      }      
    
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值