Submission #207916

#TimeUsernameProblemLanguageResultExecution timeMemory
207916balbitRectangles (IOI19_rect)C++14
72 / 100
5134 ms1032920 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define ull unsigned ll
#define f first
#define s second
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define SQ(x) (x)*(x)
#define MN(a,b) a = min(a,(__typeof__(a))(b))
#define MX(a,b) a = max(a,(__typeof__(a))(b))
#define pb push_back
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#ifdef BALBIT
#define IOS()
#define bug(...) fprintf(stderr,"#%d (%s) = ",__LINE__,#__VA_ARGS__),_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define bug(...)
#endif

const int iinf = 1<<29;
const ll inf = 1ll<<60;
const ll mod = 1e9+7;


void GG(){cout<<"-1\n"; exit(0);}

ll mpow(ll a, ll n, ll mo = mod){ // a^n % mod
    ll re=1;
    while (n>0){
        if (n&1) re = re*a %mo;
        a = a*a %mo;
        n>>=1;
    }
    return re;
}

ll inv (ll b, ll mo = mod){
    if (b==1) return b;
    return (mo-mo/b) * inv(mo%b) % mo;
}

const int MX = 2505;

vector<pii> hof[MX][MX], cof[MX][MX];
#define REP(i,n) for (int i = 0; i<n; i++)
#define FOR(i,a,b) for (int i = a; i<b; i++)

vector<int> g[MX][MX];

void go(vector<pii> up[MX][MX], vector<vector<int> > &a) {
    int n = a.size(), m = a[0].size();
    REP(i,m) REP(j,m) g[i][j] . clear();
    REP(i,n) {
        vector<int> &b = a[i];
        stack<pii> how; how.push({b[0], 0});
        FOR(j,1,m) {
            while (!how.empty() && how.top().f < b[j]) {
                g[how.top().s][j].pb(i);
                how.pop();
            }
            if (!how.empty()) {
                g[how.top().s][j].pb(i);
            }
            while (!how.empty() && how.top().f == b[j]) how.pop();
            how.push({b[j],j});
        }
    }
    REP(i,m-1) FOR(j,i+2,m) {
        if (SZ(g[i][j]) == 0) continue;
        sort(ALL(g[i][j]));
        int first = 0;
        REP(it, SZ(g[i][j]) + 1) {
            if (it == SZ(g[i][j]) || (it!=0 && g[i][j][it] != g[i][j][it-1]+1)) {
                FOR(nat, first, it) if (g[i][j][nat] -1 >= 0) up[max(g[i][j][nat]-1,0)][i].pb({g[i][j][it-1]+1,j});
                first = it;
            }
        }
    }
}

int s[MX];
ll QU(int e) {
    ll re = 0;
    for (e++; e>0; e-=e&-e) re += s[e];
    return re;
}
void MO(int e, int v) {
    for (e++; e<MX; e+=e&-e) s[e] += v;
}

ll count_rectangles(vector<vector<int> > a){
    go(hof,a);
    int n = a.size(), m = a[0].size();
#ifdef BALBIT
    REP(i,n) REP(j,m) {
        if (SZ(hof[i][j]) == 0) continue;
        bug(i,j);
        for (pii x : hof[i][j]) {
            cerr<<x.f<<' '<<x.s<<"\n";
        }
        cerr<<endl;
    }
#endif
    vector<vector<int> > aT(m);
    REP(j,m) {
        REP(i,n) {
            aT[j].pb(a[i][j]);
        }
    }
    go(cof, aT);
    REP(i,n) REP(j,m) {
        for (pii & x : cof[j][i]) {
            swap(x.f, x.s);
        }
    }
#ifdef BALBIT
    REP(i,n) REP(j,m) {
        if (SZ(cof[i][j]) == 0) continue;
        bug(i,j, j,i);
        for (pii x : cof[j][i]) {
            cerr<<x.f<<' '<<x.s<<"\n";
        }
        cerr<<endl;
    }
#endif
    ll re = 0;
    REP(i,n-2) REP(j,m-2) {
        vector<pii> ud = hof[i][j], lr = cof[j][i];
        if (ud.size() == 0 || lr.size() == 0) continue;
        sort(ALL(ud), greater<pii> ());
        sort(ALL(lr), greater<pii> ());
        int it = 0;
        for (pii & q : lr) {
            while (it < SZ(ud) && ud[it].f >= q.f) {
                MO(ud[it].s,1);
                ++it;
            }
            re += QU(q.s);
            bug(q.s,it,QU(q.s));
        }
        REP(jt, it) {
            MO(ud[jt].s,-1);
        }
    }
    return re;
}

#ifdef BALBIT
signed main(){
    IOS();
//    ll lol = count_rectangles({{0,5,5,0},{5,2,2,5},{5,3,4,5}});
//    bug(lol);
    ll lol = count_rectangles({{4, 8, 7, 5, 6},
{7, 4, 10, 3, 5},
{9, 7, 20, 14, 2},
{9, 14, 7, 3, 6},
{5, 7, 5, 2, 7},
{4, 5, 13, 5, 6}});
    bug(lol);
}
#endif
#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...