一个长度为nn的数列aia_i,有qq次操作:

  • M l r wM~l~r~w: [l,r][l,r]内所有数加上vv

  • A l r cA~l~r~c:询问[l,r][l,r]内大于等于cc的数的数量

n1000000,q3000,1w1000,1c1,000,000,000n\le 1000000,q\le 3000,1\le w\le 1000,1\le c\le 1,000,000,000

Luogu P2801

BZOJ3343

Solution

第一道中规中矩的分块模板题

每个块记个TagTag标记即可

Code

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
#include <bits/stdc++.h>

#define x first
#define y second
#define y1 Y1
#define y2 Y2
#define mp make_pair
#define pb push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;

template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; }

inline void proc_status()
{
ifstream t ("/proc/self/status");
cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) <<endl;
}

inline int read ()
{
int sum = 0, fl = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';
return sum * fl;
}

const int Maxn = 1000000 + 100, Block_Size = 1000 + 26, Block_Cnt = Maxn / Block_Size + 26;

int N, Q, A[Maxn], B[Maxn], Tag[Block_Cnt];
int Belong[Maxn], L[Block_Cnt], R[Block_Cnt], block_cnt;

inline void Init ()
{
for (int i = 1; i <= N; ++i) Belong[i] = (i - 1) / Block_Size + 1;
block_cnt = Belong[N];
for (int i = 1; i <= block_cnt; ++i) L[i] = (i - 1) * Block_Size + 1, R[i] = i * Block_Size;
Chkmin (R[block_cnt], N);

for (int i = 1; i <= N; ++i) B[i] = A[i];
for (int i = 1; i <= block_cnt; ++i) sort(B + L[i], B + R[i] + 1);
}

inline void Rebuild (int x)
{
for (int i = L[x]; i <= R[x]; ++i) A[i] += Tag[x], B[i] = A[i];
Tag[x] = 0;
sort(B + L[x], B + R[x] + 1);
}

inline int Query (int l, int r, int v)
{
int x = Belong[l], y = Belong[r], ans = 0;
Rebuild(x);
for (int i = l; i <= R[x] && i <= r; ++i) if (A[i] >= v) ++ans;
if (x != y)
{
Rebuild(y);
for (int i = L[y]; i <= r; ++i) if (A[i] >= v) ++ans;
}
for (int i = x + 1; i < y; ++i) ans += R[i] - (lower_bound (B + L[i], B + R[i] + 1, v - Tag[i]) - B) + 1;
return ans;
}

inline void Update (int l, int r, int v)
{
int x = Belong[l], y = Belong[r];
Rebuild(x);
for (int i = l; i <= R[x] && i <= r; ++i) A[i] += v;
Rebuild(x);
if (x != y)
{
Rebuild(y);
for (int i = L[y]; i <= r; ++i) A[i] += v;
Rebuild(y);
}
for (int i = x + 1; i < y; ++i) Tag[i] += v;
}

inline void Solve ()
{
Init();
while (Q--)
{
char Str[3];
scanf("%s", Str);
int l = read(), r = read(), v = read();
if (Str[0] == 'M') Update (l, r, v);
else printf("%d\n", Query (l, r, v));
}
}

inline void Input ()
{
N = read(), Q = read();
for (int i = 1; i <= N; ++i) A[i] = read();
}

int main()
{
#ifdef hk_cnyali
freopen("A.in", "r", stdin);
freopen("A.out", "w", stdout);
#endif
Input();
Solve();
return 0;
}