QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#55606#1823. Permutation CFGckiseki#WA 554ms104416kbC++6.4kb2022-10-14 19:53:562022-10-14 19:53:59

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-14 19:53:59]
  • 评测
  • 测评结果:WA
  • 用时:554ms
  • 内存:104416kb
  • [2022-10-14 19:53:56]
  • 提交

answer

#include <bits/stdc++.h>
#ifdef CKISEKI
#define safe cerr<<__PRETTY_FUNCTION__<<" line " <<__LINE__<<" safe\n"
#define debug(a...) qwerty(#a, a)
#define orange(a...) dvorak(#a, a)
using std::cerr;
template <typename ...T>
void qwerty(const char *s, T ...a) {
    cerr << "\e[1;32m(" << s << ") = (";
    int cnt = sizeof...(T);
    (..., (cerr << a << (--cnt ? ", " : ")\e[0m\n")));
}
template <typename Iter>
void dvorak(const char *s, Iter L, Iter R) {
    cerr << "\e[1;32m[ " << s << " ] = [ ";
    for (int f = 0; L != R; ++L)
        cerr << (f++ ? ", " : "") << *L;
    cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) ((void)0)
#define orange(...) ((void)0)
#endif

using namespace std;

const int maxn = 100025;

using Big = __int128;

struct Fenwick {
    int cnt[maxn];
    Big sum[maxn];
    void add(int p, int u, Big v) {
        for (++p; p < maxn; p += p & -p) {
            cnt[p] += u;
            sum[p] += v;
        }
    }
    pair<int,Big> query(int p) {
        int c = 0;
        Big s = 0;
        for (++p; p > 0; p -= p & -p)
            c += cnt[p], s += sum[p];
        return { c, s };
    }
    int bsearchSum(Big &k) {
        Big orgk = k;
        int p = 0;
        for (int s = 1 << 20; s; s >>= 1) {
            if (p + s < maxn && sum[p + s] < k) {
                p += s;
                k -= sum[p];
            }
        }
        if (query(p).second == orgk) {
            ++p;
            k = 0;
        }
        return p;
    }
    void init() {
        memset(cnt, 0, sizeof(cnt));
        memset(sum, 0, sizeof(sum));
    }
} BIT;

struct ar : array<Big,6> {
    ar & operator+=(const ar &rhs) {
        for (int j = 0; j < 6; j++)
            (*this)[j] += rhs[j];
        return *this;
    }
};


ar operator+(ar lhs, const ar &rhs) {
    for (int j = 0; j < 6; j++)
        lhs[j] += rhs[j];
    return lhs;
}

struct Segtree {
    ar st[maxn * 2];
    int n;
    void init(int _n) {
        n = _n;
        for (int i = 0; i < n * 2; i++)
            st[i] = ar{};
    }
    void add(int p, ar val) {
        for (st[p += n] += val; p > 1; p >>= 1)
            st[p >> 1] = st[p] + st[p ^ 1];
    }
    ar query(int l, int r) {
        ar res = {};
        for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if (l & 1) res += st[l++];
            if (r & 1) res += st[--r];
        }
        return res;
    }
} sgt;

// int C(int n, int k) {
//     int64_t p = 1;
//     for (int i = 0; i < k; i++)
//         p *= n - i;
//     for (int i = 1; i <= k; i++)
//         p /= i;
//     return p;
// }

ostream & operator<<(ostream &o, Big b) {
    static auto tos = [](Big x) -> string {
        assert (x >= 0);
        if (!x) return "0";
        string s;
        while (x)
            s += char(x % 10 + '0'), x /= 10;
        reverse(s.begin(), s.end());
        return s;
    };
    return o << tos(b);
};

