QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#798501 | #8790. First Billion | q1w2e3r4# | ML | 24ms | 42636kb | C++14 | 2.7kb | 2024-12-04 14:14:35 | 2024-12-04 14:14:35 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#include<random>
mt19937_64 rng(time(0));
int n;
int a[105];
vector<int> v[405];
const int SZ = 4000;
int findkth(int k, vector<int>& v, int l, int r){
if(r - l == 1){
int a = 1;
}
// cout << "kth " << l << " " << r << endl;
if(l == r) return v[l];
int len = r - l + 1;
int idx = l + rng() % len;
int pivot = v[idx];
vector<int> small, big;
for(int i=l;i<=r;i++){
int x = v[i];
if(x <= pivot) small.push_back(x);
else big.push_back(x);
}
int n = small.size(), m = big.size();
for(int i=0;i<n;i++){
v[i+l] = small[i];
}
for(int i=0;i<m;i++){
v[i+n+l] = big[i];
}
if(!n || !m) return pivot;
small.clear(), big.clear();
if(n >= k) return findkth(k, v, l, l+n-1);
else return findkth(k-n, v, l+n, r);
}
void build(int id, int l, int r){
if(l == r){
v[id].push_back(a[l]);
v[id].push_back(0);
return;
}
int mid = l + r >> 1;
build(id*2, l, mid);
build(id*2+1, mid+1, r);
vector<int> tmp;
for(int x: v[id*2]){
for(int y: v[id*2+1]){
tmp.push_back(x+y);
}
}
if(tmp.size() >= SZ){
int n = tmp.size();
int al = findkth(n/2 - SZ/2, tmp, 0, n-1), ar = findkth(n/2 + SZ/2 - 1, tmp, 0, n-1);
for(int x: tmp){
if(x >= al && x <= ar){
v[id].push_back(x);
}
}
}
else{
v[id] = tmp;
}
}
void query(int id, int l, int r, int target, vector<int>& ans){
// cout << "query " << l << " " << r << " " << target << endl;
if(l == r){
if(target == a[l]) ans.push_back(l);
assert(target == 0 || target == a[l]);
return;
}
int mid = l + r >> 1;
for(auto x: v[id*2]){
for(int y: v[id*2+1]){
if(x + y == target){
query(id*2, l, mid, x, ans);
query(id*2+1, mid+1, r, y, ans);
return;
}
}
}
}
signed main(){
cin >> n;
for(int i=1;i<=n;i++) cin >> a[i];
// int n = 100;
// for(int i=1;i<=n;i++){
// a[i] = rng() % (int)4e7 + 1;
// // cout << a[i] << " ";
// }
// cout << endl;
build(1,1,n);
bool ok = false;
for(auto x: v[1]){
// cout << x << " ";
if(x == 1e9) ok = true;
}
// assert(ok);
vector<int> ans;
query(1,1,n,(int)1e9,ans);
cout << ans.size();
for(int x: ans){
cout << " " << x;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3792kb
input:
10 386413329 88494216 245947398 316438989 192751270 204627269 65749456 3938400 150458676 345180997
output:
5 1 5 6 7 9
result:
ok OK (n = 10)
Test #2:
score: 0
Accepted
time: 0ms
memory: 3696kb
input:
10 119486233 299942886 169540407 349937991 597883752 32230162 140514533 57341098 12602102 220520836
output:
5 1 3 4 7 10
result:
ok OK (n = 10)
Test #3:
score: 0
Accepted
time: 1ms
memory: 4036kb
input:
14 384615281 84612238 83310504 54746763 142296081 56775470 128760350 343006424 177232390 214368720 67220468 21895072 16352717 224807522
output:
7 1 6 7 9 10 12 13
result:
ok OK (n = 14)
Test #4:
score: 0
Accepted
time: 1ms
memory: 4188kb
input:
14 270208635 14270307 89661499 113578022 47687195 101043954 38775146 208193324 650676076 351701957 3427619 59535626 24230888 27009752
output:
7 1 2 3 4 6 10 12
result:
ok OK (n = 14)
Test #5:
score: 0
Accepted
time: 24ms
memory: 42636kb
input:
20 61638928 106712373 5946815 178135484 4937573 111395400 15504655 67139983 101814514 312223647 130341028 43244171 37671364 54108486 337181317 37924824 153793862 70383750 102917244 66984582
output:
10 1 2 3 6 7 15 16 17 19 20
result:
ok OK (n = 20)
Test #6:
score: 0
Accepted
time: 15ms
memory: 41024kb
input:
20 67858098 61231428 99398662 1883806 82465954 303619377 87516412 154956240 94872199 76508350 13276828 136541811 203282099 99160366 127539385 13364660 141176136 39751629 67888657 127707903
output:
10 1 3 4 5 8 11 12 13 14 17
result:
ok OK (n = 20)
Test #7:
score: -100
Memory Limit Exceeded
input:
24 17125795 281143405 10375259 196293002 158174864 34520650 52919232 87393970 99085271 62281508 67168428 55174991 54533464 51393059 89276370 41441658 72793517 30466999 73758332 97064918 111541434 142047546 12934221 101092107
output:
12 1 2 6 7 8 9 11 12 14 16 21 24