QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#133535#4936. Shopping ChangesS_Explosion#TL 4ms13924kbC++142.1kb2023-08-02 10:48:132023-08-02 10:48:16

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-02 10:48:16]
  • 评测
  • 测评结果:TL
  • 用时:4ms
  • 内存:13924kb
  • [2023-08-02 10:48:13]
  • 提交

answer

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

template <typename Tp>
inline void read(Tp &x) {
    x = 0;
    bool f = true; char ch = getchar();
    for ( ; ch < '0' || ch > '9'; ch = getchar()) f ^= ch == '-';
    for ( ; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + (ch ^ 48);
    x = f ? x : -x;
}

const int N = 3e5;

int a[N + 5], b[N + 5];
std::vector<int> Vec[N + 5];

struct BIT {
    int c[N + 5];

    void add(int x, int k) {
        for ( ; x <= N; x += x & -x) c[x] += k;
    }
    int query(int x) {
        int res = 0;
        for ( ; x > 0; x -= x & -x) res += c[x];
        return res;
    }
};
BIT B1, B2;

int main() {
    int n, q;
    read(n), read(q);
    for (int i = 1; i <= n; ++i) {
        read(a[i]);
        b[i] = a[i];
    }
    int m = n;
    for (int i = 1; i <= q; ++i) {
        int k;
        read(k);
        Vec[i].resize(k);
        for (int j = 0; j < k; ++j) {
            read(Vec[i][j]);
            b[++m] = Vec[i][j];
        }
    }
    std::sort(b + 1, b + m + 1);
    m = std::unique(b + 1, b + m + 1) - (b + 1);
    for (int i = 1; i <= n; ++i) {
        a[i] = std::lower_bound(b + 1, b + m + 1, a[i]) - b;
        B1.add(a[i], 1);
    }
    for (int t = 1; t <= q; ++t) {
        long long cur = 0;
        for (int i = 1; i <= n; ++i) {
            cur += (i - 1) - B2.query(a[i]);
            B2.add(a[i], 1);
        }
        for (int i = 0; i < (int)Vec[t].size(); ++i) {
            Vec[t][i] = std::lower_bound(b + 1, b + m + 1, Vec[t][i]) - b;
            cur += (i + n) - B2.query(Vec[t][i]);
            B2.add(Vec[t][i], 1);
        }
        long long ans = cur;
        for (int i = 0; i < (int)Vec[t].size(); ++i) {
            cur += B1.query(Vec[t][i] - 1) - (n - B1.query(Vec[t][i]));
            ans = std::min(ans, cur);
        }
        printf("%lld\n", ans);
        for (int i = 1; i <= n; ++i) B2.add(a[i], -1);
        for (int i = 0; i < (int)Vec[t].size(); ++i) B2.add(Vec[t][i], -1);
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 11872kb

input:

3 3
5 6 7
6 2 3 4 8 9 10
2 100 99
3 5 6 7

output:

0
1
1

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 4ms
memory: 13924kb

input:

3 2
7 6 5
6 2 3 4 8 9 10
6 10 9 8 4 3 2

output:

3
27

result:

ok 2 lines

Test #3:

score: 0
Accepted
time: 4ms
memory: 13904kb

input:

1 1
589284012
1 767928734

output:

0

result:

ok single line: '0'

Test #4:

score: -100
Time Limit Exceeded

input:

10000 9999
298772847 712804102 869795012 527998188 804246450 598105371 843966934 639805471 937482040 887551242 254734680 188704975 17408126 626523673 553956319 697723205 231690822 637079761 232393146 377026277 962223856 338922458 912529500 710873344 942955137 51167037 195729799 529674367 990599310 4...

output:

24802338
24830913
24857654
24813132
24846150
24785558
24857315
24805175
24862714
24785339
24804999
24780907
24808009
24798987
24958122
24781372
24772291
24846071
24953540
24778276
24778689
24979527
24770012
24892097
24776909
24865295
24821506
24772586
24800432
24891424
24864488
24820312
24801522
248...

result: