QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#303904#7105. Pixel Artkevinshan#RE 0ms3524kbC++176.1kb2024-01-13 05:55:422024-01-13 05:55:42

Judging History

你现在查看的是最新测评结果

  • [2024-01-13 05:55:42]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3524kb
  • [2024-01-13 05:55:42]
  • 提交

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(n+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
            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 += 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();
                    if(L && rows[i-1][L].c2 >= rows[i][j].c1) L -= 1;
                    R = upper_bound(all(rows[i-1]), fndr) - rows[i-1].begin() - 1;
                    if(L < 0 || L >= rows[i-1].size() || R < 0 || R >= rows[i-1].size()) continue;
                    for(int pt = L; pt <= R; pt++){
                        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;
                        }
                    }
                }
            }

            tot += colmap.size();

            endedcols.clear();
            for(col ec : colending[i]){
                endedcols[ec.c] = ec;
                colmap.erase(ec.c);
            }
            cout<<tot<<" "<<cmp<<"\n";
        }
        skip:;
    }
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3524kb

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
Runtime Error

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:


result: