QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#99234#6330. XOR ReachableHEltim7WA 64ms14840kbC++232.7kb2023-04-21 17:54:292023-04-21 17:54:30

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-21 17:54:30]
  • 评测
  • 测评结果:WA
  • 用时:64ms
  • 内存:14840kb
  • [2023-04-21 17:54:29]
  • 提交

answer

#include <vector>
#include <cassert>
#include <numeric>
#include <iostream>
#include <algorithm>
using namespace std;

#define endl '\n'
using LL=long long;
using PII=pair<int,int>;
constexpr int N=1e5+10,B=30;
vector<int> op[N][2];
int a[N],b[N],c[N];
PII qry[N];
LL ans[N];

struct DisjointUnionSet {
    vector<int> fa,sz;
    vector<pair<int&,int>> fah,szh;

    void init(int n) {
        fa.resize(n+1);
        sz.resize(n+1,1);
        iota(fa.begin(), fa.end(), 0);
    }

    int find(int x) {
        while(x!=fa[x]) x=fa[x];
        return x;
    }

    bool same(int x,int y) {
        return find(x)==find(y);
    }

    LL cal(int x) {
        return 1LL*sz[x]*(sz[x]-1)/2;
    }

    bool join(int x,int y,LL &ans) {
        x=find(x);
        y=find(y);
        if(x==y) {
            fah.emplace_back(fa[0],fa[0]);
            szh.emplace_back(sz[0],sz[0]);
            return false;
        }
        ans-=cal(x);
        ans-=cal(y);
        if(sz[x]<sz[y]) swap(x,y);
        fah.emplace_back(fa[y],fa[y]);
        szh.emplace_back(sz[x],sz[x]);
        sz[x]+=sz[y];
        fa[y]=x;
        ans+=cal(x);
        return true;
    }

    void undo(LL &ans) {
        assert(!fah.empty());
        int x=fah.back().first;
        int y=fah.back().second;
        ans-=cal(x);
        fah.back().first=fah.back().second;
        szh.back().first=szh.back().second;
        ans+=cal(x);
        ans+=cal(y);
        fah.pop_back(),szh.pop_back();
    }

    int size(int x) {
        return sz[find(x)];
    }

    DisjointUnionSet() = default;
    DisjointUnionSet(int n) { init(n); }
} dsu(N);

void solve() {
    int n,m,k;
    cin>>n>>m>>k;
    for(int i=1;i<=m;i++) cin>>a[i]>>b[i]>>c[i];

    int q;
    cin>>q;
    for(int i=1;i<=q;i++) {
        cin>>qry[i].first;
        qry[i].second=i;
    }
    sort(qry+1,qry+q+1);

    for(int i=B-1;i>=0;i--) {
        if(k>>i&1^1) continue;
        for(int j=1;j<=m;j++) {
            int d=c[j]^k;
            d^=1<<i;
            int mask=~((1<<i)-1);
            int t=mask;
            d&=mask;

            int l=lower_bound(qry+1,qry+n+1,PII(d,0))-qry;
            int r=upper_bound(qry+1,qry+n+1,PII(d|~mask,N))-qry;
            if(l<r) {
                op[l][1].emplace_back(j);
                op[r][0].emplace_back(j);
            }
        }
    }

    LL res=0;
    for(int i=1;i<=q;i++) {
        for(int j:op[i][0]) dsu.undo(res);
        for(int j:op[i][1]) dsu.join(a[j], b[j], res);
        ans[qry[i].second]=res;
    }
    for(int i=1;i<=q;i++) cout<<ans[i]<<endl;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    solve();
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 6ms
memory: 10268kb

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: 2ms
memory: 10472kb

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
Wrong Answer
time: 64ms
memory: 14840kb

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:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

wrong answer 1st numbers differ - expected: '99235', found: '0'