#include <iostream>
#include <algorithm>
#include <map>
#include <stack>
#include <queue>
#include <string.h>
#include <vector>
#include <math.h>
#include <stdio.h>
using namespace std;
const int N = 500010;
int n, ans, tot, a[N], c[N], k[N];
map<int, int> dis;
void Dis() {
dis.clear(), tot = 0;
memcpy(k, a, sizeof a);
sort(k+1, k+1+n);
ans = 0;
for (int i = 1; i <= n; ++i) {
if (!dis[k[i]]) {
dis[k[i]] = ++tot;
}
c[i] = 0;
}
for (int i = 1; i <= n; ++i) {
a[i] = dis[a[i]];
}
}
int lowBit(int x) {
return x & -x;
}
int sum(int x) {
int res = 0;
for (; x; x -= lowBit(x)) {
res += c[x];
}
return res;
}
void add(int x, int y) {
for (; x <= n; x += lowBit(x)) {
c[x] += y;
}
}
int main() {
while (scanf("%d", &n), n) {
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
Dis();
for (int i = n; i >= 1; --i) {
ans += sum(a[i]-1);
add(a[i], 1);
}
printf("%d\n", ans);
}
return 0;
}
为什么这个树状数组代码在vjudge上回T掉?求大佬指点!!!
换语言逝逝
换过了,也逝世了
你选的是哪种,换了之后还是TLE吗
逝的,两种c++都逝T