QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#226534#7523. Partially Free MealPetroTarnavskyiTL 0ms5256kbC++172.5kb2023-10-26 02:48:552023-10-26 02:48:56

Judging History

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

  • [2023-10-26 02:48:56]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:5256kb
  • [2023-10-26 02:48:55]
  • 提交

answer

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
typedef tree<PII, null_type, less<PII>, rb_tree_tag,
			tree_order_statistics_node_update> ordered_set;

const int N = 200'447;
const LL INF = 1'000'000'447;
const LL LINF = INF * INF;

LL ans[N];
vector<pair<PII, int>> a;
vector<PII> b;
ordered_set s;

struct Fenwick
{
	int n;
	vector<LL> v;
	
	void init(int _n)
	{
		n = _n;
		v.assign(n, 0);
	}
	
	void add(int i, int x)
	{
		for (; i < n; i = (i + 1) | i)
			v[i] += x;
	}
	
	LL sum(int i)
	{
		LL res = 0;
		for (; i >= 0; i = (i & (i + 1)) - 1)
			res += v[i];
		return res;
	}
} fn;

LL f(int k)
{
	if (k == -1)
		return 0;
	auto it = s.find_by_order(k);
	if (it == s.end())
		return LINF;
	PII el = *it;
	int idx = lower_bound(ALL(a), MP(el, -1)) - a.begin();
	return fn.sum(a[idx].S);
}

void add(int i)
{
	int idx = lower_bound(ALL(a), MP(MP(b[i].F, i), -1)) - a.begin();
	s.insert(a[idx].F);
	fn.add(a[idx].S, a[idx].F.F);
}

void remove(int i)
{
	int idx = lower_bound(ALL(a), MP(MP(b[i].F, i), -1)) - a.begin();
	s.erase(a[idx].F);
	fn.add(a[idx].S, -a[idx].F.F);
}

void solve(int l, int r, int L, int R)
{
	if (l == r)
	{
		FOR (i, L, R)
			add(i);
		return;
	}
	if (l + 1 == r)
	{
		FOR (i, L, R)
		{
			ans[l] = min(ans[l], b[i].F + b[i].S + f(l - 1));
			add(i);
		}
		remove(R - 1);
		return;
	}
	int m = (l + r) / 2;
	int best = 0;
	FOR (i, L, R)
	{
		LL x = b[i].F + b[i].S + f(m - 1);
		if (x < ans[m])
		{
			best = i;
			ans[m] = x;
		}
		add(i);
	}
	FOR (i, L, R)
		remove(i);
	solve(l, m, L, best + 1);
	solve(m + 1, r, best, R);
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(15);
		
	int n;
	cin >> n;
	a.resize(n);
	b.resize(n);
	FOR (i, 0, n)
	{	
		cin >> b[i].F >> b[i].S;
	}
	sort(ALL(b), [](PII x, PII y)
	{
		return MP(x.S, x.F) < MP(y.S, y.F);
	});
	FOR (i, 0, n)
	{
		a[i].F.F = b[i].F;
		a[i].F.S = i;
	}
	sort(ALL(a));
	FOR (i, 0, n)
		a[i].S = i;
	fill(ans, ans + N, LINF);
	fn.init(n);
	
	solve(0, n, 0, n);
	
	FOR (i, 0, n)
		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: 5256kb

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: