题目链接:
题目:
出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及区间加法,单点查值。
输入格式
第一行输入一个数字 nnn。
第二行输入 nnn 个数字,第 iii 个数字为 aia_iai,以空格隔开。
接下来输入 nnn 行询问,每行输入四个数字 opt\mathrm{opt}opt、lll、rrr、ccc,以空格隔开。
若 opt=0\mathrm{opt} = 0opt=0,表示将位于 [l,r][l, r][l,r] 的之间的数字都加 ccc。
若 opt=1\mathrm{opt} = 1opt=1,表示询问 ara_rar 的值(lll 和 ccc 忽略)。
输出格式
对于每次询问,输出一行一个数字表示答案。
样例
样例输入
4
1 2 2 3
0 1 3 1
1 0 1 0
0 1 2 2
1 0 2 0
样例输出
2
5
数据范围与提示
对于 100% 100\% 100% 的数据,1≤n≤50000,−231≤others 1 \leq n \leq 50000, -2^{31} \leq \mathrm{others}1≤n≤50000,−231≤others、ans≤231−1 \mathrm{ans} \leq 2^{31}-1 ans≤231−1。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int main() {
int n,opt,r,l,c;
long long a[50003],f[50003],sum[50003];
scanf("%d",&n);
int k=sqrt(n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
f[i]=(i-1)/k+1;//用来记录是第几块的...
}
for(int i=1;i<=n;i++){
scanf("%d %d %d %d",&opt,&l,&r,&c);
if(opt==1){
printf("%d\n",a[r]+sum[f[r]]);
}
else {//搞他!
for(int j=l;j<=min((int)f[l]*k,r);j++)
a[j]+=c;//如果l,r在一块里,就把l~r都加c,反之先把l所在的一块加c
if(f[l]!=f[r])//如果l,r不在一块,把r所在块r前的加c
for(int j=(f[r]-1)*k+1;j<=r;j++)
a[j]+=c;
for(int j=f[l]+1;j<f[r];j++)//把l块和r块之间的每个sum搞一下
sum[j]+=c;
}
}
}
//但是我还是不懂它为啥能节省时间...