int main() {
    // freopen("g.in", "r", stdin);
    // cerr << "reopen g.in" << endl;
    cin.tie(nullptr)->sync_with_stdio(false);
    int n, s, q;
    cin >> n >> s >> q;

    vector<int> p(n+1), pos(n+1);
    vector<array<Big,6>> dp(n+1);

    for (int i = 1; i <= n; i++)
        dp[i][0] = 1;

    for (int j = 1; j <= s; j++) {
        dp[0][j] = 0;
        for (int i = 1; i <= n; i++) {
            dp[i][j] = dp[i-1][j] + dp[i][j-1];
        }
    }

    // for (int j = 0; j <= s; j++) {
    //     for (int i = 0; i <= n; i++) {
    //         cout << dp[i][j] << ' ';
    //     }
    //     cout << endl;
    // }

    // for (int i = 1; i <= n; i++) p[i] = i;
    for (int i = 1; i <= n; i++) cin >> p[i];
    for (int i = 1; i <= n; i++)
        pos[p[i]] = i;

    vector<int> ql(q);
    vector<vector<pair<Big,int>>> Q1(n+1), nQ1;
    for (int i = 0; i < q; i++) {
        int a;
        cin >> ql[i] >> a;
        Q1[n].emplace_back(a, i);
    }

    vector<vector<tuple<int,int,int>>> qs[7];
    for (int j = 0; j <= s; j++)
        qs[j].resize(n + 2);

    for (int j = s - 1; j >= 0; j--) {
        nQ1.assign(n+1, {});
        BIT.init();
        debug(j);
        for (int i = 1; i <= n; i++) {
            BIT.add(pos[i], 1, dp[i][j]);
            // for (int k = 1; k <= n; k++)
            //     cerr << BIT.query(k).second-BIT.query(k-1).second << ' ';
            // cerr << endl;

            for (auto [a, qid]: Q1[i]) {
                debug(a, qid, BIT.query(n).second);
                int pp = BIT.bsearchSum(a); // `a` changes
                // assert (ql[qid] <= i); // FIXME
                qs[j][pp].emplace_back(ql[qid], i, qid);
                if (a == 0) continue;
                int nxt = p[pp];
                nQ1[nxt].emplace_back(a, qid);
                debug(a, qid, pp, nxt);
            }
        }
        Q1 = nQ1;
    }

    vector<Big> ans(q);

    const auto calc = [](ar powsum, int a, int b) {
        // C(x - a + b, b)
        vector<Big> poly{1};
        for (int j = 0; j < b; j++) {
            // poly *= (x - a + b - j)
            vector<Big> newpoly(poly.size() + 1);
            for (int k = 0; k < poly.size(); k++) {
                newpoly[k + 1] += poly[k];
                newpoly[k] += (-a + b - j) * poly[k];
            }
            poly = newpoly;
        }

        Big res = 0;
        for (int j = 0; j < poly.size(); j++)
            res += poly[j] * powsum[j];
        return res;
    };

    for (int j = s; j >= 0; j--) {
        vector<int> freq(n + 1);
        sgt.init(n + 1);
        for (int i = 1; i <= n + 1; i++) {
            for (auto [l, r, qid]: qs[j][i]) {
                /*
                // debug(l, r, i, j);
                for (int x = 1; x < i; x++) {
                // cerr << "p[x] = " << p[x] << endl;
                if (l <= p[x] && p[x] <= r) {
                if (j == 0)
                ans[qid] += p[x] == l;
                else
                ans[qid] += C(p[x] - l + j - 1, j - 1);
                }
                }
                // debug(ans[qid]);
                 */

                if (j == 0) {
                    ans[qid] += freq[l];
                } else {
                    auto powsum = sgt.query(l, r + 1);
                    ans[qid] += calc(powsum, l, j - 1);
                }
            }
            if (i <= n) {
                freq[p[i]] += 1;
                ar tmp;
                tmp[0] = 1;
                for (int k = 1; k <= s; k++)
                    tmp[k] = tmp[k - 1] * p[i];
                sgt.add(p[i], tmp);
            }
        }
    }

    for (int i = 0; i < q; i++)
        cout << ans[i] << '\n';

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 5672kb

input:

4 3 6
1
4
3
2
1 6
2 20
4 1
3 5
2 9
1 16

output:

3
6
0
1
2
8

result:

ok 6 numbers

Test #2:

score: 0
Accepted
time: 554ms
memory: 104292kb

input:

100000 5 200000
98129
52962
60307
83804
75634
55811
85666
40965
78529
40850
54600
83058
37001
92436
30323
54632
24238
77437
87162
75991
39863
74990
32168
99841
85430
66056
69893
7561
60704
40795
81535
89902
44331
20995
99461
93620
91817
97370
84025
98836
2455
77447
37078
90192
26249
84337
70311
4006...

output:

20217
9885
20204
20217
20218
20217
20217
727
20202
20218
20218
20214
20217
7790
20217
20217
20200
20217
20218
20217
20218
20218
20218
20217
20202
20217
20218
20218
5609
8419
20218
20200
20218
20216
11843
20218
20217
20207
20217
935
20203
5909
20218
20217
20218
20200
20217
20213
20203
20207
5654
4087...

result:

ok 200000 numbers

Test #3:

score: 0
Accepted
time: 525ms
memory: 104416kb

input:

100000 5 200000
59767
75967
97372
50276
20532
77164
30969
40418
84175
87555
79967
28404
26422
22441
4543
72719
9318
93986
12744
88814
25854
93658
9042
41389
24133
60155
80340
44920
58271
50431
92622
28573
30633
318
43850
78809
69750
67699
17767
8454
2543
26572
52552
77502
24901
94119
87156
19368
394...

output:

33305
33330
19455
33332
25500
1777
33332
33310
33337
33333
33297
33331
33337
33330
33317
33304
33332
33336
33331
33337
33335
5611
33334
33337
33333
33331
33337
33307
33334
22446
31995
27049
33337
5351
32269
33332
33334
33331
33299
33331
33334
24072
21451
33331
33338
33332
33331
33331
33319
23356
333...

result:

ok 200000 numbers

Test #4:

score: 0
Accepted
time: 514ms
memory: 103380kb

input:

97997 5 200000
92883
53079
12146
7142
47500
47118
60176
54625
8908
93576
33824
61522
79201
68956
25790
76970
63243
10575
33345
20943
77076
40068
30980
20739
90036
57454
38446
49088
90613
27293
39453
52577
94545
7934
73793
6201
33713
91255
45678
63783
65019
35224
65407
39863
38120
79943
69106
76540
6...

output:

21513
21504
21512
21513
21512
19613
21418
21511
21512
6191
21512
18462
4097
21511
21511
21510
21499
21502
21513
21512
21499
21512
21512
21513
21512
21512
21501
21512
10071
21513
21510
21511
21513
21512
21511
21498
21512
21511
21497
21513
21512
21512
21513
21512
21491
21513
16589
21508
21511
21500
20...

result:

ok 200000 numbers

Test #5:

score: -100
Wrong Answer
time: 425ms
memory: 98852kb

input:

97997 5 200000
1
2
3
4
5
6
7
8
9
10
97997
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
...

output:

36269
44641
44708
44828
44700
48063
39594
44828
44405
45604
43915
44825
44706
28640
44655
44062
48063
22483
18051
44678
44952
44686
44695
44645
45215
44752
44700
35460
37180
43835
18491
40508
37270
46172
44752
44665
41482
44973
44633
44743
41636
46973
43697
43261
40377
41790
49504
43725
44184
38340
...

result:

wrong answer 4th numbers differ - expected: '44743', found: '44828'