QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#225749 | #7523. Partially Free Meal | ammardab3an | WA | 276ms | 297764kb | C++17 | 3.3kb | 2023-10-25 04:48:32 | 2023-10-25 04:48:32 |
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(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, int 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 tl, int tr, int ks){
if(tl > tr) return 0;
if(root == nullptr){
while(true){
int x = 1;
}
}
if(root->cnt < ks) return 1e18;
if(l == tl && r == tr) return root->sum;
if(l == -1 || r == -1) assert(0);
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});
}
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);
}
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 = 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: 100
Accepted
time: 0ms
memory: 3620kb
input:
3 2 5 4 3 3 7
output:
7 11 16
result:
ok 3 lines
Test #2:
score: -100
Wrong Answer
time: 276ms
memory: 297764kb
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...
output:
350098164 542379891 815516336 1634271747 2281723841 2288848823 2295081837 2436938324 3378947668 3729968912 3888786337 4450922472 4529407199 4668579114 5217871609 5748677760 6741335309 7240106251 7727932570 8316657022 8904518029 9850303712 10096632581 10289352744 10829176078 10986147364 11431637691 1...
result:
wrong answer 1st lines differ - expected: '1318594', found: '350098164'