anonymous1926's picture
Release fresh-CF: 373 executable problems and frozen training data
c01da15 verified
Raw History Blame Contribute Delete
4.97 kB
#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;
}