链接: https://www.nowcoder.com/acm/contest/77/B
来源:牛客网

题目描述

给一个数列,会有多次询问,对于每一次询问,会有两种操作:
1:给定两个整数x, y, 然后在原数组的第x位置上加y;

2:给定两个整数l,r,然后输出数组从第l位加到第r位数字的和并换行

输入:

第一行有两个整数n, m(1 <= n, m <= 100000)代表数列的长度和询问的次数
第二行n个数字,对于第i个数字a[i],(0<=a[i]<=100000)。
接下来m行,每一行有三个整数f, x, y。第一个整数f是1或者是2,代表操作类型,如果是1,接下来两个数x,y代表第x的位置上加y,如果是2,则求x到y的和,保证数据合法。

输出:

输出每次求和的结果并换行

Example:

输入:

10 2
1 2 3 4 5 6 7 8 9 10
1 1 9
2 1 10

输出:

64

MY  DaiMa:

//#include<stdio.h>
//int main()
//{
//    long int a[100002],n,m,f,x,y,i,j,sum;
//    scanf("%ld%ld",&n,&m);
//    for(i=1;i<=n;i++)
//        scanf("%ld",&a[i]);
//    while(m--)
//    {
//        scanf("%ld%ld%ld",&f,&x,&y);
//        if(f==1)
//        {
//            a[x]+=y;
//        }
//        else if(f==2)
//        {
//            sum=0;
//            for(j=x;j<=y;j++)
//                sum+=a[j];
//            printf("%ld\n",sum);
//        }
//    }
//    return 0;
//}

这种写法的话会 运行超时,所以要在输入时就求和,然后最后在减去重复的
#include<stdio.h>
int main()
{
    long int a[100002],n,m,f,x,y,i,sum=0,s;
    scanf("%ld%ld",&n,&m);
    for(i=1;i<=n;i++)
    {
        scanf("%ld",&a[i]);
        sum=sum+a[i];
    }
    while(m--)
    {
        scanf("%ld%ld%ld",&f,&x,&y);
        if(f==1)
        {
            sum=sum+y;
            a[x]+=y;
        }
        else
        {
            s=sum;
            for(i=1;i<x;i++)
                s-=a[i];
            for(i=y+1;i<=n;i++)
                s-=a[i];
            printf("%ld\n",s);
        }
    }
    return 0;
}