accoders 列传

· · 休闲·娱乐

声明:本文仅针对主站 <www.accoders.com>,与 accoders NOI 无关。

本质上是模拟赛红温记录……吗?

10.28

我不行了。

模拟赛 T1 放了一个比较典的题,需要求逆序对,于是我码了个树状数组(数据范围 3 \le n \le 5 \times 10^5,我时间复杂度 O(n\log n)),代码如下:

:::info[Code]

#include <bits/stdc++.h>
using namespace std;
#define int long long
int a[500005], b[500005], c[500005];
int tree[500005], n;
int lowbit(int x) { return x & (-x); }
void update(int x, int d) {
    while(x <= n) tree[x] += d, x += lowbit(x);
} int query(int x) { int sum = 0;
    while(x) sum += tree[x], x -= lowbit(x);
    return sum;
} signed main() {
    cin >> n; 
    for(int i = 1;i <= n;i++)
        cin >> a[i];
    for(int i = 1;i <= n;i++)
        if((a[i] % 2) != (i % 2))
            return cout << "-1\n", 0;
    int c1 = 0, c2 = 0;
    for(int i = 1;i <= n;i++)
        if(i % 2) b[++c1] = a[i];
        else c[++c2] = b[i];
    int ans = 0;
    for(int i = 1;i <= n;i++) tree[i] = 0;
    for(int i = c1;i >= 1;i--) 
        ans += query(b[i]), update(b[i], 1);
    for(int i = 1;i <= n;i++) tree[i] = 0;
    for(int i = c2;i >= 1;i--)
        ans += query(c[i]), update(c[i], 1);
    cout << ans << endl;
    return 0;
}

:::

然后:时间超限 55。没绷住……