#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <cmath> using namespace std; using LL = long long; LL getint(); int N, M; LL block, val[500005], blg[500005], tag[1005]; void add(int l, int r, int key); int main(){ N = getint(), block = sqrt(N); int i; for(i = 1; i <= N; i++) val[i] = getint(); for(i = 1; i <= N; i++) blg[i] = (i - 1) / block + 1; for(i = 1; i <= N; i++){ int op, x, y, key; op = getint(), x = getint(); y = getint(), key = getint(); if(op == 0){ add(x, y, key); }else printf("%lld\n", val[y] + tag[blg[y]]); } return 0; } void add(int l, int r, int key){ int i, top; for(i = l, top = min(blg[l] * block, 1LL * r); i <= top; i++) val[i] += key; if(blg[l] != blg[r]) for(i = (blg[r] - 1) * block + 1; i <= r; i++) val[i] += key; for(i = blg[l] + 1; i <= blg[r] - 1; i++) tag[i] += key; } LL getint(){ LL res = 0, mark = 1; char ch = getchar(); while(ch<‘0‘ || ch>‘9‘){ if(ch == ‘-‘) mark = -1; ch = getchar(); } while(ch >= ‘0‘ && ch <= ‘9‘) res = 10 * res + ch - ‘0‘, ch = getchar(); return mark * res; }
原文:https://www.cnblogs.com/downrainsun/p/11454870.html