QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#226547#7523. Partially Free MealPetroTarnavskyiWA 1676ms11776kbC++172.8kb2023-10-26 03:11:352023-10-26 03:11:36

Judging History

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

  • [2023-10-26 03:11:36]
  • 评测
  • 测评结果:WA
  • 用时:1676ms
  • 内存:11776kb
  • [2023-10-26 03:11:35]
  • 提交

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;
int cnt = 0;

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;
	}
	
	int lower_bound(LL x)
	{
		LL sum = 0;
		int i = -1;
		int lg = 31 - __builtin_clz(n);
		while (lg >= 0)
		{
			int j = i + (1 << lg);
			if (j < n && sum + v[j] < x)
			{
				sum += v[j];
				i = j;
			}
			lg--;
		}
		return i + 1;
	}
} fn, s;

LL f(int k)
{
	if (k == -1)
		return 0;
	if (k >= cnt)
		return LINF;
	int idx = s.lower_bound(k + 1);
	return fn.sum(idx);
}

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

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

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);
	s.init(n);
	
	solve(0, n, 0, n);
	
	FOR (i, 0, n)
		cout << ans[i] << '\n';
	
	cerr << double(clock()) / CLOCKS_PER_SEC << '\n';
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
2 5
4 3
3 7

output:

7
11
16

result:

ok 3 lines

Test #2:

score: -100
Wrong Answer
time: 1676ms
memory: 11776kb

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
12496161
13640774
14952231
15116182
16500797
20693779
18987207
20130710
21275323
21528965
22840422
25654701
24478679
26161985
27849166
28102808
29851806
41470886
33655926
35343107
37092105
38672333
40457964
42511082
43837184
45671538
47554040...

result:

wrong answer 8th lines differ - expected: '13476823', found: '12496161'