QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#679106 | #9519. Build a Computer | ucup-team5243# | AC ✓ | 0ms | 3868kb | C++17 | 4.6kb | 2024-10-26 16:54:00 | 2024-10-26 16:54:02 |
Judging History
answer
#ifdef NACHIA
#define _GLIBCXX_DEBUG
#else
#define NDEBUG
#endif
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(int i=0; i<int(n); i++)
const i64 INF = 1001001001001001001;
template<typename A> void chmin(A& l, const A& r){ if(r < l) l = r; }
template<typename A> void chmax(A& l, const A& r){ if(l < r) l = r; }
using namespace std;
namespace nachia {
struct DsuFast{
private:
std::vector<int> w;
public:
DsuFast(int n = 0) : w(n, -1) {}
int leader(int u){
if(w[u] < 0) return u;
return w[u] = leader(w[u]);
}
int operator[](int u){ return leader(u); }
int merge(int u, int v){
u = leader(u);
v = leader(v);
if(u == v) return u;
if(-w[u] < -w[v]) std::swap(u, v);
w[u] += w[v];
w[v] = u;
return u;
}
int size(int u){ return -w[leader(u)]; }
bool same(int u, int v){ return leader(u) == leader(v); }
};
} // namespace nachia
void testcase(){
i64 L, R; cin >> L >> R;
int msbR = 20;
while(R < (1 << msbR)) msbR--;
int msbL = 20;
while(L < (1 << msbL)) msbL--;
if(L == R){
cout << (msbR+2) << '\n';
rep(i,msbR+1){
int v = (R >> (msbR-i)) & 1;
cout << "1 " << (i+2) << " " << v << '\n';
}
cout << "0\n";
return;
}
auto dsu = nachia::DsuFast(10000);
int B = 20;
struct Edge {
int u;
int v;
int w;
};
int N = B + 2;
int src = B + 1;
int snk = 0;
vector<Edge> X;
rep(j,20) rep(t,2) X.push_back({ j+1, j, t });
X.push_back({ 0, src, -1 });
auto procL = [&](int s, int b) -> void {
for(int t=b-1; t>=0; t--){
int ss = N++;
int lbit = (L >> t) & 1;
X.push_back({ s, ss, lbit });
if(lbit == 0){
X.push_back({ s, t, 1 });
}
s = ss;
}
dsu.merge(s, snk);
};
auto procR = [&](int s, int b) -> void {
for(int t=b-1; t>=0; t--){
int ss = N++;
int lbit = (R >> t) & 1;
X.push_back({ s, ss, lbit });
if(lbit == 1){
X.push_back({ s, t, 0 });
}
s = ss;
}
dsu.merge(s, snk);
};
int lstart = src;
int rstart = src;
if(msbL == msbR){
while(true){
int lbit = (L >> msbL) & 1;
int rbit = (R >> msbR) & 1;
if(lbit == rbit){
int ss = N++;
X.push_back({ lstart, ss, lbit });
lstart = rstart = ss;
msbL--;
msbR--;
continue;
}
break;
}
int s1 = N++;
X.push_back({ lstart, s1, 0 });
lstart = s1;
int s2 = N++;
X.push_back({ rstart, s2, 1 });
rstart = s2;
procL(lstart, msbL);
procR(rstart, msbR);
} else {
int s2 = N++;
X.push_back({ rstart, s2, 1 });
rstart = s2;
procR(rstart, msbR);
for(int k=msbL+1; k<msbR; k++){
X.push_back({ lstart, k, 1 });
}
int s1 = N++;
X.push_back({ lstart, s1, 1 });
lstart = s1;
procL(lstart, msbL);
}
for(auto& e : X){
e.u = dsu[e.u];
e.v = dsu[e.v];
}
{
vector<int> mp(N);
while(true){
vector<int> id(N);
vector<int> od(N);
for(auto e : X) if(mp[e.u] >= 0 && mp[e.v] >= 0){
id[e.u] += 1;
od[e.v] += 1;
}
int cnt = 0;
rep(i,N) if(mp[i] != -1 && (id[i] == 0 || od[i] == 0)){ mp[i] = -1; cnt++; }
if(cnt == 0) break;
}
int f = 0;
rep(i,N) if(mp[i] >= 0) mp[i] = f++;
N = f;
vector<Edge> Y;
for(auto e : X){
e.u = mp[e.u];
e.v = mp[e.v];
if(e.u >= 0 && e.v >= 0 && e.w >= 0) Y.push_back(e);
}
swap(X, Y);
}
vector<vector<pair<int,int>>> adj(N);
for(auto e : X) adj[e.u].push_back({ e.v + 1, e.w });
cout << N << "\n";
for(auto a : adj){
cout << a.size();
for(auto [u,v] : a) cout << " " << u << " " << v;
cout << '\n';
}
}
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
testcase();
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3564kb
input:
5 7
output:
5 1 2 1 2 3 0 4 1 1 5 1 2 5 1 5 0 0
result:
ok ok
Test #2:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
10 27
output:
12 2 9 0 9 1 2 1 0 1 1 2 2 0 2 1 2 5 1 10 1 2 6 1 3 0 1 7 0 2 8 1 1 0 2 9 1 9 0 0 2 11 0 2 1 1 12 1 2 9 0 9 1
result:
ok ok
Test #3:
score: 0
Accepted
time: 0ms
memory: 3856kb
input:
5 13
output:
9 2 7 0 7 1 2 1 0 1 1 2 4 1 8 1 2 5 1 2 0 1 6 0 2 7 1 7 0 0 2 9 0 1 1 1 7 1
result:
ok ok
Test #4:
score: 0
Accepted
time: 0ms
memory: 3576kb
input:
1 1000000
output:
39 2 39 0 39 1 2 1 0 1 1 2 2 0 2 1 2 3 0 3 1 2 4 0 4 1 2 5 0 5 1 2 6 0 6 1 2 7 0 7 1 2 8 0 8 1 2 9 0 9 1 2 10 0 10 1 2 11 0 11 1 2 12 0 12 1 2 13 0 13 1 2 14 0 14 1 2 15 0 15 1 2 16 0 16 1 2 17 0 17 1 20 20 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 39 1 2 21 ...
result:
ok ok
Test #5:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
1 1
output:
2 1 2 1 0
result:
ok ok
Test #6:
score: 0
Accepted
time: 0ms
memory: 3572kb
input:
7 9
output:
7 2 2 1 6 1 1 3 0 1 4 0 2 5 1 5 0 0 1 7 1 1 5 1
result:
ok ok
Test #7:
score: 0
Accepted
time: 0ms
memory: 3856kb
input:
3 7
output:
6 2 5 0 5 1 2 3 1 6 1 2 4 1 1 0 2 5 1 5 0 0 1 5 1
result:
ok ok
Test #8:
score: 0
Accepted
time: 0ms
memory: 3852kb
input:
1 5
output:
5 2 5 0 5 1 3 3 1 1 1 5 1 1 4 0 2 5 1 5 0 0
result:
ok ok
Test #9:
score: 0
Accepted
time: 0ms
memory: 3656kb
input:
1 4
output:
5 2 5 0 5 1 3 3 1 1 1 5 1 1 4 0 1 5 0 0
result:
ok ok
Test #10:
score: 0
Accepted
time: 0ms
memory: 3524kb
input:
8 9
output:
5 1 2 1 1 3 0 1 4 0 2 5 0 5 1 0
result:
ok ok
Test #11:
score: 0
Accepted
time: 0ms
memory: 3520kb
input:
7 51
output:
13 2 11 0 11 1 2 1 0 1 1 2 2 0 2 1 2 3 0 3 1 4 6 1 3 1 4 1 12 1 2 7 1 4 0 1 8 0 1 9 0 2 10 1 1 0 2 11 1 11 0 0 1 13 1 1 11 1
result:
ok ok
Test #12:
score: 0
Accepted
time: 0ms
memory: 3848kb
input:
51 79
output:
16 2 11 0 11 1 2 1 0 1 1 2 2 0 2 1 2 5 1 12 1 1 6 0 1 7 0 2 8 1 3 0 2 9 1 2 0 2 10 1 1 0 2 11 1 11 0 0 1 13 1 2 14 0 3 1 2 15 0 2 1 1 16 1 1 11 1
result:
ok ok
Test #13:
score: 0
Accepted
time: 0ms
memory: 3576kb
input:
92 99
output:
14 2 10 0 10 1 1 3 1 2 4 0 5 1 1 6 1 1 11 0 1 7 1 1 8 1 2 9 0 1 1 2 10 0 10 1 0 1 12 0 1 13 0 2 14 1 1 0 2 10 1 10 0
result:
ok ok
Test #14:
score: 0
Accepted
time: 0ms
memory: 3868kb
input:
27 36
output:
13 2 9 0 9 1 2 1 0 1 1 2 4 1 10 1 1 5 0 1 6 0 2 7 1 2 0 1 8 0 1 9 0 0 1 11 1 2 12 0 2 1 1 13 1 1 9 1
result:
ok ok
Test #15:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
55 84
output:
17 2 12 0 12 1 2 1 0 1 1 2 2 0 2 1 2 3 0 3 1 2 6 1 13 1 1 7 0 2 8 1 4 0 1 9 0 2 10 1 2 0 1 11 0 1 12 0 0 1 14 1 2 15 0 3 1 1 16 1 1 17 1 1 12 1
result:
ok ok
Test #16:
score: 0
Accepted
time: 0ms
memory: 3824kb
input:
297208 929600
output:
57 2 39 0 39 1 2 1 0 1 1 2 2 0 2 1 2 3 0 3 1 2 4 0 4 1 2 5 0 5 1 2 6 0 6 1 2 7 0 7 1 2 8 0 8 1 2 9 0 9 1 2 10 0 10 1 2 11 0 11 1 2 12 0 12 1 2 13 0 13 1 2 14 0 14 1 2 15 0 15 1 2 16 0 16 1 2 17 0 17 1 2 20 1 40 1 2 21 1 18 0 2 22 1 17 0 1 23 0 1 24 0 1 25 0 2 26 1 13 0 1 27 0 2 28 1 11 0 2 29 1 10 0...
result:
ok ok
Test #17:
score: 0
Accepted
time: 0ms
memory: 3780kb
input:
45728 589156
output:
54 2 39 0 39 1 2 1 0 1 1 2 2 0 2 1 2 3 0 3 1 2 4 0 4 1 2 5 0 5 1 2 6 0 6 1 2 7 0 7 1 2 8 0 8 1 2 9 0 9 1 2 10 0 10 1 2 11 0 11 1 2 12 0 12 1 2 13 0 13 1 2 14 0 14 1 2 15 0 15 1 2 16 0 16 1 2 17 0 17 1 5 20 1 16 1 17 1 18 1 40 1 1 21 0 1 22 0 1 23 0 2 24 1 15 0 2 25 1 14 0 2 26 1 13 0 2 27 1 12 0 2 2...
result:
ok ok
Test #18:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
129152 138000
output:
47 2 31 0 31 1 2 1 0 1 1 2 2 0 2 1 2 3 0 3 1 2 4 0 4 1 2 5 0 5 1 2 6 0 6 1 2 7 0 7 1 2 8 0 8 1 2 9 0 9 1 2 10 0 10 1 2 11 0 11 1 2 14 1 32 1 1 15 0 1 16 0 1 17 0 1 18 0 2 19 1 12 0 2 20 1 11 0 1 21 0 2 22 1 9 0 2 23 1 8 0 1 24 0 1 25 0 1 26 0 2 27 1 4 0 1 28 0 1 29 0 1 30 0 1 31 0 0 1 33 1 1 34 1 1 ...
result:
ok ok
Test #19:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
245280 654141
output:
56 2 39 0 39 1 2 1 0 1 1 2 2 0 2 1 2 3 0 3 1 2 4 0 4 1 2 5 0 5 1 2 6 0 6 1 2 7 0 7 1 2 8 0 8 1 2 9 0 9 1 2 10 0 10 1 2 11 0 11 1 2 12 0 12 1 2 13 0 13 1 2 14 0 14 1 2 15 0 15 1 2 16 0 16 1 2 17 0 17 1 3 20 1 18 1 40 1 1 21 0 1 22 0 2 23 1 16 0 2 24 1 15 0 2 25 1 14 0 2 26 1 13 0 2 27 1 12 0 2 28 1 1...
result:
ok ok
Test #20:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
202985 296000
output:
52 2 35 0 35 1 2 1 0 1 1 2 2 0 2 1 2 3 0 3 1 2 4 0 4 1 2 5 0 5 1 2 6 0 6 1 2 7 0 7 1 2 8 0 8 1 2 9 0 9 1 2 10 0 10 1 2 11 0 11 1 2 12 0 12 1 2 13 0 13 1 2 14 0 14 1 2 17 1 36 1 1 18 0 1 19 0 2 20 1 15 0 1 21 0 1 22 0 1 23 0 1 24 0 2 25 1 10 0 1 26 0 1 27 0 1 28 0 2 29 1 6 0 1 30 0 1 31 0 1 32 0 1 33...
result:
ok ok
Test #21:
score: 0
Accepted
time: 0ms
memory: 3648kb
input:
438671 951305
output:
57 2 39 0 39 1 2 1 0 1 1 2 2 0 2 1 2 3 0 3 1 2 4 0 4 1 2 5 0 5 1 2 6 0 6 1 2 7 0 7 1 2 8 0 8 1 2 9 0 9 1 2 10 0 10 1 2 11 0 11 1 2 12 0 12 1 2 13 0 13 1 2 14 0 14 1 2 15 0 15 1 2 16 0 16 1 2 17 0 17 1 2 20 1 40 1 2 21 1 18 0 2 22 1 17 0 1 23 0 2 24 1 15 0 1 25 0 1 26 0 1 27 0 1 28 0 2 29 1 10 0 1 30...
result:
ok ok
Test #22:
score: 0
Accepted
time: 0ms
memory: 3640kb
input:
425249 739633
output:
56 2 38 0 38 1 2 1 0 1 1 2 2 0 2 1 2 3 0 3 1 2 4 0 4 1 2 5 0 5 1 2 6 0 6 1 2 7 0 7 1 2 8 0 8 1 2 9 0 9 1 2 10 0 10 1 2 11 0 11 1 2 12 0 12 1 2 13 0 13 1 2 14 0 14 1 2 15 0 15 1 2 16 0 16 1 2 19 1 39 1 1 20 0 2 21 1 17 0 2 22 1 16 0 1 23 0 2 24 1 14 0 1 25 0 1 26 0 2 27 1 11 0 1 28 0 1 29 0 2 30 1 8 ...
result:
ok ok
Test #23:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
551207 961718
output:
56 2 39 0 39 1 2 1 0 1 1 2 2 0 2 1 2 3 0 3 1 2 4 0 4 1 2 5 0 5 1 2 6 0 6 1 2 7 0 7 1 2 8 0 8 1 2 9 0 9 1 2 10 0 10 1 2 11 0 11 1 2 12 0 12 1 2 13 0 13 1 2 14 0 14 1 2 15 0 15 1 2 16 0 16 1 1 19 1 2 20 0 21 1 2 22 0 17 1 2 40 1 17 0 2 23 0 16 1 2 24 0 15 1 1 25 1 1 26 1 2 27 0 12 1 1 28 1 2 29 0 10 1...
result:
ok ok
Test #24:
score: 0
Accepted
time: 0ms
memory: 3648kb
input:
114691 598186
output:
55 2 39 0 39 1 2 1 0 1 1 2 2 0 2 1 2 3 0 3 1 2 4 0 4 1 2 5 0 5 1 2 6 0 6 1 2 7 0 7 1 2 8 0 8 1 2 9 0 9 1 2 10 0 10 1 2 11 0 11 1 2 12 0 12 1 2 13 0 13 1 2 14 0 14 1 2 15 0 15 1 2 16 0 16 1 2 17 0 17 1 4 20 1 17 1 18 1 40 1 1 21 0 1 22 0 2 23 1 16 0 1 24 0 1 25 0 2 26 1 13 0 1 27 0 1 28 0 1 29 0 1 30...
result:
ok ok
Test #25:
score: 0
Accepted
time: 0ms
memory: 3580kb
input:
234654 253129
output:
46 2 33 0 33 1 2 1 0 1 1 2 2 0 2 1 2 3 0 3 1 2 4 0 4 1 2 5 0 5 1 2 6 0 6 1 2 7 0 7 1 2 8 0 8 1 2 9 0 9 1 2 10 0 10 1 2 11 0 11 1 2 12 0 12 1 1 15 1 1 16 1 1 17 1 2 18 0 19 1 2 20 0 13 1 1 34 0 1 21 1 2 22 0 11 1 1 23 1 2 24 0 9 1 2 25 0 8 1 1 26 1 2 27 0 6 1 2 28 0 5 1 1 29 1 1 30 1 1 31 1 1 32 1 2 ...
result:
ok ok
Test #26:
score: 0
Accepted
time: 0ms
memory: 3676kb
input:
554090 608599
output:
52 2 37 0 37 1 2 1 0 1 1 2 2 0 2 1 2 3 0 3 1 2 4 0 4 1 2 5 0 5 1 2 6 0 6 1 2 7 0 7 1 2 8 0 8 1 2 9 0 9 1 2 10 0 10 1 2 11 0 11 1 2 12 0 12 1 2 13 0 13 1 2 14 0 14 1 1 17 1 1 18 0 1 19 0 2 20 0 21 1 2 22 0 15 1 1 38 0 1 23 1 1 24 1 1 25 1 2 26 0 11 1 1 27 1 2 28 0 9 1 2 29 0 8 1 2 30 0 7 1 1 31 1 1 3...
result:
ok ok
Extra Test:
score: 0
Extra Test Passed