QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#379881#8573. Slothful Secretaryucup-team1198#WA 710ms50396kbC++203.8kb2024-04-06 19:40:232024-04-06 19:40:23

Judging History

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

  • [2024-04-06 19:40:23]
  • 评测
  • 测评结果:WA
  • 用时:710ms
  • 内存:50396kb
  • [2024-04-06 19:40:23]
  • 提交

answer

#include <map>
#include <set>
#include <array>
#include <cmath>
#include <deque>
#include <bitset>
#include <random>
#include <string>
#include <vector>
#include <cassert>
#include <complex>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>

using namespace std;

const int MAXN = 5e5 + 100, INF = 1e9;
array<int, 2> tree[MAXN * 4];
int add[MAXN * 4];

array<int, 2> operator*(const array<int, 2>& a, const array<int, 2>& b) {
    array<int, 2> res = min(a, b);
    if (a[0] == b[0]) {
        res[1] = a[1] + b[1];
    }
    return res;
}

void pull(int v) {
    tree[v] = tree[v * 2] * tree[v * 2 + 1];
    tree[v][0] += add[v];
}

void build(int v, int vl, int vr) {
    if (vr - vl == 1) {
        tree[v] = {0, 1};
        return;
    }
    int vm = (vl + vr) / 2;
    build(v * 2, vl, vm);
    build(v * 2 + 1, vm, vr);
    pull(v);
}

void update(int v, int vl, int vr, int l, int r, int val) {
    if (l >= vr || r <= vl) return;
    if (l <= vl && r >= vr) {
        tree[v][0] += val;
        add[v] += val;
        return;
    }
    int vm = (vl + vr) / 2;
    update(v * 2, vl, vm, l, r, val);
    update(v * 2 + 1, vm, vr, l, r, val);
    pull(v);
}

array<int, 2> get(int v, int vl, int vr, int l, int r) {
    if (l >= vr || r <= vl) return {INF, 0};
    if (l <= vl && r >= vr) return tree[v];
    int vm = (vl + vr) / 2;
    auto res1 = get(v * 2, vl, vm, l, r);
    auto res2 = get(v * 2 + 1, vm, vr, l, r);
    auto res = res1 * res2;
    res[0] += add[v];
    return res;
}

unordered_map<int64_t, bool> mp;

void rev_edge(int u, int v) {
    if (u > v) swap(u, v);
    int64_t h = 1ll * u * MAXN + v;
    mp[h] ^= 1;
}

bool cmp(int u, int v) {
    bool inv = false;
    if (u > v) {
        swap(u, v);
        inv = true;
    }
    int64_t h = 1ll * u * MAXN + v;
    bool res = mp[h];
    if (inv) res = !res;
    return res;
}


int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, q;
    cin >> n >> q;
    vector<int> p(n);
    iota(p.begin(), p.end(), 0);
    vector<int> val(n);
    vector<int> id = p;

    build(1, 0, n - 1);

    while (q--) {
        int u, v;
        cin >> u >> v;
        --u; --v;
        rev_edge(u, v);
        int l = min(id[u], id[v]);
        int r = max(id[u], id[v]);
        /// cerr << l << " " << r << endl;
        if (!cmp(p[l], p[r])) {
            update(1, 0, n - 1, l, r, -1);
            val[p[l]]--;
            val[p[r]]++;
        } else {
            /// cerr << "add" << endl;
            /// cerr << l << " " << r << " " << 1 << endl;
            update(1, 0, n - 1, l, r, 1);
            val[p[l]]++;
            val[p[r]]--;
        }
        if (r == l + 1) {
            for (int i = r; i > 0 && cmp(p[i - 1], p[i]); --i) {
                /// cerr << i - 1 << ": " << val[p[i]] - val[p[i - 1]] + 1 << endl;
                update(1, 0, n - 1, i - 1, i, val[p[i]] - val[p[i - 1]] + 1);
                ++val[p[i]];
                --val[p[i - 1]];
                swap(p[i], p[i - 1]);
                swap(id[p[i]], id[p[i - 1]]);
            }   
            for (int i = r + 1; i < n && cmp(p[i - 1], p[i]); ++i) {
                /// cerr << i - 1 << ": " << val[p[i]] - val[p[i - 1]] + 1 << endl;
                update(1, 0, n - 1, i - 1, i, val[p[i]] - val[p[i - 1]] + 1);
                ++val[p[i]];
                --val[p[i - 1]];
                swap(p[i], p[i - 1]);
                swap(id[p[i]], id[p[i - 1]]);
            }
        }
        auto res = get(1, 0, n - 1, 0, n - 1);
        int cnt = res[1];
        if (res[0] != 0) {
            cnt = 0;
        }
        cout << cnt + 1 << "\n";
    }
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 710ms
memory: 50396kb

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:

wrong answer 71546th numbers differ - expected: '406786', found: '406785'