QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#250336#7627. Phonyucup-team1516WA 5ms3860kbC++177.4kb2023-11-13 04:07:102023-11-13 04:07:10

Judging History

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

  • [2023-11-13 04:07:10]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:3860kb
  • [2023-11-13 04:07:10]
  • 提交

answer

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

// 0-indexed
template<typename T>
struct BIT{
    int n;
    vector<T> bit,ary;
    BIT(int n = 0) : n(n),bit(n+1),ary(n) {}
    T operator[](int k) {
        return ary[k];
    }
    // [0, i)
    T sum(int i) {
        T res = 0;
        for (; i > 0; i -= (i&-i)) {
            res += bit[i];
        }
        return res;
    }
    // [l, r)
    T sum(int l, int r) {
        return sum(r) - sum(l);
    }
    void add(int i, T a) {
        ary[i] += a;
        i++;
        for (; i <= n; i += (i&-i)) {
            bit[i] += a;
        }
    }
    int lower_bound(T k) { // k <= sum(res)
        if (k <= 0) return 0;
        int res = 0, i = 1;
        while ((i << 1) <= n) i <<= 1;
        for (; i ; i >>= 1) {
            if (res+i <= n and bit[res+i] < k) {
                k -= bit[res += i];
            }
        }
        return res;
    }

