试题 前缀和

题目是这样的....
拥有 n 个数 ai(1 ≤ i ≤ n) 的你正在面临 m 个操作,每个操作是以下二者之一: 1 l r x: 将第 l 个数到第 r 个数的值改为 x 2 l r: 询问第 l 个数到第 r 个数的值的和。

输入:第一行包含两个非负整数 n, m。 第二行 n 个整数 ai。 接下来 m 行,每行一个操作,格式如上所述。

输出:对于每一个询问操作,输出一行一个整数为答案。

明显线段树...然后本蒟蒻只打了N^2....

#include<iostream>
#include<cstdio>
#define tcl(a,b,c) for(a=b;a<=c;a++)
using namespace std;
long long a[10001];
int main()
{
    int n,m,i,j,t;
    scanf("%d%d",&n,&m);
    tcl(i,1,n)
    {
        scanf("%lld",&a[i]);
    }
    tcl(i,1,m)
    {
        scanf("%d",&t);
        if(t==1)
        {
            int n1,n2;
            long long x;
            scanf("%d%d%lld",&n1,&n2,&x);
            tcl(j,n1,n2)
            {
                a[j]=x;
            }
        } 
        else
        {
            int n3,n4;
            long long sum=0;
            scanf("%d%d",&n3,&n4);
            tcl(j,n3,n4)
            {
                sum+=a[j];		
            }
            printf("%lld
",sum);
        }
    }
    return 0;
}	

然后.....A了?谢谢数据

原文地址:https://www.cnblogs.com/LSWorld/p/test1.html