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;
}