跳到正文

休闲·娱乐

accoders 列传

Eason_cyx约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年11月21日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1

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

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

10.28

我不行了。

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

Code
C++
#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;
}

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

原文
存档评论1 条抓取于