推荐博客:http://hzwer.com/8053.html

分块入门题库:https://loj.ac/problems/search?keyword=%E5%88%86%E5%9D%97

 

分块1:给出一个长为n的数列,以及n个操作,操作涉及区间加法,单点查值。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<cmath>
 5 using namespace std;
 6 const int maxn=5e4+10;
 7 const int maxm=1e3+10;
 8 int a[maxn],belong[maxn];
 9 int atag[maxm];
10 int n,sz;
11 
12 void add(int l,int r,int c)
13 {
14     for ( int i=l;i<=min(belong[l]*sz,r);i++ ) a[i]+=c;
15     if ( belong[l]!=belong[r] ) {
16         for ( int i=(belong[r]-1)*sz+1;i<=r;i++ ) a[i]+=c;
17     }
18     for ( int i=belong[l]+1;i<=belong[r]-1;i++ ) atag[i]+=c;
19 }
20 
21 int main()
22 {
23     int i,j,k,opt,l,r,c;
24     while ( scanf("%d",&n)!=EOF ) {
25         sz=sqrt(n);
26         for ( i=1;i<=n;i++ ) {
27             scanf("%d",&a[i]);
28             belong[i]=(i-1)/sz+1;
29         }
30         memset(atag,0,sizeof(atag));
31         for ( i=1;i<=n;i++ ) {
32             scanf("%d%d%d%d",&opt,&l,&r,&c);
33             if ( opt==0 ) add(l,r,c);
34             else if ( opt==1 ) printf("%d\n",a[r]+atag[belong[r]]);
35         }
36     }
37     return 0;
38 }
分块1

相关文章:

  • 2021-07-18
  • 2021-10-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案