QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#798499#8790. First Billionq1w2e3r4#TL 0ms0kbC++142.6kb2024-12-04 14:12:382024-12-04 14:12:47

Judging History

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

  • [2024-12-04 14:12:47]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-12-04 14:12:38]
  • 提交

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 = 3000;

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;
    }
}

详细

Test #1:

score: 0
Time Limit Exceeded

input:

10
386413329 88494216 245947398 316438989 192751270 204627269 65749456 3938400 150458676 345180997

output:



result: