QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#369278 | #6330. XOR Reachable | Vengeful_Spirit# | WA | 2ms | 7740kb | C++20 | 4.5kb | 2024-03-27 22:57:59 | 2024-03-27 22:58:00 |
Judging History
answer
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
struct node {
vector<pair<int, int>> in, ou;
}c[N];
ll sum = 0;
struct LCT {
int f[N], c[N][2], siz[N], st[N];
ll s[N], sg[N], v[N];
bool lz[N];
void init(int n) {
++n;
for(int i = 0; i < n; ++i) {
f[i] = c[i][0] = c[i][1] = lz[i] = 0;
s[i] = sg[i] = 0;
siz[i] = 1;
}
}
ll cal(int x) {
return 1ll * x * (x - 1) / 2;
}
bool nroot(int x) const {
return c[f[x]][0] == x || c[f[x]][1] == x;
}
void pushup(int x) {
int lc = c[x][0], rc = c[x][1];
siz[x] = siz[lc] + siz[rc] + 1;
s[x] = s[lc] + s[rc] + 1 + sg[x];
}
void swp(int x) {
swap(c[x][0], c[x][1]);
lz[x] ^= 1;
}
void pushdown(int x) {
int lc = c[x][0], rc = c[x][1];
if(lz[x]) {
if(lc) swp(lc);
if(rc) swp(rc);
lz[x] = 0;
}
}
void zigzag(int x) {
int y = f[x], z = f[y], typ = (c[y][0] == x);
if(nroot(y)) c[z][c[z][1] == y] = x;
f[x] = z, f[y] = x;
if(c[x][typ]) f[c[x][typ]] = y;
c[y][typ ^ 1] = c[x][typ];
c[x][typ] = y;
pushup(y);
}
void splay(int x) {
int y, tp = 0;
st[tp = 1] = y = x;
while(nroot(y)) st[++tp] = y = f[y];
while(tp) pushdown(st[tp--]);
for(; nroot(x); zigzag(x)) if(!nroot(f[x])) continue;
else zigzag((c[f[x]][0]==x) ^ (c[f[f[x]]][0]==f[x]) ? x : f[x]);
pushup(x);
}
void access(int x) {
for(int y = 0; x; x = f[y = x]) {
splay(x);
sg[x] -= s[y], s[x] -= s[y];
sg[x] += s[c[x][1]]; s[x] += s[c[x][1]];
c[x][1] = y; pushup(x);
}
}
int findroot(int x) {
access(x); splay(x); pushdown(x);
while(c[x][0]) pushdown(x=c[x][0]);
splay(x);
return x;
}
void split(int x, int y) {
makeroot(x);
access(y);
splay(y);
}
void makeroot(int x) {
access(x); splay(x);
swp(x);
pushup(x);
}
ll get(int x) {
makeroot(x);
cerr << "? " << x << " " << s[x] << "\n";
return cal(s[x]);
}
void link(int x, int y) {
cerr << "link" << x << " " << y << "\n";
sum -= get(x) + get(y);
makeroot(x);
if(x != findroot(y)) {
makeroot(x); f[x] = y;
sg[y] += s[x], s[y] += s[x];
}
sum += get(x);
cerr << "sum = " << sum << "\n";
}
void cut(int x, int y) {
cerr << "cut" << x << " " << y << "\n";
makeroot(x);
sum -= get(x);
if(x == findroot(y)) {
pushdown(x);
if(c[x][1] == y && !c[y][0] && !c[y][1]) {
c[x][1] = f[y] = 0;
pushup(x);
}
}
sum += get(x) + get(y);
cerr << "sum = " << sum << "\n";
}
} tr;
map<int, int> mp;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, k, res = 0;
cin >> n >> m >> k;
tr.init(n);
vector<pair<int, int>> q;
for(int i = 1, x, y, z; i <= m; ++i) {
cin >> x >> y >> z;
int pos = 0;
// cerr << i << "=======\n";
for(int i = 29; i >= 0; --i) {
int ki = (k >> i) & 1, zi = (z >> i) & 1;
if(ki) {
int l = pos | ((zi) << i), r = l + ((1 << i) - 1) + 1;
// cerr << l << " " << r << "\n";
if(!mp.count(l)) mp[l] = ++res;
if(!mp.count(r)) mp[r] = ++res;
(c[mp[l]].in).push_back({x, y});
(c[mp[r]].ou).push_back({x, y});
}
pos |= (ki != zi) << i;
}
}
int Q;
cin >> Q;
for(int o = 0, x; o < Q; ++o) {
cin >> x;
q.push_back({x, o});
}
sort(all(q));
vector<ll> ans(Q);
auto it = mp.begin();
for(auto [val, id] : q) {
cerr << val << "val----\n";
for(; it != mp.end() && (*it).first <= val; ++it) {
int i = (*it).second;
for(auto[x, y] : c[i].in) tr.link(x, y);
for(auto[x, y] : c[i].ou) tr.cut(x, y);
}
ans[id] = sum;
}
for(ll x: ans) cout << x << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 7692kb
input:
4 5 5 1 2 17 1 3 4 2 3 20 2 4 3 3 4 5 4 0 7 16 167
output:
2 6 3 0
result:
ok 4 number(s): "2 6 3 0"
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 7740kb
input:
9 13 488888932 2 7 771479959 3 8 783850182 5 7 430673756 6 8 350738034 4 9 400768807 2 3 83653266 1 2 829786563 5 8 357613791 7 9 579696618 3 7 423191200 3 5 867380255 1 9 907715012 6 9 1033650694 8 498260055 144262908 117665696 848664012 983408133 32610599 478007408 134182829
output:
-157 -66 -31 -167 -172 16 -157 -31
result:
wrong answer 1st numbers differ - expected: '16', found: '-157'