题目:
https://vjudge.net/problem/UVA-12003
题意:
给出一个长度为n的序列a,有m次操作,每次操作(L,R,v,p),首先找出[L,R]内严格小于v的元素个数k,然后把a[p]更新为u∗k/(R−L+1)。输出最终的序列
思路:
无脑分块
#include <bits/stdc++.h>
using namespace std;
const int N = 300000 + 10;
int L[N], R[N], pos[N];
int sz, block;
int a[N], b[N];
void reset(int x)
{
for(int i = L[x]; i <= R[x]; i++) b[i] = a[i];
sort(b + L[x], b + 1 + R[x]);
}
void init(int n)
{
block = (int)sqrt(n * log(n) / 2.0);
//block = (int)sqrt(n);
sz = n / block;
if(n % block) sz++;
for(int i = 1; i <= n; i++) pos[i] = (i-1) / block + 1;
for(int i = 1; i <= sz; i++)
{
L[i] = (i-1) * block + 1;
R[i] = i * block;
}
R[sz] = n;
for(int i = 1; i <= sz; i++) reset(i);
}
int query(int l, int r, int x)
{
int lb = pos[l], rb = pos[r];
int ans = 0;
if(lb == rb)
{
for(int i = l; i <= r; i++)
if(a[i] < x) ans++;
}
else
{
for(int i = l; i <= R[lb]; i++)
if(a[i] < x) ans++;
for(int i = L[rb]; i <= r; i++)
if(a[i] < x) ans++;
for(int i = lb+1; i < rb; i++)
ans += lower_bound(b + L[i], b + 1 + R[i], x) - (b + L[i]);
}
return ans;
}
void update(int x, int val)
{
a[x] = val;
reset(pos[x]);
}
int main()
{
int n, m, u;
while(~ scanf("%d%d%d", &n, &m, &u))
{
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
init(n);
int l, r, v, p;
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d%d", &l, &r, &v, &p);
int k = query(l, r, v);
update(p, 1LL * u * k / (r-l+1));
}
for(int i = 1; i <= n; i++) printf("%d\n", a[i]);
}
return 0;
}