QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#535327 | #7523. Partially Free Meal | Repeater | RE | 464ms | 423624kb | C++20 | 4.5kb | 2024-08-27 23:17:06 | 2024-08-27 23:17:08 |
Judging History
answer
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <map>
int n = 0;
vector<int> que;
map<long long, int> mp;
vector<long long> sum;
vector<pair<long long, long long>> ab;
int l = 1, r = 0;
struct Node {
int l, r, tl, tr, cnt;
long long sum;
Node() : l(-1), r(-1), tl(0), tr(0), cnt(0), sum(0) {}
};
vector<Node> node;
vector<int> root;
int cnt = 1;
void pushup(int idx) {
int idl = node[idx].tl, idr = node[idx].tr;
node[idx].sum = node[idl].sum + node[idr].sum;
node[idx].cnt = node[idl].cnt + node[idr].cnt;
return;
}
void build(int l, int r, int idx = 1) {
node[idx].l = l, node[idx].r = r;
return;
}
int update(int tl, int tr, int p, int idx) {
int cur = ++cnt;
node[cur].l = tl, node[cur].r = tr;
if (tl == tr && tl == p) {
node[cur].cnt = node[idx].cnt + 1;
node[cur].sum = node[idx].sum + sum[p - 1];
return cur;
}
if (p < tl || p > tr)
return cur;
int mid = (tl + tr) >> 1;
if (p <= mid) {
node[cur].tr = node[idx].tr;
node[cur].tl = update(tl, mid, p, node[idx].tl);
} else if (p > mid) {
node[cur].tl = node[idx].tl;
node[cur].tr = update(mid + 1, tr, p, node[idx].tr);
}
pushup(cur);
return cur;
}
int tans = 0, p = 0, tk = 0;
int query(int l, int r, int idx) {
if (idx == 0) {
p = r;
return -1;
}
if (node[idx].cnt + tans < tk) {
tans += node[idx].cnt;
p = r;
return -1;
}
if (l == r)
return 0;
int mid = (l + r) >> 1;
int res = 0;
if (l <= mid)
res = query(l, mid, node[idx].tl);
if (res == -1 && r > mid) {
query(mid + 1, r, node[idx].tr);
}
return 0;
}
long long getsum(int l, int r, int idx) {
if (idx == 0)
return 0;
int tl = node[idx].l, tr = node[idx].r;
if (l <= tl && tr <= r) {
return node[idx].sum;
}
if (l > tr || r < tl || l > r)
return 0;
long long ans = 0;
int mid = (tl + tr) >> 1;
if (l <= mid) {
ans += getsum(l, r, node[idx].tl);
}
if (r > mid) {
ans += getsum(l, r, node[idx].tr);
}
return ans;
}
vector<long long> ans;
int tot = 0;
void getans(int l, int r, int lk, int rk) {
// ++tot;
// if(tot >= 1000){
// return;
// }
if (lk + 1 >= rk)
return;
int idx = -1;
int k = (lk + rk) >> 1;
tk = k - 1;
for (int i = l; i <= r; ++i) {
if (que[i] < k)
continue;
int tot = 0;
tans = 0, p = 0;
query(1, sum.size(), root[que[i] - 1]);
// cout << tl << " " << "\n";
// cout << getsum(1, tl, root[que[i] - 1]) << " " << (k - 1 - query(1, tl, root[que[i] - 1])) * tr << "\n";
long long tsum = getsum(1, p, root[que[i] - 1]) + 1LL * (tk - tans) * sum[p] + ab[que[i]].first + ab[que[i]].second;
if (idx == -1) {
ans[k] = tsum;
idx = i;
} else if (ans[k] > tsum) {
ans[k] = tsum;
idx = i;
}
}
// cout << idx << "\n";
getans(l, idx, lk, k);
getans(idx, r, k, rk);
return;
}
void solve() {
cin >> n;
sum.resize(n), ab.resize(n + 1);
for (int i = 1; i <= n; ++i) {
long long ta = 0, tb = 0;
cin >> ta >> tb;
sum[i - 1] = ta;
ab[i].first = ta, ab[i].second = tb;
}
sort(sum.begin(), sum.end());
sum.erase(unique(sum.begin(), sum.end()), sum.end());
for (int i = 0; i < sum.size(); ++i) {
mp[sum[i]] = i + 1;
}
que.resize(n + 1);
sort(ab.begin(), ab.end(), [&](pair<long long, long long> a, pair<long long, long long> b) { return a.second < b.second; });
for (int i = 1; i <= n; ++i) {
while (l <= r && ab[i].first + ab[i].second <= ab[que[r]].first + ab[que[r]].second) --r;
que[++r] = i;
}
node.resize(sum.size() << 6);
build(1, sum.size());
root.resize(n + 1);
root[0] = 1;
for (int i = 1; i <= n; ++i) {
root[i] = update(1, sum.size(), mp[ab[i].first], root[i - 1]);
}
ans.resize(n + 1);
getans(l, r, 0, n + 1);
for (int i = 1; i <= n; ++i) cout << ans[i] << "\n";
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin >> t;
while (t--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3664kb
input:
3 2 5 4 3 3 7
output:
7 11 16
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 357ms
memory: 423468kb
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:
1318594 3208018 5570526 7340845 9223347 11149865 12332210 13476823 14788280 16172895 17768627 19336633 20693779 22005236 23389851 25073157 26760338 28509336 30294967 32093959 33976461 35893858 37754030 39588384 41470886 43388283 45309771 47309654 48837539 50417767 52079411 53762717 55190044 56577630...
result:
ok 200000 lines
Test #3:
score: 0
Accepted
time: 339ms
memory: 423624kb
input:
200000 847759212 808195187 499393947 158845704 286713001 829058634 347836790 268095042 525802369 342098012 260721485 611292083 95451146 853465743 666288241 921988396 958024432 522176958 284971271 952579505 408975858 657474613 444942472 596256250 291446883 538551864 942712385 142670067 239468458 9590...
output:
1398661 1990331 2912658 4186260 5510021 7421286 8925937 10233209 11427663 12466304 13739906 15048281 16438721 17917204 19556134 21034617 22945882 24964675 27046288 28988324 30624067 32102550 33629849 35258767 36683909 38129744 39608227 41135526 42764444 44559165 46466041 48377306 49860205 51250645 5...
result:
ok 200000 lines
Test #4:
score: 0
Accepted
time: 340ms
memory: 423532kb
input:
200000 453045091 809721921 853476741 595698118 218851090 887561745 864896419 760665890 291906050 833777661 706333418 479486544 207093005 36386646 969262171 137545723 501777026 470632513 238108337 821895650 757244578 277013047 715147479 651214680 995278884 359331973 20441197 903583173 61588145 378817...
output:
1629450 3451675 5862264 7342961 8475016 9707673 11350240 13021035 14824028 16466595 18145506 19755199 21397766 23219991 25058891 26869771 28383182 29992875 31635442 33457667 35296567 37269548 39205891 41028116 42867016 44839997 46268352 47600752 48988255 50421419 51854715 53368126 54897681 56507374 ...
result:
ok 200000 lines
Test #5:
score: 0
Accepted
time: 406ms
memory: 385368kb
input:
200000 724371 812164590 786885 853213841 375367 446887348 442587 266053048 910413 644441954 304737 493015342 449866 304716006 711453 152694644 405700 232509173 921335 223801102 979624 725734275 524990 894904581 975621 908452940 211492 159635302 996047 882815311 201533 598711233 152477 256633677 9411...
output:
112065 210146 299616 337074 391448 445884 500608 562757 637222 714160 812241 882389 944538 1019003 1095941 1179290 1276070 1329394 1383830 1438554 1500703 1564162 1638627 1715565 1798914 1886244 1975154 2064372 2159112 2230385 2293760 2357219 2421075 2486440 2560905 2636978 2713916 2797265 2883902 2...
result:
ok 200000 lines
Test #6:
score: 0
Accepted
time: 464ms
memory: 188784kb
input:
200000 82260 816973645 63190 30304976 15365 98123005 79313 698909248 88351 460688945 45258 145977968 35624 362819795 34062 929296068 13142 373585892 93631 33870837 47414 576582646 25940 764754575 8424 255583152 73559 151261046 51329 935294030 89414 108264540 14984 966447646 70866 477634636 7290 9827...
output:
13886 30185 60597 84857 106218 121551 137850 159549 187975 207878 228613 247795 267766 282648 297981 314280 332886 353621 374388 396087 418706 437819 458554 479321 499571 520306 541073 562772 582630 603365 624132 645831 668408 688579 709314 730081 751780 774399 798048 821818 847209 872827 901523 930...
result:
ok 200000 lines
Test #7:
score: -100
Runtime Error
input:
200000 1286 819121925 8997 273901462 8502 181755051 7515 175443695 5821 856272297 7390 111029219 5073 269357259 1581 35806553 7694 913461554 8300 307364045 8256 33038036 5700 229695181 250 919848697 7280 624783228 434 719961279 1232 128882963 8258 924021143 3834 843971163 5625 811204037 1492 2383695...