求助,分块为什么最后3点WA
查看原帖
求助,分块为什么最后3点WA
305002
vegetable_ste楼主2021/5/14 23:01
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10, sqrN = 330;
int n, m;
ll a[N];
int id[N], L[sqrN], R[sqrN], cnt = 0;
ll add[sqrN], sum[sqrN];
int main() {
  scanf("%d%d", &n, &m);
  for(int i = 1; i <= n; i ++ ) scanf("%lld", a + i);
  int t = 1, len = floor(sqrt(n * 1.0));
  for(; t <= n; t += len ) {
    cnt ++ ;
    L[cnt] = t;
    R[cnt] = t + len - 1;
    for(int j = t; j < t + len; j ++ ) id[j] = cnt;
    for(int j = t; j < t + len; j ++ ) sum[cnt] += a[j];
  }
  R[cnt] = n;
  for(int i = 1; i <= m; i ++ ) {
    int op; scanf("%d", &op);
    int x, y; ll k = 0;
    if(op == 1) {
      scanf("%d%d%lld", &x, &y, &k);
      int ix = id[x], iy = id[y];
      if(ix == iy || ix == iy - 1)
        for(int j = x; j <= y; j ++ ) a[j] += k, sum[id[j]] += k; // N
      else {
        for(int j = ix + 1; j <= iy - 1; j ++ )
          add[j] += k; // sqrN
        for(int j = x; j <= R[ix]; j ++ ) a[j] += k, sum[ix] += k; // N
        for(int j = L[iy]; j <= y; j ++ ) a[j] += k, sum[iy] += k; // N
      }
    } else {
      scanf("%d%d", &x, &y);
      ll ans = 0;
      int ix = id[x], iy = id[y];
      if(ix == iy || ix == iy - 1)
        for(int j = x; j <= y; j ++ ) ans += add[id[j]] + a[j]; // N
      else {
        for(int j = ix + 1; j <= iy - 1; j ++ ) // sqrN
          ans += (R[j] - L[j] + 1) * add[j] + sum[j];
        for(int j = x; j <= R[ix]; j ++ ) ans += a[j] + add[ix]; // N
        for(int j = L[iy]; j <= y; j ++ ) ans += a[j] + add[iy]; // N
      }
      printf("%lld\n", ans);
    }
  }
  return 0;
}
2021/5/14 23:01
加载中...