Daniel_yao @ 2022-09-30 08:34:56
RT
#include <bits/stdc++.h>
#define int long long
#define H 19260817
#define rint register int
#define For(i,l,r) for(rint i=l;i<=r;++i)
#define FOR(i,r,l) for(rint i=r;i>=l;--i)
#define MOD 1000003
#define mod 1000000007
using namespace std;
inline int read() {
rint x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
return x*f;
}
void print(int x){
if(x<0){putchar('-');x=-x;}
if(x>9){print(x/10);putchar(x%10+'0');}
else putchar(x+'0');
return;
}
const int N = 500010 * log(1e9);
struct Node {
int ls, rs;
int l, r;
int num;
}t[N << 2];
int n = read(), a[N], ans, mx, idx = 1;
void pushup(int p) {
t[p].num = t[t[p].ls].num + t[t[p].rs].num;
}
void add(int p, int l, int r, int k) {
t[p].l = l, t[p].r = r;
if(l == r) {
t[p].num ++;
return ;
}
int mid = l + r >> 1;
if(k <= mid) {
if(!t[p].ls) t[p].ls = ++idx;
add(t[p].ls, l, mid, k);
} else {
if(!t[p].rs) t[p].rs = ++idx;
add(t[p].rs, mid + 1, r, k);
}
pushup(p);
}
int query(int p, int l, int r) {
if(p == 0) return 0;
if(l <= t[p].l && t[p].r <= r) {
return t[p].num;
}
int mid = t[p].l + t[p].r >> 1, ans = 0;
if(l <= mid) {
ans += query(t[p].ls, l, r);
}
if(r > mid) {
ans += query(t[p].rs, l, r);
}
return ans;
}
signed main() {
For(i,1,n) a[i] = read(), mx = max(mx, a[i]);
For(i,1,n) {
add(1, 1, n, a[i]);
ans += query(1, a[i] + 1, mx);
}
cout << ans << '\n';
return 0;
}
by bamboo12345 @ 2022-09-30 08:50:15
@yzy001633 给一组hack
5
10 9 8 7 6
by Daniel_yao @ 2022-09-30 08:53:15
@bamboo123 eee....被hack了.....
by Daniel_yao @ 2022-09-30 08:54:22
@bamboo123 那究竟是为啥呢?
by bamboo12345 @ 2022-09-30 08:54:47
@yzy001633 这个hack倒挺神奇的,改成
5
5 4 3 2 1
又是可以的
by Daniel_yao @ 2022-09-30 08:56:09
@bamboo123 确实可以,???? 让我思考一下
by bamboo12345 @ 2022-09-30 08:57:00
@yzy001633 好像你的add没有改成功?
by bamboo12345 @ 2022-09-30 08:57:42
@yzy001633 哈哈哈你的add调用不应该是1到mx吗?你写了个1到n
by Daniel_yao @ 2022-09-30 08:59:06
@bamboo123 哈(恍然大雾
by Daniel_yao @ 2022-09-30 09:00:14
@bamboo123 我写普通线段树写习惯了,都忘记这是权值线段树了。(无语。。。
by Daniel_yao @ 2022-09-30 09:03:17
@bamboo123 问问dalao:
改完后50分了,后面50分MLE好像是数组空间的,所以数组要开多大,权值线段树的数组空间大小要怎样开才保证不会错?