Submission #18959209


Source Code Expand

Copy
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 200005;
int seg[4*N],h;
void update(int idx,int p=1,int s=0,int e=h)
{
    if(s==e)
    {
        seg[p] = 1;
        return;
    }
    int mid = (s+e)>>1;
    if(idx<=mid)
        update(idx,2*p,s,mid);
    else
        update(idx,2*p+1,mid+1,e);
    seg[p] = seg[2*p] + seg[2*p+1];
}
int get(int l,int r,int p=1,int s=0,int e=h)
{
    if(r<s||l>e)
        return 0;
    if(l<=s&&e<=r)
        return seg[p];
    int mid = (s+e)>>1;
    return get(l,r,p<<1,s,mid)+get(l,r,p<<1|1,mid+1,e);
}
void solve()
{
    int w, m;
    cin>>h>>w>>m;
    vector<pair<int,int>> v(m);
    for(int i=0;i<m;i++)
    {
        cin>>v[i].first>>v[i].second;
        v[i].first--;
        v[i].second--;
    }
    sort(v.begin(),v.end());

    vector<vector<int>> by_row(h), by_col(w);
    for (auto &it : v)
    {
        by_row[it.first].push_back(it.second);
        by_col[it.second].push_back(it.first);
    }
    for (int x = 0; x < h; x++)
        by_row[x].push_back(w);
    for (int y = 0; y < w; y++)
        by_col[y].push_back(h);

    int reachable_rows = by_col[0].front();
    int reachable_cols = by_row[0].front();

    ll ans = 0;
    for (int x = 0; x < reachable_rows; x++)
        ans += by_row[x].front();
    for (int y = 1; y < reachable_cols; y++)
    {
        int height = by_col[y].front();

        ans += max(0, height - reachable_rows);

        int x = min(reachable_rows, height);
        ans += get(0,x-1);

        for (int it : by_col[y])
            update(it);
    }
    cout<<ans<<'\n';
}
int main()
{
    ios_base::sync_with_stdio(false),cin.tie(0);
    solve();
    return 0;
}

Submission Info

Submission Time
Task F - Rook on Grid
User Khaled__Mahmoud
Language C++ (GCC 9.2.1)
Score 600
Code Size 1759 Byte
Status AC
Exec Time 195 ms
Memory 28760 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 29
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
hand_01.txt AC 67 ms 24368 KB
hand_02.txt AC 65 ms 24980 KB
hand_03.txt AC 21 ms 14144 KB
hand_04.txt AC 25 ms 14072 KB
hand_05.txt AC 2 ms 3512 KB
hand_06.txt AC 2 ms 3436 KB
random_01.txt AC 195 ms 28688 KB
random_02.txt AC 127 ms 28688 KB
random_03.txt AC 134 ms 28760 KB
random_04.txt AC 191 ms 28628 KB
random_05.txt AC 185 ms 28528 KB
random_06.txt AC 149 ms 28692 KB
random_07.txt AC 143 ms 28676 KB
random_08.txt AC 66 ms 26844 KB
random_09.txt AC 66 ms 26564 KB
random_10.txt AC 65 ms 26856 KB
random_11.txt AC 57 ms 6672 KB
random_12.txt AC 52 ms 6764 KB
random_13.txt AC 56 ms 6696 KB
random_14.txt AC 55 ms 6808 KB
random_15.txt AC 53 ms 6788 KB
random_16.txt AC 55 ms 6808 KB
random_17.txt AC 55 ms 6788 KB
random_18.txt AC 4 ms 3676 KB
random_19.txt AC 4 ms 3680 KB
random_20.txt AC 2 ms 3680 KB
sample_01.txt AC 2 ms 3568 KB
sample_02.txt AC 2 ms 3440 KB
sample_03.txt AC 68 ms 24920 KB