数列分块入门 1

Description

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

Input

第一行输入一个数字 n。
第二行输入 n 个数字,第 i 个数字为 ai,以空格隔开。
接下来输入 n行询问,每行输入四个数字 opt、l、r、c,以空格隔开。
若 opt=0,表示将位于 [l,r]的之间的数字都加 c。
若 opt=1,表示询问 ar 的值(l 和 c 忽略)。
1≤n≤50000,-2^31 ≤others、ans≤2^31-1 

Output

对于每次询问,输出一行一个数字表示答案。

Sample Input

4
1 2 2 3
0 1 3 1
1 0 1 0
0 1 2 2
1 0 2 0

Sample Output

2
5

区间加法,单点查询。

基础的东西 。
分块 每个块的大小都是sqrt(n) 
如果是完整的块就直接更新到标记里
不完整的块就直接暴力更新。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cmath>
 5 using namespace std;
 6 const int maxn=50010;
 7 int n,bot;
 8 int a[maxn],bl[maxn],tag[maxn];
 9 inline void add(int l,int r,int c)
10 {
11     for(int i=l;i<=min(r,bl[l]*bot);i++) a[i]+=c;
12     if(bl[l]!=bl[r])
13         for(int i=(bl[r]-1)*bot+1;i<=r;i++) a[i]+=c; 
14     for(int i=bl[l]+1;i<=bl[r]-1;i++) tag[i]+=c;
15 }
16 int main()
17 {
18     scanf("%d",&n);
19     bot=sqrt(n);
20     for(int i=1;i<=n;i++)
21     {
22         scanf("%d",&a[i]);
23         bl[i]=(i-1)/bot+1;
24     }
25     int opt,l,r,c;
26     for(int i=1;i<=n;i++)
27     {
28         scanf("%d%d%d%d",&opt,&l,&r,&c);
29         if(opt==0) add(l,r,c);
30         else printf("%d\n",a[r]+tag[bl[r]]);
31     }
32     return 0;
33 } 
View Code

 

posted @ 2018-08-15 15:41  Dingwenh  阅读(192)  评论(0编辑  收藏  举报