QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#225742 | #7523. Partially Free Meal | ammardab3an | RE | 0ms | 3648kb | C++17 | 3.1kb | 2023-10-25 03:51:08 | 2023-10-25 03:51:09 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll ans[200010];
struct node {
ll sum;
int cnt;
node *left, *right;
node(ll sum){
this->sum = sum;
this->cnt = 1;
left = right = nullptr;
}
node(ll sum, int cnt){
this->sum = sum;
this->cnt = cnt;
left = right = nullptr;
}
node(node *left, node *right){
sum = (left ? left->sum : 0) + (right ? right->sum : 0);
cnt = (left ? left->cnt : 0) + (right ? right->cnt : 0);
this->left = left;
this->right = right;
}
};
vector<node*> per;
node* that_node = nullptr;
node* create_node(int val){
node* nd = new node(val);
nd->left = nd->right = that_node;
return nd;
}
node* create_node(node* left, node* right){
node* nd = new node(left, right);
return nd;
}
node* add(node*root, int l, int r, int idx){
if(l == r){
return create_node(idx);
}
int mid = (l + r)/2;
if(idx <= mid) return create_node(add(root->left, l, mid, idx), root->right);
else return create_node(root->left, add(root->right, mid+1, r, idx));
}
ll get(node* root, int l, int r, int tl, int tr, int ks){
if(tl > tr) return 0;
if(root == nullptr){
while(true){
int x = 1;
}
}
if(l == -1 || r == -1){
while(true){
int x = 1;
}
}
if(root->cnt < ks) return 1e18;
if(l == tl && r == tr) return root->sum;
int mid = (l+r)/2;
if(root->left == nullptr || root->right == nullptr){
while(true){
int x = 1;
}
}
if(root->left->cnt >= ks) return get(root->left, l, mid, tl, min(mid, tr), ks);
return root->left->sum + get(root->right, mid+1, r, max(mid+1, tl), tr, ks - (root->left->cnt));
}
void solve(const vector<pair<int, int>> &v, int l, int r, int tl, int tr){
if(tl > tr) return;
int mid = (tl+tr)/2;
pair<ll, int> best(1e18, -1);
for(int i = l; i <= r; i++){
ll cur = v[i].first + v[i].second;
cur += get(per[i], 0, 1e9, 0, 1e9, mid-1);
best = min(best, {cur, i});
}
ans[mid] = best.first;
solve(v, l, best.second, tl, mid-1);
solve(v, best.second, r, mid+1, tr);
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
that_node = new node(0, 0);
that_node->left = that_node;
that_node->right = that_node;
per.push_back(that_node);
int n;
cin >> n;
vector<pair<int, int>> v(n);
for(auto &[f, s] : v) cin >> s >> f;
if(n==0){
n = 1000;
v.resize(n);
srand(0);
for(auto &[a, b] : v){ a = abs(rand()); b = abs(rand()); }
for(auto &[a, b] : v){ cout << a << " " << b << endl;}
}
sort(v.begin(), v.end());
for(auto &[f, s] : v) swap(f, s), per.push_back(add(per.back(), 0, 1e9, f));
solve(v, 0, n-1, 1, n);
for(int i = 1; i <= n; i++){
cout << ans[i] << "\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3648kb
input:
3 2 5 4 3 3 7
output:
7 11 16
result:
ok 3 lines
Test #2:
score: -100
Runtime Error
input:
200000 466436993 804989151 660995237 756645598 432103296 703610564 6889895 53276988 873617076 822481192 532911431 126844295 623111499 456772252 937464699 762157133 708503076 786039753 78556972 5436013 582960979 398984169 786333369 325119902 930705057 615928139 924915828 506145001 164984329 208212435...