Submission #165106

# Submission time Handle Problem Language Result Execution time Memory
165106 2019-11-25T07:52:43 Z egekabas Bob (COCI14_bob) C++14
120 / 120
182 ms 18072 KB
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long   ll;
typedef unsigned long long   ull;
typedef long double ld;
typedef pair<ll, ll>    pll;
typedef pair<ull, ull>    pull;
typedef pair<ll, ll>  pii;
typedef pair<ld, ld>  pld;
int a[1009][1009];
int down[1009][1009];
ll ans;
stack<pii> s;
int n, m;
ll ctwo(ll x){
    return x*(x-1)/2 + x;
}
void spop(int idx, int line, int cur){
    pii tmp = s.top();

    s.pop();
    if(s.empty()){
        ans += ctwo(idx-tmp.ss)*(down[line][tmp.ff]-cur);
    }
    else{
        ans += ctwo(idx-tmp.ss)*(down[line][tmp.ff]- max(cur, down[line][s.top().ff]));

    }
}
int main() {
    
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= m; ++j)
            scanf("%d", &a[i][j]);

    for(int i = n; i > 0; --i)
        for(int j = m; j > 0; --j){
            if(i == n || a[i][j] != a[i+1][j])
                down[i][j] = 1;
            else
                down[i][j] = down[i+1][j]+1;
        }

    int beg = -1;
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= m; ++j){
            if(a[i][j] != a[i][j-1]){
                while(!s.empty())
                    spop(j, i, 0);
                beg = j;
            }
            while(!s.empty() && down[i][s.top().ff] > down[i][j])
                spop(j, i, down[i][j]);
            if(!s.empty() && down[i][s.top().ff] == down[i][j]){
                int t1 = s.top().ss;
                s.pop();
                s.push({j, t1});
                continue;
            }
            if(!s.empty())
                s.push({j, s.top().ff+1});
            else
                s.push({j, beg});
        }
        while(!s.empty())
            spop(m+1, i, 0);
    }
    cout << ans << "\n";
    
}

Compilation message

bob.cpp: In function 'int main()':
bob.cpp:39:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &m);
     ~~~~~^~~~~~~~~~~~~~~~
bob.cpp:42:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d", &a[i][j]);
             ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 4856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 5388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 5496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 180 ms 15004 KB Output is correct
2 Correct 115 ms 10124 KB Output is correct
3 Correct 114 ms 10104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 182 ms 18048 KB Output is correct
2 Correct 121 ms 10336 KB Output is correct
3 Correct 114 ms 10176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 176 ms 17800 KB Output is correct
2 Correct 114 ms 10232 KB Output is correct
3 Correct 115 ms 10160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 178 ms 18072 KB Output is correct
2 Correct 115 ms 10104 KB Output is correct
3 Correct 118 ms 10104 KB Output is correct