제출 #654168

#제출 시각아이디문제언어결과실행 시간메모리
654168jiahngRectangles (IOI19_rect)C++14
0 / 100
10 ms1488 KiB
#include "rect.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> pi;
typedef vector <int> vi;
typedef vector <pi> vpi;
typedef pair<pi, ll> pii;
typedef pair <pi,pi> pipi;
typedef set <ll> si;
typedef long double ld;
#define f first
#define s second
#define mp make_pair
#define FOR(i,s,e) for(int i=s;i<=int(e);++i)
#define DEC(i,s,e) for(int i=s;i>=int(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define maxn 2501
#define INF (ll)1e9
#define MOD 1000000007
typedef pair <vi, int> pvi;
typedef pair <int,pi> ipi;
typedef vector <pii> vpii;

int N,M;
set <pi> hpairs[maxn], vpairs[maxn];
bool intersect(pi x, pi y){
    return max(x.f, y.f) <= min(x.s, y.s);
}
struct node{
    int s,e,m,num=0;
    unordered_map <int,int> fw;
    node *l,*r;
    node(int ss,int ee){
        s = ss; e = ee; m = (s + e) / 2;
        if (s != e){
            l = new node(s,m); r = new node(m+1,e);
        }
    }

    void insert(int p, int v){
        num++;
        for (int i = v+1; i <= M; i += i & (-i)) fw[i]++;
        if (s == e) return;
        if (p > m) r->insert(p,v);
        else l->insert(p,v);
    }
    int qry(int a,int b,int c){ // Number in [a,b] >= c
        if (a <= s && e <= b){
            int ans = 0;
            for (int i = c; i > 0; i -= i & (-i)) ans += fw[i];
            return num - ans; 
        }else if (a > e || s > b) return 0;
        else return l->qry(a,b,c) + r->qry(a,b,c);
    }
};
vector <pii> B[maxn];
vi C[maxn],D[maxn];
long long count_rectangles(std::vector<std::vector<int> > a) {
    N = 5, M = 6;
    N = a.size(); M = a[0].size();

    FOR(i,0,N-1){
        stack <pi> st;
        FOR(j,0,M-1){
            while (!st.empty() && st.top().f < a[i][j]) st.pop();
            if (!st.empty() && j - st.top().s + 1 >= 3) hpairs[i].insert(pi(st.top().s+1, j-1)); 
            st.push(pi(a[i][j], j));
        }
        while (!st.empty()) st.pop();
        DEC(j,M-1,0){
            while (!st.empty() && st.top().f < a[i][j]) st.pop();
            if (!st.empty() && st.top().s - j + 1 >= 3) hpairs[i].insert(pi(j+1, st.top().s-1));
            st.push(pi(a[i][j], j));
        }
    }

    FOR(j,0,M-1){
        stack <pi> st;
        FOR(i,0,N-1){
            while (!st.empty() && st.top().f < a[i][j]) st.pop();
            if (!st.empty() && i - st.top().s + 1 >= 3) vpairs[j].insert(pi(st.top().s+1, i-1)); 
            st.push(pi(a[i][j], i));
        }
        while (!st.empty()) st.pop();
        DEC(i,N-1,0){
            while (!st.empty() && st.top().f < a[i][j]) st.pop();
            if (!st.empty() && st.top().s - i + 1 >= 3) vpairs[j].insert(pi(i+1, st.top().s-1));
            st.push(pi(a[i][j], i));
        }
    }

    /*
    cout << "HPAIRS: \n";
    FOR(i,0,N-1){
        cout << "ROW " << i << '\n';
        aFOR(j, hpairs[i]) cout << j.f << ' ' << j.s << '\n';
    }
    cout << "VPAIRS: \n";
    FOR(i,0,M-1){
        cout << "COL " << i << '\n';
        aFOR(j, vpairs[i]) cout << j.f << ' ' << j.s << '\n';
    }
    */

    vector <pipi> hrects, vrects;
    FOR(i,0,N-1){
        aFOR(j, hpairs[i]){
            int a = i, b = j.f, c = N - 1, d = j.s;
            FOR(k,i+1,N-1){
                if (hpairs[k].count(j)){
                    hpairs[k].erase(j);
                }else{
                    c = k - 1;
                    break;
                }
            }
            hrects.pb(pipi(pi(a,b), pi(c,d)));
        }
    }

    FOR(i,0,M-1){
        aFOR(j, vpairs[i]){
            int a = j.f, b = i, c = j.s, d = M - 1;
            FOR(k,i+1,M-1){
                if (vpairs[k].count(j)){
                    vpairs[k].erase(j);
                }else{
                    d = k - 1;
                    break;
                }
            }
            vrects.pb(pipi(pi(a,b), pi(c,d)));
        }
    }
    /*
    cout << "HRECTS: \n";
    aFOR(i, hrects) cout << i.f.f << ' ' << i.f.s << ' ' << i.s.f << ' ' << i.s.s << '\n';
    cout << "VRECTS: \n";
    aFOR(i, vrects) cout << i.f.f << ' ' << i.f.s << ' ' << i.s.f << ' ' << i.s.s << '\n';
    */

    int ans = 0;
    aFOR(i, vrects){
        if (i.f.s > 0) B[i.f.s-1].pb(pii(pi(i.f.f, i.s.f), -1));
        B[i.s.s].pb(pii(pi(i.f.f, i.s.f), 1));
    }
    FOR(i,0,hrects.size() - 1){
        C[hrects[i].s.s].pb(i);
    }
    FOR(i,0,hrects.size() - 1){
        D[hrects[i].f.s].pb(i);
    }
    node *seg = new node(0,N-1), *seg2 = new node(0,N-1);
    FOR(i,0,M-1){
        aFOR(j,C[i]){
            seg->insert(hrects[j].f.f, hrects[j].s.f);
        }
        aFOR(j, D[i]){
            seg2->insert(hrects[j].f.f, hrects[j].s.f);
        }
        aFOR(j, B[i]){
            if (j.s == 1) ans += seg->qry(0, j.f.f, j.f.s);
            else ans -= seg2->qry(0,j.f.f,j.f.s);
        }
    }
    return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...