CE求助
  • 板块学术版
  • 楼主Chinshyo
  • 当前回复3
  • 已保存回复3
  • 发布时间2022/8/16 07:03
  • 上次更新2023/10/27 15:13:26
查看原帖
CE求助
312820
Chinshyo楼主2022/8/16 07:03

RT,此代码在我的电脑上运行可以正确地输出答案,但是提交就会CE。

评测记录

错误信息

Main.cc:2:24: bits/stdc++.h: No such file or directory
Main.cc: In function `int main()':
Main.cc:56: error: `scanf' undeclared (first use this function)
Main.cc:56: error: (Each undeclared identifier is reported only once for each function it appears in.)
Main.cc:62: error: `sqrt' undeclared (first use this function)
Main.cc:91: error: `printf' undeclared (first use this function)

代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

int n, q;
const int MaxN = 1e5 + 114;
ll a[MaxN], sum[MaxN], add_tag[MaxN];
int L[MaxN], R[MaxN], pos[MaxN];

void add(int l, int r, ll d) {
    if(pos[l] == pos[r]) {
        for(int i = l; i <= r; i++) {
            a[i] += d;
        }
        sum[pos[l]] += d * (r - l + 1);
    } else {
        for(int i = l; i <= R[pos[l]]; i++) {
            a[i] += d;
        }
        sum[pos[l]] += d * (R[pos[l]] - l + 1);
        for(int i = L[pos[r]]; i <= r; i++) {
            a[i] += d;
        }
        sum[pos[r]] = d * (r - L[pos[r]] + 1);
        for(int i = pos[l] + 1; i <= pos[r] - 1; i++) {
            add_tag[i] += d;
        }
    }
}

ll ask(int l, int r) {
    ll res = 0;
    if(pos[l] == pos[r]) {  
        for(int i = l; i <= r; i++) {
            res += a[i];
        }
        return res + (r - l + 1) * add_tag[pos[l]];
    } else {
        for(int i = l; i <= R[pos[l]]; i++) {
            res += a[i];
        }
        res += add_tag[pos[l]] * (R[pos[l]] - l + 1);
        for(int i = L[pos[r]]; i <= r; i++) {
            res += a[i];
        }
        res += add_tag[pos[r]] * (r - L[pos[r]] + 1);
        for(int i = pos[l] + 1; i <= pos[r] - 1; i++) {
            res += sum[i] + add_tag[i] * (R[i] - L[i] + 1);
        }
        return res;
    }
}

int main() {
    scanf("%d%d", &n, &q);

    for(int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
    }

    int t = sqrt(n);
    for(int i = 1; i <= t; i++) {
        L[i] = (i - 1) * t + 1;
        R[i] = i * t;
    }
    if(R[t] < n) {
        t++;
        L[t] = R[t - 1] + 1;
        R[t] = n;
    } 

    for(int i = 1; i <= t; i++) {
        for(int j = L[i]; j <= R[i]; j++) {
            pos[j] = i;
            sum[i] += a[j];
        }
    }

    for(int i = 1; i <= q; i++) {
        char opt[5];
        scanf("%s", opt);
        //cout << "> " << opt << endl; 
        if(opt[0] == 'C') {
        	int l, r, d;
        	scanf("%d%d%d", &l, &r, &d);
            add(l, r, d);
        } else if(opt[0] == 'Q'){
        	int l, r;
        	scanf("%d%d", &l, &r);
            printf("%lld\n", ask(l, r));
        }
    }
    return 0;
}
2022/8/16 07:03
加载中...