分块
终于知道分块怎么写了。
要注意特判在同一块内的操作,防止l与r分别把这个块进行了错误的处理。
https://loj.ac/problem/6277模板题
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 using namespace std; 7 typedef long long ll; 8 9 const ll Maxn = 50010; 10 11 ll a[Maxn],p[Maxn]; 12 ll n,m,opt,N; 13 ll l,r,c; 14 15 ll ask(int i){return a[i]+p[i/N];} 16 17 int main(){ 18 scanf("%lld",&n); 19 N = sqrt(n); 20 for(ll i = 1;i <= n;i++)scanf("%lld",&a[i]); 21 for(ll i = 1;i <= n;i++){ 22 scanf("%lld%lld%lld%lld",&opt,&l,&r,&c); 23 if(opt)printf("%lld\n",ask(r)); 24 else{ 25 if(l/N == r/N){ 26 for(int i = l;i <= r;i++)a[i] += c; 27 continue; 28 } 29 for(ll i = (l/N+1)*N-1;i >= l;i--)a[i] += c; 30 for(ll i = r/N*N;i <= r;i++)a[i] += c; 31 for(ll i = l/N+1;i < r/N;i++)p[i] += c; 32 } 33 // for(int i = 1;i <= n;i++)cout << ask(i) << ' ';cout << endl; 34 } 35 return 0; 36 }