@[cn:苏卿念](/space/show?uid=57699)
这题不用这样做啊...
lower_bound和upper_bound不好吗
在algorithm里
by decoqwq @ 2018-02-19 00:02:47
@[cn:苏卿念](/space/show?uid=57699)
参考一下 http://blog.csdn.net/u012198382/article/details/24887181
by decoqwq @ 2018-02-19 00:05:16
@[刘浩宇(寂)](/space/show?uid=48265)
人家这是bit写的
by iodwad @ 2018-02-19 00:47:21
```cpp
for(int i=1;i<=n;i++) add(i,1);
ans+=answer(t[i]-1);
```
蒟蒻看不懂您的写法啊。。。add(i,1)看起来很玄学啊,贴一下我自己~~照着网上写的~~的,您参考下
```cpp
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
#define lowbit(a) (a&(-a))
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,bufsize,stdin),p1==p2)?EOF:*p1++)
const int bufsize = 500000;
using namespace std;
char buf[bufsize], *p1=buf, *p2=buf;
inline int read(){
register int c = getchar(), x = 0;
while(!isdigit(c)) c = getchar();
while(isdigit(c)) x = (x<<3)+(x<<1)+(x^48), c = getchar();
return x;
}
int n;
struct Node{
int ord, val;
inline bool operator < (const Node &rhs) const {
return val < rhs.val;
}
}p[500010];
int a[500010], l[500010];
inline void add(int x, int k){
while(x <= n){
l[x] += k;
x += lowbit(x);
}
}
inline int Sum(int x){
int ans = 0;
while(x > 0){
ans += l[x];
x -= lowbit(x);
}
return ans;
}
int main(void){
//freopen("data.in", "r", stdin);
scanf("%d", &n);
long long ans = 0;
for(register int i = 1; i <= n; ++i)scanf("%d", &p[i].val), p[i].ord=i;// p[i].val=read(), p[i].ord=i;//
sort(p+1,p+1+n);
for(register int i = 1; i <= n; ++i) a[p[i].ord]=i;
for(register int i = 1; i <= n; ++i) add(a[i], 1), ans += i - Sum(a[i]);
cout << ans << '\n';
return 0;
}
```
by qqvq @ 2018-02-19 08:44:44
@[刘浩宇(寂)](/space/show?uid=48265) 树状数组求逆序对呀。。。。。
by Randyhoads @ 2018-02-19 08:59:29
然而本蒟蒻看不懂这是什么
by Macrohard @ 2018-02-19 09:37:47
@[WLZS](/space/show?uid=37408) ...
by decoqwq @ 2018-02-19 09:55:31
@[ZCDHJ](/space/show?uid=24878) dalao换头像了啊
by Niko @ 2018-02-19 10:21:59
```
for(;x;x+=lowbit(x))
```
你确定不会死循环??
终止条件应该是**$x<=n$**吧?
@[cn:苏卿念](/space/show?uid=57699)
by YoungNeal @ 2018-02-19 10:45:29
您这种题都能错?树状数组求逆序对,要离散化的QAQ,还有似乎除了lowbit没一个写对的QAQ
by shadowice1984 @ 2018-02-19 10:45:47