QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#307240 | #7105. Pixel Art | Minhho | AC ✓ | 326ms | 12700kb | C++17 | 6.3kb | 2024-01-18 11:04:44 | 2024-01-18 11:04:44 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define ps push
#define in insert
#define f first
#define s second
#define nl cout<<"\n"
#define ca(v) for(auto i:v) cout<<i<<" ";
#define cbit(x) __builtin_popcount(x)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (a*b/gcd(a, b))
const int xm[4] = {-1, 1, 0, 0};
const int ym[4] = {0, 0, -1, 1};
const int MOD = 1e9 + 7;
const int MAXN = 5e5 + 5;
const ll POW = 9973;
struct col {
int id;
int r1, r2, c;
friend bool operator<(col a, col b){
return a.r1 < b.r1;
}
};
struct row {
int id;
int c1, c2, r;
friend bool operator<(row a, row b){
return a.c1 < b.c1;
}
};
struct DSU {
vector<int> d;
void init(int N) { d = vector<int>(N, -1); }
int get(int x) { return d[x] < 0 ? x: d[x] = get(d[x]); }
bool same(int a, int b) { return get(a) == get(b); }
bool unite(int x, int y) {
x = get(x); y = get(y); if(x == y) return 0;
if (d[x] > d[y]) swap(x, y);
d[x] += d[y]; d[y] = x; return 1;
}
};
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
if (fopen("input.in", "r")) {
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
}
int t; cin>>t;
while (t--){
int n, m, k;
cin>>n>>m>>k;
DSU d;
d.init(k+1);
vector<col> cols; // row start, row end, col
vector<row> rows[n];
for(int i=0; i<n; i++) rows[i] = vector<row>();
for(int i=0; i<k; i++){
int r1, c1, r2, c2;
cin>>r1>>c1>>r2>>c2;
r1--; c1--; r2--; c2--;
if(r1 == r2){
row tmp = {i, c1, c2, r1};
rows[r1].pb(tmp);
} else {
col tmp = {i, r1, r2, c1};
cols.pb(tmp);
}
}
for(int i=0; i<n; i++) sort(all(rows[i]));
sort(all(cols));
map<int, col> colmap; // cur cols
map<int, col> endedcols; // prev cols;
vector<col> colending[n];
// vector<pair<int, col>> pvcols;
for(int i=0; i<n; i++) colending[i] = vector<col>();
int c = 0;
ll tot = 0;
int cmp = 0;
for(int i=0; i<n; i++){
// col on row
if(rows[i].size()){
for(auto p : endedcols){
col ec = p.s;
row fnd = {0, ec.c, ec.c, 0};
int it = upper_bound(all(rows[i]), fnd) - rows[i].begin() - 1;
if(it >= 0 && rows[i][it].c1 <= ec.c && rows[i][it].c2 >= ec.c){
if(d.unite(ec.id, rows[i][it].id)) cmp -= 1;
}
}
}
while(c < cols.size() && cols[c].r1 == i){
cmp += 1;
colmap[cols[c].c] = cols[c];
colending[cols[c].r2].pb(cols[c]);
// col by col
if(colmap.count(cols[c].c-1)){
if(d.unite(colmap[cols[c].c-1].id, cols[c].id)) cmp -= 1;
}
if(colmap.count(cols[c].c+1)){
if(d.unite(colmap[cols[c].c+1].id, cols[c].id)) cmp -= 1;
}
// col below row
if(i && rows[i-1].size()){
row fnd = {0,cols[c].c,cols[c].c,0};
int it = upper_bound(all(rows[i-1]), fnd) - rows[i-1].begin() - 1;
if(it >= 0 && rows[i-1][it].c1 <= cols[c].c && rows[i-1][it].c2 >= cols[c].c){
if(d.unite(cols[c].id, rows[i-1][it].id)) cmp -= 1;
}
}
// col on col;
if(endedcols.count(cols[c].c)){
if(d.unite(endedcols[cols[c].c].id, cols[c].id)) cmp -= 1;
}
c += 1;
}
for(int j=0; j<rows[i].size(); j++){
cmp += 1;
tot += (ll)(rows[i][j].c2 - rows[i][j].c1 + 1);
// col by row
if(colmap.count(rows[i][j].c1 - 1)) {
if(d.unite(rows[i][j].id, colmap[rows[i][j].c1 - 1].id)) cmp -= 1;
}
if(colmap.count(rows[i][j].c2 + 1)) {
if(d.unite(rows[i][j].id, colmap[rows[i][j].c2 + 1].id)) cmp -= 1;
}
// row by row
if(j) {
if(rows[i][j].c1 == rows[i][j-1].c2 + 1) {
if(d.unite(rows[i][j].id, rows[i][j-1].id)) cmp -= 1;
}
}
// row on row
if(i && rows[i-1].size()){
int L = rows[i-1].size();
int R = -1;
row fndl = {0, rows[i][j].c1, rows[i][j].c1, 0};
row fndr = {0, rows[i][j].c2, rows[i][j].c2, 0};
L = lower_bound(all(rows[i-1]), fndl) - rows[i-1].begin();
R = upper_bound(all(rows[i-1]), fndr) - rows[i-1].begin() - 1;
for(int pt = L-1; pt <= R+1; pt++){
if(pt < 0 || pt >= rows[i-1].size()) continue;
// assert(pt >= 0 && pt < rows[i-1].size());
if(rows[i-1][pt].c1 >= rows[i][j].c1 && rows[i-1][pt].c1 <= rows[i][j].c2){
if(d.unite(rows[i][j].id, rows[i-1][pt].id)) cmp -= 1;
}
else if(rows[i-1][pt].c2 >= rows[i][j].c1 && rows[i-1][pt].c2 <= rows[i][j].c2){
if(d.unite(rows[i][j].id, rows[i-1][pt].id)) cmp -= 1;
}
else if(rows[i-1][pt].c1 <= rows[i][j].c1 && rows[i-1][pt].c2 >= rows[i][j].c2){
if(d.unite(rows[i][j].id, rows[i-1][pt].id)) cmp -= 1;
}
}
}
}
tot += (ll)colmap.size();
endedcols.clear();
for(col ec : colending[i]){
endedcols[ec.c] = ec;
colmap.erase(ec.c);
}
cout<<tot<<" "<<cmp<<"\n";
}
skip:;
}
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3844kb
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: 0
Accepted
time: 326ms
memory: 12700kb
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:
ok 355756 lines
Extra Test:
score: 0
Extra Test Passed