链接:
题意:
给出一个长为 的数列,以及 个操作,操作涉及区间加法,询问区间内小于某个值 的元素个数。
思路:
分块,用vector维护每个区域的数值,每次通过二分去找满足的值.
每次单个修改时
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 5e4+10;
int a[MAXN], Tag[MAXN];
int Rank[MAXN];
vector<int> Vec[MAXN];
int n, part;
void Re(int pos)
{
Vec[pos].clear();
for (int i = (pos-1)*part+1;i <= min(pos*part, n);i++)
Vec[pos].push_back(a[i]);
sort(Vec[pos].begin(), Vec[pos].end());
}
void Update(int l, int r, int c)
{
for (int i = l;i <= min(r, Rank[l]*part);i++)
a[i] += c;
Re(Rank[l]);
if (Rank[l] != Rank[r])
{
for (int i = (Rank[r]-1)*part+1;i <= r;i++)
a[i] += c;
Re(Rank[r]);
}
for (int i = Rank[l]+1;i <= Rank[r]-1;i++)
Tag[i] += c;
}
int Query(int l, int r, int v)
{
int cnt = 0;
for (int i = l;i <= min(r, Rank[l]*part);i++)
{
if (a[i] + Tag[Rank[i]] < v)
cnt++;
}
if (Rank[l] != Rank[r])
{
for (int i = max((Rank[r]-1)*part+1, l);i <= r;i++)
{
if (a[i]+Tag[Rank[i]] < v)
cnt++;
}
}
for (int i = Rank[l]+1;i <= Rank[r]-1;i++)
cnt += lower_bound(Vec[i].begin(), Vec[i].end(), v-Tag[i])-Vec[i].begin();
return cnt;
}
int main()
{
scanf("%d", &n);
part = sqrt(n);
for (int i = 1;i <= n;i++)
scanf("%d", &a[i]);
for (int i = 1;i <= n;i++)
{
Rank[i] = (i - 1) / part + 1;
Vec[Rank[i]].push_back(a[i]);
}
for (int i = 1;i <= Rank[n];i++)
sort(Vec[i].begin(), Vec[i].end());
int op, l, r, c;
for (int i = 1;i <= n;i++)
{
scanf("%d %d %d %d", &op, &l, &r, &c);
if (op == 0)
Update(l, r, c);
else
printf("%d
", Query(l, r, c*c));
}
return 0;
}