QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#177529#6872. Magma CavePPPAC ✓1780ms26536kbC++175.5kb2023-09-13 01:40:432023-09-13 01:40:43

Judging History

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

  • [2023-09-13 01:40:43]
  • 评测
  • 测评结果:AC
  • 用时:1780ms
  • 内存:26536kb
  • [2023-09-13 01:40:43]
  • 提交

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();
    for (int T: inter) {
        assert(T <= tr);
        if (unite(u[T], v[T])) {
        } else {
            when[ST][T] = min(when[ST][T], tr - 1);
        }
    }
    do_roll(SZ);
    //дожили до конца
    //< tl vse dobavlyaem
    if (tl == tr) {
        return;
    }
    vector<int> inter_1, inter_2;
    SZ = roll_back.size();
    for (int T : inter) {
        if (T <= tm && (T >= tl || when[ST][T] < tr)) {
            inter_1.emplace_back(T);
        }
        else if (T < tl && when[ST][T] >= tr) {
            unite(u[T], v[T]);
        }
    }
    go(tl, tm, inter_1);
    do_roll(SZ);
    SZ = roll_back.size();
    //tl .. tm
    //tm - 1
    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++) bck[i] = 0;
    for (int i = 1; i <= q; i++) {
        cin >> tp[i];
        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 + 1; j++) {
            fenw[j] = 0;
            del[j] = -1;
        }

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

        for (int i = 1; i <= q; i++) {
            if (tp[i] == 1) {
                if (when[ST][i] >= i) {
                    upd(w[i], +1);
                }
                if (del[i] != -1) {
                    upd(w[del[i]], -1);
                }
            }
            if (tp[i] == 2) {
                int his_id = id_q[i];
                if (his_id == 0) {
                    ans[i] = false;
                    continue;
                }
                if (ST == 0) {
                    if (get_fenw(q) != (n - 1)) 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: 100
Accepted
time: 1780ms
memory: 26536kb

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:

NO
NO
NO
NO
YES
NO
NO
YES
NO
NO
NO
NO
NO
YES
NO
YES
NO
YES
NO
NO
YES
YES
YES
YES
YES
YES
NO
YES
NO
YES
NO
NO
YES
NO
YES
YES
NO
NO
NO
NO
NO
YES
YES
NO
NO
NO
YES
YES
NO
NO
YES
NO
NO
NO
YES
YES
YES
YES
NO
NO
YES
NO
YES
NO
NO
NO
YES
NO
NO
NO
NO
YES
NO
YES
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YE...

result:

ok 321346 lines