QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#467178#7523. Partially Free Mealfairyqq28TL 1ms5920kbC++141.7kb2024-07-08 14:54:002024-07-08 14:54:02

Judging History

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

  • [2024-07-08 14:54:02]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:5920kb
  • [2024-07-08 14:54:00]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cassert>
#define mid ((l+r)>>1)
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
using namespace std;
const int N = 200010, INF = 1e9;
int tot;
struct node {int ls, rs, cnt, sum;} t[N * 35];
#define ls t[u].ls
#define rs t[u].rs
void up(int u){
    t[u].cnt = t[ls].cnt + t[rs].cnt;
    t[u].sum = t[ls].sum + t[rs].sum;
}
void update(int &u, int l, int r, int x, int val){
    t[++tot] = t[u], u = tot;
    if(l == r) {t[u].cnt++, t[u].sum += l; return;}
    if(x <= mid) update(ls, l, mid, x, val);
    else update(rs, mid + 1, r, x, val);
    up(u);
}
int query(int u, int l, int r, int k){
    if(!u) return 0;
    assert(k <= t[u].cnt);
    if(l == r) return k * l;
    if(k <= t[ls].cnt) return query(ls, l, mid, k);
    return query(ls, l, mid, t[ls].cnt) + query(rs, mid + 1, r, k - t[ls].cnt);
}

int n, rt[N], ans[N];
pair<int, int> a[N];

#undef mid
void solve(int l, int r, int L, int R){
    if(l > r) return;
    int mid = (l+r)>>1, p = R;
    ans[mid] = query(rt[p-1], 1, INF, mid - 1) + a[p].first + a[p].second;
    rep(i, max(mid, L), R-1){
        int t = query(rt[i-1], 1, INF, mid - 1) + a[i].first + a[i].second;
        if(t < ans[mid]) ans[mid] = t, p = i;
    }
    solve(l, mid-1, L, p), solve(mid+1, r, p, R);
}

int main(){
    scanf("%d", &n);
    rep(i, 1, n) scanf("%d%d", &a[i].second, &a[i].first);
    sort(a + 1, a + n + 1);
    rep(i, 1, n) rt[i] = rt[i-1], update(rt[i], 1, INF, a[i].second, 1);
    solve(1, n, 1, n);
    rep(i, 1, n) printf("%d\n", ans[i]);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: