Submission #238388

# Submission time Handle Problem Language Result Execution time Memory
238388 2020-06-11T00:22:49 Z ant101 Bob (COCI14_bob) C++14
120 / 120
193 ms 25860 KB
#include <iostream>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <stack>
#include <queue>
#include <assert.h>
#include <limits>
#include <cstdio>
using namespace std;

//#define RDEBUG 1
#ifdef RDEBUG
#define D(x) x
#else
#define D(x)
#endif
#define inf 0x7fffffff
#define MOD 1000000007

typedef long long ll;


ll add(ll a, ll b) {
    a += b;
    if(a >= MOD) {
        a -= MOD;
    }
    return a;
}
ll sub(ll a, ll b) {
    a -= b;
    if(a < 0) {
        a += MOD;
    }
    return a;
}

ll mul(ll a, ll b) {
    return (a * b)%MOD;
}

void add_self(ll& a, ll b) {
    a = add(a, b);
}
void sub_self(ll& a, ll b) {
    a = sub(a, b);
}
void mul_self(ll& a, ll b) {
    a = mul(a, b);
}


const ll MAXN = 200010;


int main() {
    int n , m ;
    scanf("%d %d" , &n , &m) ;
    int arr[n+2][m+2] , now[n+2][m+2];
    for(int i = 1 ; i <= n ; ++i) {
        for(int j = 1 ; j <= m ; ++j)
            scanf("%d" , &arr[i][j]) ;
    }
    //prepare now for every element
    for(int i = 1 ; i <= n ; ++i) {
        now[i][m] = 1ll ;
        for(int j = m-1 ; j >= 1 ; --j) {
            if(arr[i][j] == arr[i][j+1])
                now[i][j] = now[i][j+1] + 1ll;
            else
                now[i][j] = 1ll;
        }
    }
    long long ans = 0ll ;
    long long sol[n+2][m+2] ;
    //loop on every column
    for(int j = 1 ; j <= m ; ++j) {
        arr[0][j] = -1 ;
        stack< pair<int , int> >s ;
        for(int i = 1 ; i <= n ; ++i) {
            pair<int , int>cur ;
            cur.first = i , cur.second = 1ll ;
            sol[i][j] = 0ll ;
            //new value
            if(arr[i][j] != arr[i-1][j]) {
                while(!s.empty())
                    s.pop() ;
                sol[i][j] = now[i][j] * 1ll;
                ans += sol[i][j] ;
                s.push(cur) ;
                continue ;
            }
            //loop in stack
            while (!s.empty() && now[s.top().first][j] >= now[i][j]) {
                pair<int , int>p = s.top() ;
                s.pop();
                cur.second += p.second ;
            }
            sol[i][j] = (cur.second * 1ll * now[i][j]) ;
            if (s.size() > 0) {
                pair<int , int>p = s.top() ;
                sol[i][j] += sol[p.first][j] ;
            }
            ans += sol[i][j] ;
            s.push(cur) ;
        }
    }
    return printf("%lld" , ans) , 0 ;
}




Compilation message

bob.cpp: In function 'int main()':
bob.cpp:66: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:70:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d" , &arr[i][j]) ;
             ~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 4856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 5292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 5496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 5368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 169 ms 22752 KB Output is correct
2 Correct 144 ms 18040 KB Output is correct
3 Correct 149 ms 18040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 192 ms 25720 KB Output is correct
2 Correct 160 ms 17988 KB Output is correct
3 Correct 151 ms 17912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 186 ms 25600 KB Output is correct
2 Correct 151 ms 17912 KB Output is correct
3 Correct 140 ms 17940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 193 ms 25860 KB Output is correct
2 Correct 141 ms 17932 KB Output is correct
3 Correct 146 ms 17912 KB Output is correct