neu 1492 Segment Balls(思维 水)

http://acm.neu.edu.cn/hustoj/problem.php?id=1492

 

题意: 有n个盒子

         现在有两种操作:

                               1 在序号为x的倍数的盒子里放y个球

                               2 查询序号x到y的盒子里的球的总数

 

思路: 当时以为线段数 而且没看明白 number is multiple of B 就放弃了

         其实 题目中给出 1 <= B ,C <= 10 已经暗示了这题不需要线段树

         只需要在操作1 时 a[x]+=y;

         再通过 (y/i-(x-1)/i)*a[i]  来计算总量 

 

过了时间竟然不能提交了 暂且贴个代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
long long int a[20];
int main()
{
    int i,j,k;
    int t,n,q;
    int x,y;
    char que[50];
    scanf("%d",&t);
    while(t--)
    {
        memset(a,0,sizeof(a));
        scanf("%d%d",&n,&q);
        while(q--)
        {
            scanf("%s%d%d",que,&x,&y);
            if(que[0]=='P')
            {
                a[x]+=y;
            }
            else if(que[0]=='Q')
            {
                long long  int ans=0;
                for(i=1;i<=10;i++)
                {
                    if(y<i) break;
                    if(a[i]!=0)
                    {
                        ans+=(y/i-(x-1)/i)*a[i];
                    }
                }
                printf("%lld\n",ans);
            }
        }
    }
    return 0;
}

 

posted @ 2015-02-01 13:40  sola94  阅读(121)  评论(0编辑  收藏  举报