分块——入门2

#水题爆炸记
这里写图片描述

woc这题卡了我一天,这么水的题,我真的**无语,先是发现二分写错了,然后发现建立块都写错了,我真的无语了。
上代码(舞草):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//By Bibi
/// .-~~~~~~~~~-._ _.-~~~~~~~~~-.
/// __.' ~. .~ `.__
/// .'// \./ \\`.
/// .'// | \\`.
/// .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`.
/// .'//.-" `-. | .-' "-.\\`.
/// .'//______.============-.. \ | / ..-============.______\\`.
/// .'______________________________\|/______________________________`.
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define dep(i,a,b) for(int i=a;i>=b;--i)
#define pl sort(d+l[be[L]],d+r[be[L]]+1)
#define pr sort(d+l[be[R]],d+r[be[R]]+1)
using namespace std;
const int MAXN=5e4+5;
int read()
{
int sum=0,flag=1;
char c;
for(;c<'0'||c>'9';c=getchar())if(c=='-') flag=-1;
for(;c>='0'&&c<='9';c=getchar())sum=(sum<<1)+(sum<<3)+c-'0';
return sum*flag;
}
int n;
int a[MAXN],op,c;
int d[MAXN];
int num;
int block;
int be[MAXN],l[MAXN],r[MAXN],L,R;
int p[MAXN];
void build()
{
block=sqrt(n);
num=n/block;
if(n%block) num++;
rep(i,1,n) be[i]=(i-1)/block+1,d[i]=a[i];
rep(i,1,num)
{
l[i]=(i-1)*block+1;
r[i]=i*block;
}
r[num]=n;
rep(i,1,num) sort(d+l[i],d+r[i]+1);
}
void ask()
{
int ans=0;
if(be[L]==be[R])
{
rep(i,L,R) if(a[i]+p[be[i]]<c) ans++;
printf("%d\n",ans);
}
else
{
rep(i,L,r[be[L]]) if(a[i]+p[be[i]]<c) ans++;
rep(i,l[be[R]],R) if(a[i]+p[be[i]]<c) ans++;
rep(i,be[L]+1,be[R]-1)
{
int ll=l[i],rr=r[i],result=0,mid;
if(d[l[i]]+p[i]>=c) continue;
while(ll+1<rr)
{
mid=(ll+rr)>>1;
if(d[mid]+p[i]<c) ll=mid;
else rr=mid;
}
if(d[rr]+p[i]<c) ans+=rr-l[i]+1;
else ans+=ll-l[i]+1;
}
printf("%d\n",ans);
}
}
void add()
{
if(be[L]==be[R])
{
rep(i,L,R) a[i]+=c;
rep(i,l[be[L]],r[be[L]]) d[i]=a[i];
pl;
}
else
{
rep(i,L,r[be[L]]) a[i]+=c;
rep(i,l[be[L]],r[be[L]]) d[i]=a[i];
pl;
rep(i,l[be[R]],R) a[i]+=c;
rep(i,l[be[R]],r[be[R]]) d[i]=a[i];
pr;
rep(i,be[L]+1,be[R]-1) p[i]+=c;
}
}
void init()
{
n=read();
rep(i,1,n) a[i]=read();
build();
rep(i,1,n)
{
op=read();
if(op)
{
L=read(),R=read(),c=read(),c=c*c;
ask();
}
else
{
L=read(),R=read(),c=read();
add();
}
}
}
int main()
{
init();
return 0;
}