HDU 4417
题意:
给一个长度为n(n<=1e5)的数组
给q个询问,每个询问有l,r,x三个数
每个询问就是查询[l,r]中有多少个<=x的数
解法:
先将原数组A排序,并保留原有的下标,排序后的数组为B数组
然后对询问按x从小到大排序
for询问
对每个x暴力处理每一个小于x的B数组元素在原数组A的下标pos,用树状数组对pos+1
这样就说明在pos这个位置有1个小于x的值
再计算下[L,R]之间有多少个1就行了就可以了
暴力处理下标那里可以像尺取一样瞎搞
所以总的复杂度应该是nlogn+qlogn之类的吧。。大概。。
修改+查询还看不懂-_-看懂再补
#include <bits/stdc++.h>
#define make_pair mp
#define first X
#define second Y
using namespace std;
typedef long long LL;
const int maxn = 100000 + 5;
int n, c[maxn], ans[maxn];
struct x {
int d;
int p;
bool operator <(x c)const {
return d < c.d;
};
} a[maxn];
struct query {
int l, r, x;
int id;
bool operator <(query c) const {
return x < c.x;
}
} Q[maxn];
void add(int x) {
while(x <= n) {
c[x]++;
x += (x & (-x));
}
}
int sum(int x) {
int s = 0;
while(x > 0) {
s += c[x];
x -= (x & (-x));
}
return s;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCaL
int t;
cin >> t;
for(int k = 1; k <= t; k++) {
memset(c, 0, sizeof c);
int q;
scanf("%d%d", &n, &q);
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i].d);
a[i].p = i;
}
sort(a + 1, a + 1 + n);
for(int i = 0; i < q; i++) {
scanf("%d%d%d", &Q[i].l, &Q[i].r, &Q[i].x);
Q[i].id = i;
Q[i].l++;
Q[i].r++;
}
sort(Q, Q + q);
int l = 1;
for(int i = 0; i < q; i++) {
x tmp;
tmp.p = 1;
tmp.d = Q[i].x;
int pos = upper_bound(a + 1, a + 1 + n, tmp) - a;
while(l < pos) {
add(a[l].p);
l++;
}
ans[Q[i].id] = sum(Q[i].r) - sum(Q[i].l - 1);
}
printf("Case %d:\n", k);
for(int i = 0; i < q; i++)
printf("%d\n", ans[i]);
}
return 0;
}