Submission #103766

# Submission time Handle Problem Language Result Execution time Memory
103766 2019-04-02T13:12:40 Z MohamedAhmed0 Bob (COCI14_bob) C++14
120 / 120
225 ms 18168 KB
#include <bits/stdc++.h>

using namespace std;

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]) ;
    }
    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] ;
    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 ;
            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 ;
            }
            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:8: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:13: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 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 4344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 4344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 4472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 4224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 201 ms 16164 KB Output is correct
2 Correct 152 ms 16120 KB Output is correct
3 Correct 168 ms 18168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 224 ms 16176 KB Output is correct
2 Correct 173 ms 16216 KB Output is correct
3 Correct 205 ms 17912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 225 ms 16176 KB Output is correct
2 Correct 161 ms 16116 KB Output is correct
3 Correct 149 ms 18004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 201 ms 16260 KB Output is correct
2 Correct 160 ms 16120 KB Output is correct
3 Correct 138 ms 17884 KB Output is correct