Datasets:
File size: 4,966 Bytes
c01da15 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | #include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
struct Solver {
int n, q;
int B; // heavy threshold
int W; // words in a bitset = (n+63)/64
vector<vector<int>> adj;
vector<int> deg;
vector<char> isHeavy;
vector<int> hId;
vector<vector<ull>> hBits; // bitsets for heavy rows
vector<vector<int>> hDiff; // hDiff[i][j] = |heavy_i \ heavy_j|
vector<int> heavyRows; // heavyRows[i] = row id
vector<int> tag;
int timer;
set<pair<int,int>> ord; // (degree, row id)
long long bad; // number of incomparable adjacent pairs
Solver(int n_, int q_) : n(n_), q(q_) {
B = max(1, (int)std::sqrt((double)q));
W = (n + 63) >> 6;
adj.assign(n, {});
deg.assign(n, 0);
isHeavy.assign(n, 0);
hId.assign(n, -1);
heavyRows.clear();
tag.assign(n, 0);
timer = 1;
ord.clear();
for (int i = 0; i < n; ++i) ord.insert({0, i});
bad = 0;
}
// return true iff N(a) ⊆ N(b) or N(b) ⊆ N(a)
bool comp(int a, int b) {
if (a == b) return true;
if (deg[a] > deg[b]) swap(a, b); // a is the smaller (or equal) set
if (deg[a] > B) { // both are heavy
int ha = hId[a], hb = hId[b];
return hDiff[ha][hb] == 0; // a ⊆ b
}
if (isHeavy[b]) { // a light, b heavy
const auto &bits = hBits[hId[b]];
for (int col : adj[a]) {
if ( ((bits[col >> 6] >> (col & 63)) & 1ULL) == 0 )
return false;
}
return true;
}
// both light
++timer;
for (int col : adj[b]) tag[col] = timer;
for (int col : adj[a]) if (tag[col] != timer) return false;
return true;
}
void makeHeavy(int r) {
int id = (int)hBits.size();
hId[r] = id;
isHeavy[r] = 1;
vector<ull> bits(W, 0);
for (int col : adj[r]) bits[col >> 6] |= 1ULL << (col & 63);
hBits.push_back(move(bits));
for (auto &row : hDiff) row.push_back(0);
hDiff.emplace_back(hBits.size(), 0);
for (int hi = 0; hi < id; ++hi) {
int common = 0;
for (int col : adj[r]) {
if ( (hBits[hi][col >> 6] >> (col & 63)) & 1ULL ) ++common;
}
hDiff[id][hi] = deg[r] - common; // r \ h
hDiff[hi][id] = deg[heavyRows[hi]] - common; // h \ r
}
heavyRows.push_back(r);
}
void addHeavyEdge(int r, int c) {
int id = hId[r];
for (int hi = 0; hi < (int)hBits.size(); ++hi) {
if (hi == id) continue;
if ( (hBits[hi][c >> 6] >> (c & 63)) & 1ULL ) {
--hDiff[hi][id]; // hi had c, r did not, now it does
} else {
++hDiff[id][hi]; // r gains c which hi lacks
}
}
hBits[id][c >> 6] |= 1ULL << (c & 63);
}
void addEdge(int r, int c) {
adj[r].push_back(c);
++deg[r];
if (isHeavy[r]) {
addHeavyEdge(r, c);
} else if (deg[r] > B) {
makeHeavy(r);
}
}
string answerQuery(int r, int c) {
// 1) remove r from the degree order, updating the counter
auto it = ord.find({deg[r], r});
int oldL = -1, oldR = -1;
if (it != ord.begin()) {
auto it2 = it; --it2;
oldL = it2->second;
}
auto itNxt = next(it);
if (itNxt != ord.end()) oldR = itNxt->second;
if (oldL != -1 && !comp(oldL, r)) --bad;
if (oldR != -1 && !comp(r, oldR)) --bad;
if (oldL != -1 && oldR != -1 && !comp(oldL, oldR)) ++bad;
ord.erase(it);
// 2) actually color the cell black
addEdge(r, c);
// 3) insert r with its new degree and update the counter
auto ret = ord.insert({deg[r], r});
auto it2 = ret.first;
int newL = -1, newR = -1;
if (it2 != ord.begin()) {
auto it3 = it2; --it3;
newL = it3->second;
}
auto it4 = next(it2);
if (it4 != ord.end()) newR = it4->second;
if (newL != -1 && newR != -1 && !comp(newL, newR)) --bad;
if (newL != -1 && !comp(newL, r)) ++bad;
if (newR != -1 && !comp(r, newR)) ++bad;
return bad == 0 ? "YES" : "NO";
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
if (!(cin >> T)) return 0;
while (T--) {
int n, q;
cin >> n >> q;
Solver solver(n, q);
for (int i = 0; i < q; ++i) {
int r, c;
cin >> r >> c;
--r; --c;
cout << solver.answerQuery(r, c) << '\n';
}
}
return 0;
} |