题目:Most Influential Pumpkin
分析:
将原序列a分块,并保持块内元素升序排列,存于b。
对于每次查询l, r,左右两端的块的暴力+1,中间的块的懒惰标记+1.
设上一次的中位数为mid,如果当前更新完之后,数组a中<=mid的数< n / 2 + 1,那么mid++
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;//三年竞赛一场空,不开long long见祖宗
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define x first
#define y second
typedef pair<int, int> pii;
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int inf = 0x3f3f3f3f;
int a[maxn], b[maxn];
int l[maxn], r[maxn], id[maxn];
int add[maxn];
int n, k;
int len;
void resort(int num)
{
for(int i = l[num]; i <= r[num]; i++) b[i] = a[i];
sort(b + l[num], b + r[num] + 1);
}
void update(int ll, int rr)
{
if(id[ll] == id[rr])
{
for(int i = ll; i <= rr; i++)
a[i]++;
resort(id[ll]);
return;
}
for(int i = id[ll] + 1; i <= id[rr] - 1; i++) add[i]++;
for(int i = ll; i <= r[id[ll]]; i++) a[i]++;
resort(id[ll]);
for(int i = rr; i >= l[id[rr]]; i--) a[i]++;
resort(id[rr]);
}
int getnum(int x)
{
int res = 0;
for(int i = id[1]; i <= id[n]; i++)
{
int ll = l[i], rr = r[i];
res += upper_bound(b + ll, b + rr + 1, x - add[i]) - b - ll;
}
return res;
}
int main()
{
#ifdef ONLINE_JUDGE
freopen("f.in", "r", stdin);
//freopen("out.txt","w",stdout);
#endif
while(cin >> n >> k && n && k)
{
len = sqrt(n);
for(int i = 1; i <= n; i++) cin >> a[i], b[i] = a[i];
sort(b + 1, b + n + 1);
int midnum = b[n / 2 + 1];
for(int i = 1; i <= n; i++)
id[i] = (i - 1) / len + 1;
for(int i = id[1]; i <= id[n]; i++)
{
add[i] = 0;
l[i] = (i - 1) * len + 1;
r[i] = min(n, len * i);
}
for(int i = id[1]; i <= id[n]; i++)
resort(i);
while(k--)
{
int ll, rr; cin >> ll >> rr;
update(ll, rr);
if(getnum(midnum) < n / 2 + 1) midnum++;
cout << midnum << endl;
}
}
}