Portal
Portal1: LibreOJ
Description
A given length \ (n-\) is the number of columns, and \ (n-\) operations, the operation involving the addition interval, a single check-point value.
Input
The first input line of a digital \ (n-\) .
The second row input \ (n-\) digits, the \ (I \) digits of \ (a_i \) , separated by a space.
Now enter \ (n-\) line of inquiry, four digits per line \ (opt \) , \ (L \) , \ (R & lt \) , \ (C \) , separated by a space.
If \ (\ texttt opt = {0} \) , represents located \ ([l, r] \ ) numbers are added between \ (C \) .
If \ (\ texttt opt = {}. 1 \) , indicating an inquiry \ (a_i \) values ( \ (L \) and \ (C \) is ignored).
Output
For each inquiry, the output line number indicates the answer.
Sample Input
4
1 2 2 3
0 1 3 1
1 0 1 0
0 1 2 2
1 0 2 0
Output
2
5
Hint
For \ (100 \% \) data, \ (. 1 \ n-Le \ 50000 Le, 31 is -2 ^ {} \ Le Others, ANS \ Le 31 is {2} ^ -. 1 \) .
Solution
Block, the first sequence into \ (\ sqrt {n} \ ) blocks, when the addition interval, the left and right trim piece violent process, the entire block lazy updating flag. Single-point evaluation, as long as the value of their own in which it blocks labeled lazy add it.
Code
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int MAXN = 50005;
int n, a[MAXN], bl[MAXN], tag[MAXN];
int main() {
scanf("%d", &n);
int block = (int)sqrt(n);//总块数
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
bl[i] = (i - 1) / block;//分块
}
for (int i = 1; i <= n; i++) {
int opt, x, y, val;
scanf("%d%d%d%d", &opt, &x, &y, &val);
if (opt == 0) {
if (bl[x] == bl[y]) {
for (int i = x; i <= y; i++)
a[i] += val;//如果区间不包含任何块
} else {
for (; bl[x] == bl[x - 1]; x++)
a[x] += val;//处理边角料
for (; bl[y] == bl[y + 1]; y--)
a[y] += val;//处理边角料
for (int i = x; i <= y; i += block)
tag[bl[i]] += val;//更新整块的懒标记
}
} else printf("%d\n", a[y] + tag[bl[y]]);//单点求值
}
return 0;
}
Attachment
Test data Download: https://www.lanzous.com/i51c8of