F2. Field Division (hard version)(离散化,*****)

https://codeforces.com/problemset/problem/1980/F2

解析:

范围很大,但有效的点很少,可以离散化处理,然后按照特殊的顺序进行处理,时间复杂度为

O(n),难点在于代码的。

#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <math.h>
#include <map>
#include <sstream>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <stdio.h>
#include <tuple>
using namespace std;
/*
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
*/
typedef long long LL;
#define int long long
#define ld long double
//#define INT __int128
const LL INF = 0x3f3f3f3f3f3f3f3f;
typedef unsigned long long ULL;
typedef pair<long long, long long> PLL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const ld eps = 1e-12;
const int N = 2e5 + 10, M = N + 10;
int n, m, k;

bool cmp(const PII& a, const PII& b) {
	if (a.first == b.first)return a.second < b.second;
	return a.first > b.first;
}
void solve() {
	cin >> n >> m >> k;
	vector<pair<int,int>>A(k);
	map<pair<int, int>, int>mp;
	for (int i = 0; i < k; i++) {
		cin >> A[i].first >> A[i].second;
		mp[A[i]] = i;
	}
	A.push_back({ 0,0 });
	mp[{0, 0}] = k++;
	

	sort(A.begin(), A.end(), cmp);
	vector<int>sum(k+1), y(k + 1, m + 1), x(k + 1, n);
	vector<int>ans(k, 0);
	//cout << "______________________" << endl;
	for (int i = 1; i <= k; i++) {
		auto t = A[i - 1];
		y[i] = y[i - 1];
		x[i] = x[i - 1];
		sum[i] = sum[i - 1];
		if (t.second < y[i]) {
			ans[mp[t]] = 1;
			sum[i] += (y[i] - 1) * (x[i] - t.first);
			y[i] = t.second;
			x[i] = t.first;
		}
	}
	//cout << "_________" << endl;
	cout << sum[k] << endl;
	for (int i = 1; i <= k; i++) {
		auto t = A[i - 1];
		if (ans[mp[t]] == 0)continue;
		int xx = x[i - 1];
		int yy = y[i - 1];
		int s = sum[i - 1];
		for (int j = i + 1; j <= k; j++) {
			auto tt = A[j - 1];
			if (yy > tt.second) {
				s += (yy - 1) * (xx - tt.first);
				yy = tt.second;
				xx = tt.first;
			}
			if (ans[mp[tt]] == 1) {
				ans[mp[t]] = s - sum[j];
				break;
			}
		}
	}
	for (int i = 0; i < k-1; i++) {
		cout << ans[i] << " ";
	}
	cout << endl;
}
signed main() {
	ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
	int T;
	cin >> T;
	while (T--) {
		solve();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值