QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#177504#6872. Magma CavePPPRE 0ms0kbC++175.6kb2023-09-13 00:43:382023-09-13 00:43:38

Judging History

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

  • [2023-09-13 00:43:38]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2023-09-13 00:43:38]
  • 提交

answer

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
int n, q;
const int maxN = 400'005;
int tp[maxN], u[maxN], v[maxN], w[maxN], k[maxN];
int ST = 0;
int p[maxN];
vector<tuple<int, int, int>> roll_back;
int sz[maxN];

int get(int u) {
    while (u != p[u]) u = p[u];
    return u;
}

bool unite(int u, int v) {
    u = get(u);
    v = get(v);
    if (u == v) return false;
    if (sz[u] < sz[v]) swap(u, v);
    roll_back.emplace_back(u, p[u], sz[u]);
    roll_back.emplace_back(v, p[v], sz[v]);
    sz[u] += sz[v];
    p[v] = u;
    return true;
}

void do_roll(int SZ) {
    while ((int) roll_back.size() > SZ) {
        p[get<0>(roll_back.back())] = get<1>(roll_back.back());
        sz[get<0>(roll_back.back())] = get<2>(roll_back.back());
        roll_back.pop_back();
    }
}

int when[2][maxN];
bool ans[maxN];
void go(int tl, int tr, vector<int> &inter) {
    int tm = (tl + tr) / 2;
    int SZ = roll_back.size();
//    cout << " ??? " << tl << " " << tr << endl;
    for (int T: inter) {
//        cout << " ASK " << T << endl;
        if (unite(u[T], v[T])) {
        } else {
            when[ST][T] = min(when[ST][T], tr - 1);
        }
    }
    do_roll(SZ);
    if (tl == tr) {
        return;
    }
    vector<int> inter_1, inter_2;
    //tl .. tm ||
    //added
    SZ = roll_back.size();
    for (int T : inter) {
        if ((T >= tl && T <= tm) || when[ST][T] < tr) {
            inter_1.emplace_back(T);
        }
        else if (T < tl && when[ST][T] == tr) {
            unite(u[T], v[T]);
        }
    }
    //1 .. 4
    go(tl, tm, inter_1);
    do_roll(SZ);
    SZ = roll_back.size();
    for (int T : inter) {
        if (T <= tm && when[ST][T] == tr) {
            unite(u[T], v[T]);
        }
        else if (T > tm || when[ST][T] >= tm + 1){
            inter_2.emplace_back(T);
        }
    }
    go(tm + 1, tr, inter_2);
    do_roll(SZ);
}

int bck[maxN];
int fenw[maxN];
void upd(int v, int by) {
    while (v <= q) {
        fenw[v] += by;
        v = (v | (v - 1)) + 1;
    }
}
int get_fenw(int r) {
    int ans = 0;
    while (r > 0) {
        ans += fenw[r];
        r &= (r - 1);
    }
    return ans;
}
int del[maxN];
int id_q[maxN];
mt19937 rnd(228);
void solve() {
    cin >> n >> q;
//    n = rnd() % 10 + 1;
//    q = rnd() % 10 + 1;
//    cout << n << " " << q << endl;
    for (int i = 1; i <= q; i++) {
        cin >> tp[i];
//        tp[i] = 1;
        if (tp[i] == 1) {
//            while (u[i] == v[i]) {
//                u[i] = rnd() % n + 1;
//                v[i] = rnd() % n + 1;
//            }
//            w[i] = i;
//            cout << u[i] << " " << v[i] << " " << w[i] << endl;
            cin >> u[i] >> v[i] >> w[i];
            bck[w[i]] = i;
        } else {
            cin >> k[i] >> w[i];
            id_q[i] = bck[w[i]];
            ans[i] = true;
        }
    }
    for (int z = 0; z < 2; z++) {
        for (int i = 1; i <= n; i++) {
            p[i] = i;
            sz[i] = 1;
        }
        roll_back.clear();

        ST = z;
        vector<int> inter;
        for (int i = 1; i <= q; i++) {
            if (tp[i] == 1) {
                inter.emplace_back(i);
                when[ST][i] = q + 1;
            }
        }
        sort(inter.begin(), inter.end(), [&](int x, int y) {
            return w[x] < w[y];
        });
        go(1, q, inter);

        for (int j = 1; j <= q; j++) {
            fenw[j] = 0;
            del[j] = -1;
        }

        for (int i = 1; i <= q; i++) {
            if (tp[i] == 1) {
//                cout << i << " " << when[ST][i] << endl;
                if (when[ST][i] <= q && when[ST][i] >= i) {
                    assert(del[when[ST][i]] == -1);
                    del[when[ST][i]] = i;
                }
            }
        }

        for (int i = 1; i <= q; i++) {
            if (tp[i] == 1) {
                if (when[ST][i] >= i) {
//                    cout << " UPD " << w[i] << endl;
                    upd(w[i], +1);
                }
                if (del[i] != -1) {
//                    cout << " UPD2 " << w[del[i]] << endl;
                    upd(w[del[i]], -1);
                }
            }
            else {
                int his_id = id_q[i];
                if (ST == 0) {
                    if (get_fenw(q) != (n - 1)) ans[i] = false;
                    if (his_id > i) ans[i] = false;
                    if (k[i] > get_fenw(w[his_id])) {
                        ans[i] = false;
                    }
                }
                else {
                    if (n - k[i] > get_fenw(w[his_id])) {
                        ans[i] = false;
                    }
                }
            }
        }

        for (int i = 1; i <= q; i++) {
            if (tp[i] == 1) {
                w[i] = q + 1 - w[i];
            }
        }
    }

    for (int i = 1; i <= q; i++) {
        if (tp[i] == 2) {
            if (ans[i]) cout << "YES\n";
            else cout << "NO\n";
        }
    }

}


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef DEBUG
    freopen("input.txt", "r", stdin);
#endif
//    dp[0] = 1;
//    for (int i = 2; i <= 50; i++) {
//        dp[i] += dp[i - 2];
//        if (i >= 3) dp[i] += dp[i - 3];
//    }
//    cout << dp[50] << '\n';
//    exit(0);
    int tst = 1000;
    cin >> tst;
    while (tst--) {
        solve();
    }
    return 0;
}

详细

Test #1:

score: 0
Runtime Error

input:

100
500 1999
1 112 419 318
1 419 208 1700
1 208 255 603
1 255 202 306
1 202 54 326
1 54 468 1506
1 468 23 856
1 23 90 758
1 90 271 1104
1 271 442 1900
1 442 441 19
1 441 378 1227
1 378 466 24
1 466 186 228
1 186 399 1387
1 399 288 297
1 288 144 1763
1 144 418 1896
1 418 217 673
1 217 281 1259
1 281 ...

output:


result: