QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#379295#8573. Slothful Secretaryucup-team1516#WA 831ms35844kbC++174.5kb2024-04-06 16:57:552024-04-06 16:57:57

Judging History

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

  • [2024-04-06 16:57:57]
  • 评测
  • 测评结果:WA
  • 用时:831ms
  • 内存:35844kb
  • [2024-04-06 16:57:55]
  • 提交

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; }

// 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] を求める
    T sum_cyc(int i, int j) {
        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() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, q;
    cin >> n >> q;
    BIT<int> bit(n + 5);
    BIT<ll> seg(n + 5);
    for (int i = 0; i < n; ++i) {
        bit.add(i, 1);
        seg.add(i, i);
    }
    vector<int> deg(n);
    for (int i = 0; i < n; ++i) {
        deg[i] = n - 1 - i;
    }
    int res = n;
    set<pair<int, int>> st;
    while (q--) {
        int x, y;
        cin >> x >> y;
        x -= 1, y -= 1;
        if (st.find({x, y}) == st.end()) {
            st.insert({x, y});
        } else {
            st.erase(st.find({x, y}));
            swap(x, y);
        }
        // deg[x] -= 1, deg[y] += 1
        vector<int> v;
        for (int i = -2; i <= 2; ++i) {
            if (deg[x] + i < 0) continue;
            v.push_back(deg[x] + i);
        }
        for (int i = -2; i <= 2; ++i) {
            if (deg[y] + i < 0) continue;
            v.push_back(deg[y] + i);
        }
        sort(v.begin(), v.end());
        v.erase(unique(v.begin(), v.end()), v.end());
        auto check = [&](int i) -> bool {
            if (bit[i] == 0) return false;
            ll low_cnt = bit.sum(i);
            ll high_cnt = n - low_cnt;
            ll sum = (ll)(n - 1) * (ll)n / 2;
            ll low_sum = seg.sum(i);
            ll high_sum = sum - low_sum;
            //            cout << low_cnt << " " << high_cnt << " " << sum << " " << low_sum << " "
            //            << high_cnt
            //                 << endl;
            high_sum -= high_cnt * (high_cnt - 1) / 2;
            //            cout << " " << high_sum << " " << low_cnt * high_cnt << endl;
            return high_sum >= low_cnt * high_cnt;
        };
        for (int j : v)
            if (check(j)) res -= 1;
        bit.add(deg[x], -1);
        seg.add(deg[x], -deg[x]);
        bit.add(deg[y], -1);
        seg.add(deg[y], -deg[y]);
        deg[x] -= 1, deg[y] += 1;
        bit.add(deg[x], 1);
        seg.add(deg[x], deg[x]);
        bit.add(deg[y], 1);
        seg.add(deg[y], deg[y]);
        for (int j : v)
            if (check(j)) res += 1;
        cout << res << "\n";
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 789ms
memory: 31948kb

input:

500000 500000
111236 111238
400537 400538
14798 14799
480138 480140
99901 99904
169041 169045
20969 20970
111098 111100
245492 245493
25296 25300
21736 21739
415491 415495
222594 222595
162236 162238
362422 362425
422694 422695
339973 339974
241678 241680
192401 192403
125627 125629
261473 261474
10...

output:

499998
499998
499998
499996
499993
499989
499989
499987
499987
499983
499980
499976
499976
499974
499971
499971
499971
499969
499967
499965
499965
499961
499961
499961
499961
499961
499961
499958
499955
499952
499949
499947
499947
499945
499942
499940
499938
499936
499934
499930
499926
499924
499920...

result:

ok 500000 numbers

Test #2:

score: -100
Wrong Answer
time: 831ms
memory: 35844kb

input:

500000 500000
275072 275075
426847 426850
409242 409246
146242 146245
383677 383680
306056 306058
399602 399608
166932 166934
484108 484109
164331 164340
223174 223175
36591 36593
312357 312358
396602 396605
196545 196550
332 338
394336 394339
493543 493550
112676 112679
473708 473709
173498 173499
...

output:

499997
499994
499990
499987
499984
499982
499977
499975
499975
499970
499970
499968
499968
499965
499960
499955
499952
499947
499944
499944
499944
499939
499934
499930
499926
499922
499919
499917
499917
499912
499907
499907
499904
499900
499897
499892
499887
499883
499880
499878
499878
499873
499871...

result:

wrong answer 7th numbers differ - expected: '499976', found: '499977'