QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#177484 | #6872. Magma Cave | PPP# | RE | 0ms | 0kb | C++17 | 5.5kb | 2023-09-13 00:15:36 | 2023-09-13 00:15: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 = 200'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 p[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();
vector<int> good, bad;
for (int T: inter) {
if (unite(u[T], v[T])) {
good.emplace_back(T);
} else {
bad.emplace_back(T);
when[ST][T] = min(when[ST][T], tr - 1);
}
}
do_roll(SZ);
if (tl == tr) {
return;
}
vector<int> inter_1, inter_2;
for (int T : inter) {
if (T <= tm || when[ST][T] < tr) {
inter_1.emplace_back(T);
}
}
go(tl, tm, inter_1);
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);
}
int bck[maxN];
int fenw[maxN];
int cnt[2][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];
void solve() {
cin >> n >> q;
for (int i = 1; i <= q; i++) {
cin >> tp[i];
if (tp[i] == 1) {
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 i = 1; i <= q; i++) {
// if (tp[i] == 1) {
// cout << i << " " << when[ST][i] << endl;
// }
// }
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;
// }
// }
// cout << " HIHI " << " " << q << endl;
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);
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;
// cout << " HIHIHIHI " << k[i] << " " << get(w[his_id]) << " " << w[his_id] << endl;
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 = 1;
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 ...