Loj 6278. 数列分块入门 2

题目描述

给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及区间加法,询问区间内小于某个值 xxx 的元素个数。

输入格式

第一行输入一个数字 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,表示询问 [l,r][l, r][l,r] 中,小于 c2c^2c2 的数字的个数。

输出格式

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

样例

样例输入

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

样例输出

3
0
2

数据范围与提示

对于 100% 100\%100% 的数据,1≤n≤50000,−231≤others 1 \leq n \leq 50000, -2^{31} \leq \mathrm{others}1n50000,231others、ans≤231−1 \mathrm{ans} \leq 2^{31}-1ans2311。

 1 #include<bits/stdc++.h>
 2 using namespace std; 
 3 
 4 #define F(i,a,b) for(int i=a;i<=b;i++) 
 5 #define D(i,a,b) for(int i=a;i>=b;i--) 
 6 #define ms(i,a)  memset(a,i,sizeof(a)) 
 7 #define LL       long long 
 8 #define st(x)    ((x-1)*B+1)
 9 #define ed(x)    min(n,x*B)
10 #define bl(x)    ((x-1)/B+1) 
11 
12 int inline read(){
13     int x=0,w=0; char c=getchar(); 
14     while (c<'0' || c>'9') w+=c=='-',c=getchar(); 
15     while (c>='0' && c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar(); 
16     return w? -x: x;  
17 }
18 
19 int const maxn=50003; 
20 int n,B; 
21 LL a[maxn],b[maxn],c[maxn],add[250];  
22 
23 void update(int l,int r,int z){
24     int x=bl(l); 
25     int y=bl(r); 
26     if(x==y){
27         F(i,st(x),ed(x)) c[i]=a[i];  
28         F(i,l,r)   c[i]=a[i]=a[i]+z; 
29         sort(c+st(x),c+ed(x)+1);  
30         F(i,st(x),ed(x)) b[i]=c[i];   
31     }else {
32         F(i,st(x),ed(x)) c[i]=a[i];  
33         F(i,l,ed(x)) c[i]=a[i]=a[i]+z; 
34         sort(c+st(x),c+ed(x)+1) ; 
35         F(i,st(x),ed(x)) b[i]=c[i];  
36         F(i,st(y),ed(y)) c[i]=a[i]; 
37         F(i,st(y),r) c[i]=a[i]=a[i]+z; 
38         sort(c+st(y),c+ed(y)+1); 
39         F(i,st(y),ed(y)) b[i]=c[i];  
40         F(i,x+1,y-1) add[i]+=z;   
41     }
42 }
43 
44 int find(int l,int r,int k,LL z){
45     if(b[r]+add[k]<z) return r-l+1;   
46     int s=l;  
47     while (l<r){
48         int mid=(l+r)/2; 
49         if(b[mid]+add[k]>=z) r=mid; 
50         else l=mid+1;  
51     }
52     return r-s;  
53 }
54 int query(int l,int r,LL z){
55     int x=bl(l); 
56     int y=bl(r);  
57     int ans=0; 
58     z=z*z;   
59     if(x==y){
60         F(i,l,r) if(a[i]+add[x]<z) ans++;  
61     }else {
62         F(i,l,ed(x)) if(a[i]+add[x]<z) ans++; 
63         F(i,st(y),r) if(a[i]+add[y]<z) ans++; 
64         F(i,x+1,y-1) ans+=find(st(i),ed(i),i,z);  
65     }
66     return ans; 
67 
68 }
69 int main(){
70     n=read(); 
71     F(i,1,n) a[i]=b[i]=read(); 
72     B=(int) sqrt(n);  
73     F(i,1,bl(n)) sort(b+st(i),b+ed(i)+1);  
74     F(i,1,n){
75         int x=read(); 
76         int l=read(); 
77         int r=read(); 
78         int c=read();  
79         if(l>r) swap(l,r);  
80         if(x==0) update(l,r,c); 
81         else printf("%d\n",query(l,r,c)); 
82     }
83     return 0; 
84 }
View Code

 

posted @ 2018-09-25 21:33  zjxxcn  阅读(233)  评论(0编辑  收藏  举报