QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#187834 | #7105. Pixel Art | UrgantTeam# | TL | 4ms | 10556kb | C++23 | 3.9kb | 2023-09-25 01:45:13 | 2023-09-25 01:45:13 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int const maxn = 1e5 + 5;
vector < pair < pair < int, int >, int > > in[maxn];
int p[maxn];
vector < pair < int, int > > adds[maxn], del[maxn];
ll b[maxn], c[maxn];
int get(int v) {
if (p[v] == v) return v;
p[v] = get(p[v]);
return p[v];
}
int add(int u, int v) {
u = p[u], v = p[v];
if (u == v) return 0;
p[u] = v;
return 1;
}
main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#endif // HOME
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n, m, k, r1, c1, r2, c2;
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) in[i] = {}, b[i] = 0, c[i] = 0;
for (int i = 1; i <= k; i++) p[i] = i;
for (int i = 1; i <= k; i++) {
cin >> r1 >> c1 >> r2 >> c2;
if (r1 == r2) {
b[r1] += (c2 - c1 + 1);
in[r1].push_back({{c1, c2}, i});
} else {
c[r1]++;
c[r2 + 1]--;
adds[r1].push_back({c1, i});
del[r2 + 1].push_back({c1, i});
}
}
int cmp = 0;
set < pair < pair < int, int >, int > > Q;
set < pair < int, int > > h;
ll curr = 0, add_all = 0;
for (int i = 1; i <= n; i++) {
cmp += in[i].size();
cmp += adds[i].size();
sort(in[i].begin(), in[i].end());
int l = 0;
vector < pair < pair < int, int >, int > > cur;
while (l < in[i].size()) {
int r = l, cur_r = in[i][l].first.second;
while (r < in[i].size() && in[i][r].first.first <= cur_r + 1) {
cmp -= add(in[i][l].second, in[i][r].second);
cur_r = max(cur_r, in[i][r].first.second);
cur.push_back({{in[i][l].first.first, cur_r}, in[i][l].second});
r++;
}
l = r;
}
in[i] = cur;
int l_ptr = 0, r_ptr = 0;
set < pair < int, int > > now_del;
for (auto key : del[i]) {
h.erase(key);
now_del.insert(key);
}
for (auto key : adds[i]) {
auto it = h.upper_bound({key.first + 1, -1});
if (it != h.end() && (*it).first == key.first + 1) {
cmp -= add(key.second, (*it).second);
}
if (it != h.begin()) {
it--;
if ((*it).first == key.first - 1) {
cmp -= add(key.second, (*it).second);
}
}
it = now_del.lower_bound({key.first, -1});
if (it != now_del.end() && (*it).first == key.first) {
cmp -= add(key.second, (*it).second);
}
h.insert(key);
}
for (auto key : in[i]) {
while (l_ptr < in[i - 1].size() && in[i - 1][l_ptr].first.second < key.first.first) l_ptr++;
while (r_ptr < in[i - 1].size() && in[i - 1][r_ptr].first.first <= key.first.second) r_ptr++;
for (int j = l_ptr; j < r_ptr; j++) cmp -= add(key.second, in[i - 1][j].second);
int L = key.first.first, R = key.first.second;
auto it = h.lower_bound({R + 1, -1});
if (it != h.end() && (*it).first == R + 1) cmp -= add(key.second, (*it).second);
it = h.lower_bound({L, -1});
if (it != h.begin()) {
it--;
if ((*it).first == L - 1) cmp -= add(key.second, (*it).second);
}
}
add_all += c[i];
curr += b[i] + add_all;
cout << curr << " " << cmp << '\n';
}
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 4ms
memory: 10556kb
input:
3 2 5 2 1 1 1 2 2 3 2 5 2 5 2 1 1 1 3 2 3 2 5 3 3 3 1 1 1 2 3 1 3 2 1 3 2 3
output:
2 1 5 2 3 1 6 1 3 1 4 1 6 2
result:
ok 7 lines
Test #2:
score: -100
Time Limit Exceeded
input:
2130 2 5 2 1 1 1 2 2 3 2 5 2 5 2 1 1 1 3 2 3 2 5 3 3 3 1 1 1 2 3 1 3 2 1 3 2 3 3 100 51 1 2 2 2 1 4 2 4 1 6 2 6 1 8 2 8 1 10 2 10 1 12 2 12 1 14 2 14 1 16 2 16 1 18 2 18 1 20 2 20 1 22 2 22 1 24 2 24 1 26 2 26 1 28 2 28 1 30 2 30 1 32 2 32 1 34 2 34 1 36 2 36 1 38 2 38 1 40 2 40 1 42 2 42 1 44 2 44 ...
output:
2 1 5 2 3 1 6 1 3 1 4 1 6 2 50 49 100 49 200 50 50 5 150 6 200 6 2 5 4 7 6 7 8 7 10 7 12 7 14 7 16 7 18 7 20 7 22 7 24 7 26 7 28 7 30 8 32 8 34 8 36 9 38 9 40 9 42 9 44 9 46 9 48 9 50 9 52 9 54 9 56 10 58 10 60 11 62 11 64 12 66 12 68 13 70 13 72 14 74 14 76 15 78 15 80 16 82 16 84 17 86 17 88 18 90...