    // The 2nd UC Stage 9: Qinhuangdao - I
    // 円環状で見たときに bit[i]+bit[i-1]+...+bit[j] を求める
    // i=j のときは 0 を返すようにしました
    T sum_cyc(int i, int j) {
        if (i == j) return 0;
        else if (j < i) return sum(j, i+1);
        else return sum(0, i+1) + sum(j, n);
    }
    // The 2nd UC Stage 9: Qinhuangdao - I
    // 円環状で見たときに bit[i]+bit[i-1]+...+bit[j] >= k となる最近の j と左辺の総和を求める
    // 雑にlog2つ
    pair<int, T> lower_bound_cyc(int j, T k) {
        T prefix = sum(j+1);
        if (prefix < k) {
            k -= prefix;
            int l = 0, r = n;
            while (r-l > 1) {
                int mid = (l+r)/2;
                T s = sum(mid, n);
                if (s >= k) {
                    l = mid;
                }
                else {
                    r = mid;
                }
            }
            return {l, prefix+sum(l,n)};
        }
        else {
            int l = 0, r = j+1;
            while (r-l > 1) {
                int mid = (l+r)/2;
                T s = sum(mid, j+1);
                if (s >= k) {
                    l = mid;
                }
                else {
                    r = mid;
                }
            }
            return {l, sum(l, j+1)};
        }
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,q; cin >> n >> q;
    ll K; cin >> K;
    vector<ll> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    sort(a.rbegin(), a.rend());
    vector<ll> z(n);
    for (int i = 0; i < n; ++i) {
        z[i] = a[i]%K;
    }
    sort(z.begin(), z.end());
    z.erase(unique(z.begin(), z.end()), z.end());
    int m = z.size();

    int sz = 0;
    BIT<int> bit(m);
    auto add = [&](int i) -> void {
        sz += 1;
        bit.add(i, 1);
    };

    vector<int> md(n);
    for (int i = 0; i < n; ++i) {
        md[i] = lower_bound(z.begin(), z.end(), a[i]%K) - z.begin();
        if (a[0]-a[i] < K) {
            add(md[i]);
        }
    }

    int pos = md[0];
    int cnt = 0;
    ll mx = a[0];
    __int128 nx = 0;

    auto bitsum = [&](int s, int t) -> int {
        if (t <= s) {
            return bit.sum(t, s+1);
        }
        else {
            return sz-bit.sum(s+1, t);
        }
    };

    auto calNext = [&]() -> void {
        if (sz >= n) {
            return;
        }
        else {
            ll dif = z[pos]-z[md[sz]];
            if (dif < 0) dif += K;
            nx = bitsum(pos, md[sz])-cnt;
            nx = bit.sum_cyc(pos, md[sz]) - cnt;

            ll uo = mx-dif;
            nx += (__int128)(uo-a[sz]-K)/K*(__int128)sz;
        }
    };

    calNext();

    auto debug = [&]() -> void {
//        cout << "debug" << endl;
//        for (int i = 0; i < query.size(); ++i) {
//            cout << query[i].first << " " << query[i].second << endl;
//        }
//        cout << "A" << endl;
//        for (int i = 0; i < 18; ++i) {
//            cout << a[i] << " ";
//        }
//        cout << endl;
        for (int i = 0; i < m; ++i) {
            cout << bit[i] << " ";
        }
        cout << endl;
        cout << "pos = " << pos << endl;
        cout << "cnt = " << cnt << endl;
        cout << "mx  = " << mx << endl;
        cout << "nx  = " << (ll)nx << endl;
    };

    while (q--) {
        char c; cin >> c;
        ll in; cin >> in;
        if (c == 'A') {
            int x = in;
            ll res;
            if (sz < x) {
                res = a[x-1];
            }
            else {
                // 最大値との差分を返すように
                auto findKth = [&](int i, int k, int cnt) -> ll {
                    if (bit[i]-cnt >= k) return 0;
                    if (sz-cnt < k) return K;

                    k += cnt;
                    auto p = bit.lower_bound_cyc(i, k);
                    ll dif = z[i]-z[p.first];
                    if (dif < 0) dif += K;
                    return dif;
                };
                ll dif = findKth(pos, x, cnt);
                res = mx-dif;
            }
            cout << res << "\n";
        }
        else {
            auto update = [&]() -> void {
                if (pos) {
                    mx -= z[pos]-z[pos-1];
                    pos -= 1;
                    cnt = 0;
                }
                else {
                    mx -= z[0]-z[m-1]+K;
                    pos = m-1;
                    cnt = 0;
                }
            };

            ll t = in;
            while (sz < n and t >= nx) {
                t -= nx;
                mx = a[sz]+K;
                pos = md[sz];
                ll cp = a[sz];
                while (sz < n and a[sz] == cp) {
                    add(md[sz]);
                }
                update();
                calNext();
            }

            ll u = t/sz;
            mx -= u*K;
            t -= u*sz;
            nx -= t;

            if (bit[pos]-cnt >= t) {
                cnt += t;
            }
            else {
                t -= (bit[pos]-cnt);
                update();
//                debug();

                if (bit.sum(pos+1) <= t) {
                    t -= bit.sum(pos+1);
                    mx -= z[pos]-z.back()+K;
                    pos = m-1;
                    cnt = 0;
                }

                // (r,pos]は全消し可能
                int l = -1, r = pos;
                while (r-l > 1) {
                    int mid = (l+r)/2;
                    if (bit.sum(mid+1,pos+1) <= t) {
                        r = mid;
                    }
                    else {
                        l = mid;
                    }
                }
                t -= bit.sum(r+1,pos+1);
                mx -= z[pos]-z[r];
                pos = r;
                cnt = 0;
                assert(bit[pos] >= t);
                cnt = t;
            }

//            debug();
        }
    }
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3700kb

input:

3 5 5
7 3 9
A 3
C 1
A 2
C 2
A 3

output:

3
4
-1

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 3636kb

input:

5 8 8
294 928 293 392 719
A 4
C 200
A 5
C 10
A 2
C 120
A 1
A 3

output:

294
200
191
0
-2

result:

ok 5 lines

Test #3:

score: 0
Accepted
time: 0ms
memory: 3584kb

input:

100 100 233
5101 8001 6561 6329 6305 7745 4321 811 49 1121 3953 8054 8415 9876 6701 4097 6817 6081 495 5521 2389 2042 4721 8119 7441 7840 8001 5756 5561 129 1 5981 4801 7201 8465 7251 6945 5201 5626 3361 5741 3650 7901 2513 8637 3841 5621 9377 101 3661 5105 4241 5137 7501 5561 3581 4901 561 8721 811...

output:

6881
9161
4721
8200
2945
7647
7531
5291
5001
2042
4721
4721
6881
4097
7187
7218
7035
7018
811
6752
2561
6683
6114
6135
3581
5291
1485
5957
5393
2042
5303
5171
5205
4721
5084
4029
4097
4591
4816
4586
4705
2042
4535
4454
4603
4435
3581
4345
115
2042
4284
2042
4274
1485
4326
-17552

result:

ok 56 lines

Test #4:

score: 0
Accepted
time: 0ms
memory: 3860kb

input:

100 300 3453213
4777243 17293302 43453992 45342348 82151965 11360220 63630258 38237802 1 56066162 75020027 83365329 100659838 2141897 73421986 102600372 100824166 46869742 31931613 26168082 41399743 62249685 31666167 96044265 81576202 44039394 94271661 37319513 46613514 14176026 23991180 3310773 635...

output:

16238806
100824166
74028301
100824166
98702945
100659838
3066866
82151965
78167694
86340309
16238806
64926204
73585978
16238806
64926204
4777243
62249685
78951605
16238806
3066866
77853267
78427881
75980670
43453992
16238806
1
78122989
62404688
72949759
3310773
35761909
44960459
73552383
72045179
16...

result:

ok 159 lines

Test #5:

score: -100
Wrong Answer
time: 5ms
memory: 3768kb

input:

10000 10000 424242
17989609 33305173 36700219 9019831 18836819 21791961 21965035 18824893 1921235 23926509 28110961 33993409 12966853 13082665 4452379 1637119 38371575 1826245 36931693 1 33924345 30310225 30503101 4578015 31263907 15733393 40333897 34523425 25601465 11528899 18236695 4978289 9301153...

output:

29899255
19256287
20728753
27691558
11569357
18782611
6940738
25519453
36411607
15396403
20282701
5151091
13921909
21284225
20657428
13921909
19561543
11116483
30447757
38758135
7394929
27457873
34310557
1419251
40938731
25617811
25928137
21610443
40849627
18524773
24971899
24506287
4463929
27691558...

result:

wrong answer 4977th lines differ - expected: '-20800647', found: '-20800865'