QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#208627#7523. Partially Free MealSolitaryDream#TL 1ms8040kbC++171.6kb2023-10-09 19:27:452023-10-09 19:27:45

Judging History

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

  • [2023-10-09 19:27:45]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:8040kb
  • [2023-10-09 19:27:45]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
const int N = 2e5 + 10;
namespace Seg {
    const int S = N * 32;
    int si[S], sm[S], c[S][2], size;
    inline void Add(int &v, int x, int l = 1, int r = 1e9) {
        int nv = ++size;
        si[nv] = si[v]; sm[nv] = sm[v]; 
        c[nv][0] = c[v][0]; c[nv][1] = c[v][1];
        v = nv;
        ++si[v]; sm[v] += x;
        if (l == r) return;
        int mid = (l + r) >> 1;
        if (x <= mid) Add(c[v][0], x, l, mid); else Add(c[v][1], x, mid + 1, r);
    }
    inline int Ask(int v, int k) {
        if (si[v] == k) return sm[v];
        if (si[c[v][0]] >= k) return Ask(c[v][0], k);
        else return sm[c[v][0]] + Ask(c[v][1], k - si[c[v][0]]);
    }
}
int n, ans[N];
pii a[N];
int rt[N];
inline void Solve(int l, int r, int bl, int br) {
    int v = (l + r) >> 1;
    int minf = 1e15, mini = -1;
    for (int i = max(v, bl); i <= br; ++i) {
        int curf = a[i].first + Seg::Ask(rt[i], v);
        if (curf < minf) minf = curf, mini = i;
    }
    // printf("%lld %lld %lld\n", v, a[mini].first, minf);
    ans[v] = minf;
    if (l < v) Solve(l, v - 1, bl, mini); 
    if (v < r) Solve(v + 1, r , mini, br);
}
signed main() {
    scanf("%lld", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%lld%lld", &a[i].second, &a[i].first);
    }
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; ++i) Seg::Add(rt[i] = rt[i - 1], a[i].second);
    Solve(1, n, 1, n);
    for (int i = 1; i <= n; ++i) printf("%lld\n", ans[i]);
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 8040kb

input:

3
2 5
4 3
3 7

output:

7
11
16

result:

ok 3 lines

Test #2:

score: -100
Time Limit Exceeded

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:


result: