1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "dzialka.h"
#include "message.h"
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;
const int roz = 100 * 1000 + 10;
int gl[roz];
vector <pair <int, int> > stos;

int main()
{
    ios_base::sync_with_stdio(0);
    if(MyNodeId() > 0)
        return 0;

    int i, j;
    int n = GetFieldHeight();
    int m = GetFieldWidth();

    ll odp = 0;

    for(i = 0; i < n; i++)
    {
        //string a;
        //cin>>a;
        ll moz = 0;
        stos.clear();
        stos.push_back(make_pair(0, 0));
        for(j = 0; j < m; j++)
        {
            if(IsUsableCell(i, j))
            {
                gl[j]++;
                ll pom = 0;
                while(stos.back().first > gl[j])
                {
                    pom += stos.back().second;
                    moz -= stos.back().second * stos.back().first;
                    stos.pop_back();
                }
                if(stos.back().first != gl[j])
                {
                    stos.push_back(make_pair(gl[j], 0));
                }
                //cout<<moz<<".";
                moz += pom * stos.back().first + gl[j];
                stos.back().second += pom + 1;
            }
            else
            {
                moz = 0;
                gl[j] = 0;
                while(stos.back().first != 0)
                {
                    stos.pop_back();
                }
            }
            //cout<<moz<<" ";
            odp += moz;
        }
        //cout<<"\n";
    }
    cout<<odp;
}