QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#226032 | #7523. Partially Free Meal | ammardab3an | RE | 0ms | 0kb | C++17 | 3.0kb | 2023-10-25 14:52:31 | 2023-10-25 14:52:31 |
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll ans[200010];
#define int long long
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(ll val){
node* nd = new node(val);
nd->left = nd->right = that_node;
return nd;
}
node* create_node(ll val, int cnt){
node* nd = new node(val, cnt);
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, ll idx){
if(l == r){
return create_node(root->sum+idx, root->cnt+1);
}
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 ks){
if(root->cnt < ks) return 1e18;
if(l == r){
return (root->sum)/(root->cnt)*ks;
}
int mid = (l+r)/2;
if(root->left->cnt >= ks) return get(root->left, l, mid, ks);
return root->left->sum + get(root->right, mid+1, r, 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;
ll cur1 = get(per[i], 0, 1e9, mid-1);
cur += cur1;
best = min(best, {cur, i});
}
assert(best.second >= mid-1);
ans[mid] = best.first;
solve(v, l, best.second, tl, mid-1);
solve(v, best.second, r, mid+1, tr);
}
main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
that_node = new node(0LL, 0LL);
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 = 200000;
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));
for(int i = 0; i <= n; i++){
if(per[i]->cnt != i) assert(0);
}
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: 0
Runtime Error
input:
3 2 5 4 3 3 7