QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#588074 | #7105. Pixel Art | xuanbac05 | WA | 579ms | 34536kb | C++17 | 5.7kb | 2024-09-25 00:32:01 | 2024-09-25 00:32:01 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()
using namespace std;
const int mxN = 5e5 + 227;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
struct DSU{
int n;
int ok;
vector<int> lab;
DSU(){}
DSU(int _n) {
n = _n;
ok = 0;
lab.resize(n + 7, -1);
}
int root(int u) {
return lab[u] < 0 ? u : lab[u] = root(lab[u]);
}
bool union_root(int u, int v) {
u = root(u);
v = root(v);
if(u == v) return false;
if(lab[u] > lab[v]) swap(u, v);
lab[u] += lab[v];
lab[v] = u;
++ok;
return true;
}
int getComp() {
return ok;
}
} dsu;
struct SegmentRow {
int l, r, idx;
bool operator<(const SegmentRow &other) const {
if(l == other.l && r == other.r) return idx < other.idx;
if(l == other.l) return r < other.r;
return l < other.l;
}
};
struct SegmentCol {
int pos, op, idx;
bool operator<(const SegmentCol &other) const {
return op < other.op;
}
};
int n;
int m;
int k;
vector<SegmentCol> orderCol[mxN];
vector<SegmentRow> orderRow[mxN];
bool isIntersect(int l1, int r1, int l2, int r2) {
int maxl = max(l1, l2);
int minr = min(r1, r2);
return maxl <= minr;
}
void solve()
{
cin >> n >> m >> k;
dsu = DSU(k);
multiset<pair<int, int>> col;
multiset<SegmentRow> row;
multiset<SegmentRow> preRow;
for(int i = 1; i <= k; i++) {
int r1, c1, r2, c2;
cin >> r1 >> c1 >> r2 >> c2;
if(r1 == r2) { // row
orderRow[r1].pb({c1, c2, i});
}
else { // col
orderCol[r1].pb({c1, -1, i});
orderCol[r2 + 1].pb({c1, 1, i});
}
}
ll res = 0;
int comp = 0;
for(int i = 1; i <= n; i++) {
row.clear();
sort(all(orderRow[i]));
sort(all(orderCol[i]));
multiset<pair<int, int>> preCol;
for(auto it : orderCol[i]) {
if(it.op == 1) {
col.erase(col.find(make_pair(it.pos, it.idx)));
preCol.insert(make_pair(it.pos, it.idx));
}
}
// col col
for(auto it : orderCol[i]) {
if(it.op == -1) {
++comp;
auto jt = col.lower_bound(make_pair(it.pos - 1, -1));
if(jt != col.end()) {
if(it.pos - 1 <= (*jt).fi && (*jt).fi <= it.pos + 1) {
dsu.union_root(it.idx, (*jt).se);
++jt;
if(jt != col.end()) {
if(it.pos - 1 <= (*jt).fi && (*jt).fi <= it.pos + 1) {
dsu.union_root(it.idx, (*jt).se);
}
}
}
}
jt = preCol.lower_bound(make_pair(it.pos, -1));
if(jt != col.end() && (*jt).fi == it.pos) {
dsu.union_root(it.idx, (*jt).se);
}
col.insert(make_pair(it.pos, it.idx));
}
}
for(auto it : orderRow[i]) {
res += it.r - it.l + 1;
row.insert(it);
++comp;
}
res += sz(col);
// common row
auto it = row.begin();
while(it != row.end()) {
auto jt = it; ++jt;
if(jt != row.end()) {
if((*it).r + 1 == (*jt).l) {
dsu.union_root((*it).idx, (*jt).idx);
}
}
++it;
}
// row col
it = row.begin();
while(it != row.end()) {
auto jt = col.lower_bound(make_pair((*it).l - 1, -1));
while(jt != col.end() && (*jt).fi <= (*it).r + 1) {
dsu.union_root((*it).idx, (*jt).se);
++jt;
}
++it;
}
it = row.begin();
while(it != row.end()) {
auto jt = preCol.lower_bound(make_pair((*it).l, -1));
while(jt != preCol.end() && (*jt).fi <= (*it).r) {
dsu.union_root((*it).idx, (*jt).se);
++jt;
}
++it;
}
// previous row current row
it = row.begin();
auto jt = preRow.begin();
while(it != row.end() && jt != preRow.end()) {
if(isIntersect((*it).l, (*it).r, (*jt).l, (*jt).r) == true) {
dsu.union_root((*it).idx, (*jt).idx);
}
if((*it).r <= (*jt).r) ++it;
else ++jt;
}
swap(preRow, row);
row.clear();
cout << res << ' ' << comp - dsu.getComp() << endl;
}
for(int i = 1; i <= max(n, m) + 1; i++) {
orderCol[i].clear();
orderRow[i].clear();
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// freopen(TASK".inp" , "r" , stdin);
// freopen(TASK".out" , "w" , stdout);
int tc = 1;
cin >> tc;
while(tc--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 8ms
memory: 26944kb
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
Wrong Answer
time: 579ms
memory: 34536kb
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 50 100 50 200 1 50 50 150 1 200 1 2 1 4 1 6 1 8 1 10 1 12 1 14 1 16 1 18 1 20 1 22 1 24 1 26 1 28 1 30 1 32 1 34 1 36 1 38 1 40 1 42 1 44 1 46 1 48 1 50 1 52 1 54 1 56 1 58 1 60 1 62 1 64 1 66 1 68 1 70 1 72 1 74 1 76 1 78 1 80 1 82 1 84 1 86 1 88 1 90 1 92 1 94 1 96 1...
result:
wrong answer 10234th lines differ - expected: '63 5', found: '63 6'