char_cha_ch @ 2022-09-03 16:04:54
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#define N 10000009
#define int long long
int st[N], en[N];
int b[N];
int lazy[N];
int whe[N];
inline void updata(int l, int r, int x)
{
int L = whe[l], R = whe[r];
if (L == R)
{
for (register int i = l;i <= r;++ i)
b[i] += x;
std::sort(b + st[L], b + en[L]);
}
else
{
for (register int i = l;i <= en[L];++ i)
b[i] += x;
for (register int i = st[R];i <= r;++ i)
b[i] += x;
std::sort(b + st[R], b + en[R]);
for (register int i = L + 1;i < R;++ i)
lazy[i] += x;
}
}
inline int query(int l, int r, int x)
{
int L = whe[l], R = whe[r], ret = 0;
if (L == R)
while (b[r] + lazy[R] >= ret && r >= l)
++ ret, -- r;
else
{
for (register int i = en[L];b[i] + lazy[L] >= x && i >= l;-- i)
++ ret;
for (register int i = r;b[i] + lazy[L] >= x && i >= st[R];-- i)
++ ret;
for (register int i = L + 1;i < R;++ i)
{
int j = en[i];
while (j >= st[i] && b[j] + lazy[i] >= x)
++ ret, -- j;
}
}
return ret;
}
signed main()
{
int n, m;
scanf("%lld%lld", &n, &m);
char opt;
int l, r, z;
for (register int i = 1;i <= n;++ i)
scanf("%lld", &b[i]);
int blo = sqrt(n);
for (register int i = 1;i <= blo;++ i)
st[i] = en[i - 1] + 1, en[i] = n / blo * i;
en[blo] = n;
for (register int i = 1;i <= blo;++ i)
{
for (register int j = st[i];j <= en[i];++ j)
whe[j] = i;
std::sort(b + st[i], b + en[i] + 1);
}
while (m --)
{
std::cin >> opt;
scanf("%lld%lld%lld", &l, &r, &z);
if (opt == 'M')
updata(l, r, z);
else
printf("%lld\n", query(l, r, z));
}
return 0;
}
by W_SUN @ 2022-10-03 09:07:11
@kirihara233 块长调整为sqrt(n)+1
即可