結果

問題 No.2574 Defect-free Rectangles
ユーザー momoyuumomoyuu
提出日時 2023-12-15 16:16:35
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 219 ms / 2,000 ms
コード長 1,387 bytes
コンパイル時間 1,116 ms
コンパイル使用メモリ 110,764 KB
実行使用メモリ 38,784 KB
最終ジャッジ日時 2023-12-15 16:16:38
合計ジャッジ時間 3,236 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 3 ms
6,676 KB
testcase_05 AC 3 ms
6,676 KB
testcase_06 AC 35 ms
7,424 KB
testcase_07 AC 18 ms
7,424 KB
testcase_08 AC 18 ms
7,424 KB
testcase_09 AC 212 ms
38,784 KB
testcase_10 AC 138 ms
38,784 KB
testcase_11 AC 213 ms
38,784 KB
testcase_12 AC 219 ms
38,784 KB
testcase_13 AC 88 ms
38,784 KB
testcase_14 AC 71 ms
14,592 KB
testcase_15 AC 42 ms
8,576 KB
testcase_16 AC 17 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;
using ll = long long;

#include<stack>
int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll ans = 0;
    ll h,w,n;
    cin>>h>>w>>n;
    vector<vector<int>> a(h,vector<int>(w+1,0));
    for(int i = 0;i<n;i++){
        int x,y;
        cin>>x>>y;
        x--;y--;
        a[x][y] = 1;
    }
    for(int i = 0;i<h;i++) a[i][w] = 1;
    vector<int> last(w+1,-1);
    for(int i = 0;i<h;i++){
        stack<pair<ll,ll>> st;
        for(int j = 0;j<=w;j++){
            if(a[i][j]) last[j] = i;
            int len = i - last[j];
            ll cnt = 0;
            while(st.size()){
                pair<ll,ll> now = st.top();
                if(now.first<=len) break;
                st.pop();
                ll can = now.first;
                ll mx = len;
                if(st.size()) mx = max(mx,st.top().first);
                ll use = can - mx;
                ans += use * (now.second) * (now.second+1) / 2;
                if(st.size()&&st.top().first>len){
                    st.top().second += now.second;
                    continue;
                }
                cnt += now.second;
            }
            cnt++;
            if(st.size()&&st.top().first==len) st.top().second += cnt;
            else st.push(make_pair(len,cnt));
        }
    }
    cout<<ans<<endl;
}
0