Submission #906284

# Submission time Handle Problem Language Result Execution time Memory
906284 2024-01-13T22:49:27 Z vjudge1 Rectangles (IOI19_rect) C++17
13 / 100
1195 ms 440912 KB
#include "rect.h"
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define F first
#define S second
#define pb push_back


long long count_rectangles(std::vector<std::vector<int> > a) {

#define int short

typedef pair<int, int> pii;
typedef vector<int> vi;

    int n = sz(a);
    int m = sz(a[0]);

    #define GET(a, item) lower_bound(all(a), pair<int, int>(item, -1))
    auto has = [&](const vector<pair<int, int>>& a, int item) {
        auto ptr = lower_bound(all(a), pair<int, int>(item, -1));
        return ptr != a.end() and ptr->first == item;
    };

    vector<vector<vector<pair<int,int>>>> rowf(n,vector<vector<pair<int,int>>>(m));

    rep(i,0,n){
        vector<pii> stack;
        rep(j,0,m){
            
            while(sz(stack) && stack.back().F < a[i][j]){
                if(stack.back().S+1 <= j-1) {
                    rowf[i][stack.back().S+1].emplace_back(j-1, -1);
                    // cout<<"push "<<(stack.back().S+1)<<" to "<<j-1<<endl;
                }
                stack.pop_back();
            }
            if(sz(stack) && stack.back().S+1 <= j-1) {
                // cout<<"push "<<(stack.back().S+1)<<" to "<<j-1<<endl;
                rowf[i][stack.back().S+1].emplace_back(j-1, -1);
            }
            while(sz(stack) && stack.back().F == a[i][j]) stack.pop_back();
            stack.pb({a[i][j],j});
        }
    }

    vector<vector<vector<pair<int,int>>>> colf(n,vector<vector<pair<int,int>>>(m));
    rep(j,0,m){
        vector<pii> stack;
        rep(i,0,n){
            while(sz(stack) && stack.back().F < a[i][j]){
                if(stack.back().S+1<=i-1) colf[stack.back().S+1][j].emplace_back(i-1, -1);
                stack.pop_back();
            }
            if(sz(stack) && stack.back().S+1<=i-1) colf[stack.back().S+1][j].emplace_back(i-1, -1);
            while(sz(stack) && stack.back().F == a[i][j]) stack.pop_back();
            stack.pb({a[i][j],i});
        }
    }


    
    rep(i,0,n)rep(j,0,m){
        for(auto p : rowf[i][j]){
            if(p.S == -1){
                int k = i;
                while(k<n && has(rowf[k][j], p.F)) k++;
                rep(l,i,k) {
                    GET(rowf[l][j], p.F)->second=k-1;
                }
            }
        }
        for(auto p : colf[i][j]){
            if(p.S == -1){
                int k = j;
                while(k<m && has(colf[i][k], p.F)) k++;
                rep(l,j,k) {
                    GET(colf[i][l], p.F)->second=k-1;
                }
            }
        }
    }

    // rep(i,0,n){
    //     rep(j,0,m){
    //         cout<<i<<' '<<j<<" rows:";
    //         for(pii x : rowf[i][j]) cout<<' '<<x.F<<","<<x.S;
    //         cout<<endl;
    //         cout<<i<<' '<<j<<" cols:";
    //         for(pii x : colf[i][j]) cout<<' '<<x.F<<","<<x.S;
    //         cout<<endl;
    //     }
    // }

    long long ans = 0;
    rep(i,0,n){
        rep(j,0,m){
            for(pii l : colf[i][j])for(pii r : rowf[i][j]){
                // [i, r.F], r.S tells how far we go on other
                // [j, l.F], l.S tells how far we go on other
                bool good = l.S>=r.F && r.S >= l.F;
                // cout<<i<<' '<<j<<" ("<<l.F<<','<<l.S<<") ("<<r.F<<','<<r.S<<") "<<good<<endl;
                ans += good;
            }
        }
    }

    return ans;
}

Compilation message

rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:18:21: warning: typedef 'vi' locally defined but not used [-Wunused-local-typedefs]
   18 | typedef vector<int> vi;
      |                     ^~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Incorrect 1 ms 344 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Incorrect 1 ms 344 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Incorrect 1 ms 344 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Incorrect 1 ms 344 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 860 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 520 ms 202088 KB Output is correct
3 Correct 1195 ms 438676 KB Output is correct
4 Correct 1098 ms 440756 KB Output is correct
5 Correct 1088 ms 440912 KB Output is correct
6 Correct 232 ms 169692 KB Output is correct
7 Correct 440 ms 322252 KB Output is correct
8 Correct 447 ms 343256 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Incorrect 1 ms 344 KB Output isn't correct
4 Halted 0 ms 0 KB -