Untitled
unknown
c_cpp
a year ago
3.1 kB
3
Indexable
#include "bits/stdc++.h" using namespace std; #ifdef LOCAL #include "debug.h" #else #define debug(x...) #endif struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; constexpr int K = 450; constexpr int N = 1e5; void solve() { int n, m; cin >> n >> m; vector<int> v(n); for (int &i : v) { cin >> i; i--; } vector<int> g[n]; int ans = 0; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; if (v[a] != v[b]) { ans++; } g[a].push_back(b); g[b].push_back(a); } auto is_heavy = [&](int a) { return (int)g[a].size() >= K; }; int dp[K][N]; unordered_map<int, int, custom_hash> mp; for (int i = 0; i < n; i++) { sort(g[i].begin(), g[i].end(), [&](int lhs, int rhs) { return g[lhs].size() > g[rhs].size() || (g[lhs].size() == g[rhs].size() && lhs > rhs); }); if (is_heavy(i)) { assert(mp.size() < K); mp[i] = mp.size(); fill(dp[mp[i]], dp[mp[i]] + N, 0); } } for (int i = 0; i < n; i++) { if (is_heavy(i)) { for (int &u : g[i]) { if (!is_heavy(u)) { dp[mp[i]][v[u]]++; } } } } int q; cin >> q; while (q--) { int C, V; cin >> C >> V; C--; V--; if (!is_heavy(C)) { for (int &u : g[C]) { if (!is_heavy(u)) { if (v[u] == v[C]) { ans++; } if (v[u] == V) { ans--; } } else { dp[mp[u]][v[C]]--; dp[mp[u]][V]++; if (v[u] == v[C]) { ans++; } if (v[u] == V) { ans--; } } } } else { for (int &u : g[V]) { if (!is_heavy(u)) break; if (v[u] == v[C]) { ans++; } if (v[u] == V) { ans--; } } ans -= dp[mp[C]][v[C]]; ans += dp[mp[C]][V]; } v[C] = V; cout << ans << '\n'; } } signed main() { ios::sync_with_stdio(false); cin.tie(NULL); int tests = 1; // cin >> tests; while (tests--) { solve(); } return 0; }
Editor is loading...