QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#97651#6330. XOR Reachablewhatever#RE 1ms7524kbC++142.3kb2023-04-17 19:38:362023-04-17 19:38:39

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-17 19:38:39]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:7524kb
  • [2023-04-17 19:38:36]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
using pii = pair<int, int>;
using i64 = long long;
#define rep(i, a, b) for (int i = a, I = b; i <= I; ++i)
#define per(i, a, b) for (int i = a, I = b; i >= I; --i)
template<typename T> void up(T &x, T y) { if (x < y) x = y; }
template<typename T> void down(T &x, T y) { if (x > y) x = y; }

const int N = 100005, M = N * 30;
int n, m, k, x[N], y[N], z[N], q, tot = 1, ch[M][2];
vector<int> mdf[N];
i64 ans, res[N];
int par[N], siz[N], sx[N], sy[N], top;

int find(int x) {
    while (x != par[x]) x = par[x];
    return x;
}
void merge(int id) {
    int u = find(x[id]), v = find(y[id]);
    if (u == v) return;
    if (siz[u] < siz[v]) swap(u, v);
    par[v] = u;
    ans += 1ll * siz[u] * siz[v];
    siz[u] += siz[v];
    sx[++top] = u, sy[top] = v;
}
void clear(int prv) {
    for (; top > prv; --top) {
        int u = sx[top], v = sy[top];
        siz[u] -= siz[v];
        par[v] = v;
        ans -= 1ll * siz[u] * siz[v];
    }
}

void insert(int id, int x) {
    int p = 1;
    per(i, 29, 0) {
        int &q = ch[p][x >> i & 1];
        if (!q) q = ++tot;
        mdf[q].push_back(id);
        p = q;
    }
}
void solve(int p, int d, vector<pii> qry) {
    if (!p || d == -1) {
        for (auto [_, id] : qry) res[id] = ans;
        return;
    }
    vector<pii> q[2];
    for (const auto &it : qry) q[(it.first >> d & 1)].push_back(it);
    if (k >> d & 1) {
        int prv = top;
        rep(i, 0, 1) {
            if (q[i].empty()) continue;
            for (auto id : mdf[ch[p][i]]) merge(id);
            solve(ch[p][!i], d - 1, q[i]);
            clear(prv);
        }
    } else {
        rep(i, 0, 1) {
            if (q[i].empty()) continue;
            solve(ch[p][i], d - 1, q[i]);
        }
    }
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0);

    cin >> n >> m >> k;
    rep(i, 1, m) {
        cin >> x[i] >> y[i] >> z[i];
        insert(i, z[i]);
    }
    rep(i, 1, n) par[i] = i, siz[i] = 1;

    cin >> q;
    vector<pii> qry;
    rep(i, 1, q) {
        int x; cin >> x;
        qry.push_back({x, i});
    }
    solve(1, 29, qry);
    rep(i, 1, q) cout << res[i] << "\n";

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 7476kb

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: 0
Accepted
time: 1ms
memory: 7524kb

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:

16
7
5
13
13
16
16
5

result:

ok 8 numbers

Test #3:

score: -100
Runtime Error

input:

446 99235 764320136
1 2 467639025
1 39 240791819
1 197 320023434
1 391 968343602
1 116 841220144
1 345 697806443
1 409 489643820
1 339 733516272
1 89 560099922
1 431 722346703
1 433 246809211
1 344 769002876
1 234 597076758
1 178 505730391
1 75 826728597
1 74 14756981
1 63 280238017
1 288 638627144
...

output:


result: