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
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include "dzialka.h"
#include "message.h"
#include <bits/stdc++.h>
using namespace std;
#define PB push_back
#define MP make_pair
#define LL long long
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define RE(i,n) FOR(i,1,n)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define R(i,n) REP(i,n)
#define VI vector<int>
#define PII pair<int,int>
#define LD long double
#define FI first
#define SE second
#define f first
#define s second
#define st FI
#define nd SE
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

template<class C> void mini(C& _a4, C _b4) { _a4 = min(_a4, _b4); }
template<class C> void maxi(C& _a4, C _b4) { _a4 = max(_a4, _b4); }

template<class TH> void _dbg(const char *sdbg, TH h){ cerr << sdbg << '=' << h << endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

template<class T> ostream& operator<<(ostream& os, vector<T> V) {
  os << "["; for (auto vv : V) os << vv << ","; os << "]"; return os;
}
template<class L, class R> ostream& operator<<(ostream& os, pair<L, R> P) {
  return os << "(" << P.st << ", " << P.nd << ")";
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

//// Library used by Marek Sokolowski during PA 2016. 

const int SendAfter = 63990;

struct Sender {
    enum FieldType { FLD_CHAR, FLD_INT, FLD_LL };
    struct field {
        FieldType type;
        union { char ch; int n; LL l; };
    };
    struct Submitter {};

    vector<field> toSend;
    int dest;
    int cap;

    Sender() : dest(-10000), cap(SendAfter) {}
    Sender(int dst){
        dest = dst;
        cap = SendAfter;
        toSend.clear();
    }
    ~Sender(){
        if(toSend.size() > 0) submit();
    }

    int capacity() const { return cap; }
    void capacity(int newCapacity) { cap = newCapacity; }
    
    void submit(){
        LL msgsize = 4;
        if(toSend.size() == 0) return;
        PutInt(dest, (int)toSend.size());
        for(auto& f : toSend){
            switch(f.type){
                case FLD_CHAR: PutChar(dest, f.ch); msgsize++; break;
                case FLD_INT:  PutInt(dest, f.n); msgsize += 4; break;
                case FLD_LL:   PutLL(dest, f.l); msgsize += 8; break;
                default:       assert(false);
            }
        }
        Send(dest);
        toSend.clear();
    }

    void add_field(field f){
        assert(dest >= 0);
        toSend.push_back(f);
        if(toSend.size() >= cap) submit();
    }

    friend Sender& operator<<(Sender& snd, char ch){
        field f; f.type = FLD_CHAR; f.ch = ch; snd.add_field(f); return snd;
    }
    friend Sender& operator<<(Sender& snd, int n){
        field f; f.type = FLD_INT; f.n = n; snd.add_field(f); return snd;
    }
    friend Sender& operator<<(Sender& snd, LL l){
        field f; f.type = FLD_LL; f.l = l; snd.add_field(f); return snd;
    }
    friend Sender& operator<<(Sender& snd, Sender::Submitter){
        snd.submit(); return snd;
    }
};
Sender::Submitter submit;


struct Receiver {
    int numEnqueued;
    int node;

    Receiver() : node(-10000) {}
    Receiver(int nod) : node(nod), numEnqueued(0) {}

    void try_fetch(){
        assert(node >= -1);
        assert(numEnqueued >= 0);
        if(numEnqueued > 0){ numEnqueued--; return; }
        Receive(node);
        numEnqueued = GetInt(node);
        numEnqueued--;
    }

    char read_char(){ try_fetch(); return GetChar(node); }
    int read_int(){ try_fetch(); return GetInt(node); }
    LL read_ll(){ try_fetch(); return GetLL(node); }

    friend Receiver& operator>>(Receiver& recv, char& ch){
        ch = recv.read_char(); return recv;
    }
    friend Receiver& operator>>(Receiver& recv, int& n){
        n = recv.read_int(); return recv;
    }
    friend Receiver& operator>>(Receiver& recv, LL& l){
        l = recv.read_ll(); return recv;
    }
};

template<typename T>
Sender& operator<<(Sender& snd, const vector<T>& vec){
    snd << (int)vec.size();
    for(T v : vec) snd << v;
    return snd;
}

template<typename T>
Receiver& operator>>(Receiver& recv, vector<T>& vec){
    int sz;
    recv >> sz;
    vec = vector<T>(sz);
    for(int i = 0; i < sz; i++){
        recv >> vec[i];
    }
    return recv;
}


LL MyNode, TotalNodes;
vector<Sender> senders;
vector<Receiver> receivers;

void init(){
    MyNode     = MyNodeId();
    TotalNodes = NumberOfNodes();
    for(LL i = 0; i < TotalNodes; i++){
        senders.emplace_back(i);
        receivers.emplace_back(i);
    }
}


///////////////////////////////////////////////////////////////////////////

int64_t solveSegment(int left, int right, vector<int> &a, vector<int> &b) {
    if(left == right) {
        return 1LL * a[left] * b[right];
    }

    int mid = (left + right) / 2;
    int64_t ans = solveSegment(left, mid, a, b) + solveSegment(mid + 1, right, a, b);

    vector<pair<int, int>> left_pairs, right_pairs;

    for(int i = mid; i >= left; --i) {
        pair<int, int> current = make_pair(a[i], b[i]);
        if(not left_pairs.empty()) {
            current.first = min(current.first, left_pairs.back().first);
            current.second = min(current.second, left_pairs.back().second);
        }
        left_pairs.push_back(current);
    }
    
    int64_t right_sum_of_pairs = 0, sum_first = 0, sum_second = 0;
    vector<int64_t> part_first(right - mid, 0), part_second(right - mid, 0);

    for(int i = mid + 1; i <= right; ++i) {
        pair<int, int> current = make_pair(a[i], b[i]);
        if(not right_pairs.empty()) {
            current.first = min(current.first, right_pairs.back().first);
            current.second = min(current.second, right_pairs.back().second);
        }
        right_pairs.push_back(current);
        right_sum_of_pairs += 1LL * current.first * current.second;
        sum_first += current.first;
        sum_second += current.second;
        part_first[i - (mid + 1)] = sum_first;
        part_second[i - (mid + 1)] = sum_second;
    }
    
    int it = 0;
    
    for(int i = 0; i <= mid - left; ++i) { // <= <=
        while(it < int(right_pairs.size())
            and right_pairs[it].first >= left_pairs[i].first
            and right_pairs[it].second >= left_pairs[i].second) {
            it += 1;
        }
        ans += 1LL * it * left_pairs[i].first * left_pairs[i].second;
    }
    
    it = 0;

    for(int i = 0; i <= mid - left; ++i) { // > >
        while(it < int(right_pairs.size())
            and (right_pairs[it].first >= left_pairs[i].first
            or right_pairs[it].second >= left_pairs[i].second)) {
            right_sum_of_pairs -= 1LL * right_pairs[it].first * right_pairs[it].second;
            it += 1;
        }
        ans += right_sum_of_pairs;
    }

    int it_first = 0, it_second = 0;
    
    for(int i = 0; i <= mid - left; ++i) { // <= >
        while(it_first < int(right_pairs.size())
            and right_pairs[it_first].first >= left_pairs[i].first) {
            it_first += 1;
        }
        while(it_second < int(right_pairs.size())
            and right_pairs[it_second].second >= left_pairs[i].second) {
            it_second += 1;
        }
        
        if(it_second < int(right_pairs.size()) and it_second <= it_first - 1) { // <= <
            int64_t sub = 0;
            if(it_second >= 1)
                sub = part_second[it_second - 1];
            ans += 1LL * left_pairs[i].first * (part_second[it_first - 1] - sub);
        }

        if(it_first < int(right_pairs.size()) and it_first <= it_second - 1) { // > <=
            int64_t sub = 0;
            if(it_first >= 1)
                sub = part_first[it_first - 1];
            ans += 1LL * left_pairs[i].second * (part_first[it_second - 1] - sub);
        }
    }
    
    return ans; 
}

int64_t solveFast(vector<int> a, vector<int> b) {
    int n = a.size();
    return solveSegment(0, n - 1, a, b);
}

int64_t solveSimple(vector<int> v) {
    int n = v.size();

    vector<int> st;
    vector<int> left(n, -1), right(n, n);

    for(int i = 0; i < n; ++i) {
        while(not st.empty() and v[i] <= v[st.back()]) {
            st.pop_back();
        }
        if(not st.empty()) {
            left[i] = st.back();
        }
        st.push_back(i);
    }
    
    st.clear();

    for(int i = n - 1; i >= 0; --i) {
        while(not st.empty() and v[i] < v[st.back()]) {
            st.pop_back();
        }
        if(not st.empty()) {
            right[i] = st.back();
        }
        st.push_back(i);
    }

    int64_t ans = 0;

    for(int i = 0; i < n; ++i) {
        ans += v[i] * (i - left[i]) * (right[i] - i);
    }

    return ans;
}

bool switched = false;

int father(int x) {
    return (x - 1) / 2;
}

int left_son(int x) {
    return 2 * (x + 1) - 1;
}

int right_son(int x) {
    return 2 * (x + 1);
}

int ask(int x, int y) {
    if(switched)
        return IsUsableCell(y, x);
    return IsUsableCell(x, y);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout << fixed << setprecision(11);
    cerr << fixed << setprecision(6);
    init();

    int n = GetFieldHeight();
    int m = GetFieldWidth();

    if(n > m) {
        switched = true;
        swap(n, m);
    }
        
    int left = 0, right = m - 1;

    if(MyNode > 0) {
        receivers[father(MyNode)] >> left >> right;
    } 
    

    int mid = (left + right) / 2;
    long long ans = 0;

    if(right_son(MyNode) < TotalNodes and left < right) {
        senders[left_son(MyNode)] << left << mid << submit;
        senders[right_son(MyNode)] << mid + 1 << right << submit;
        
        long long to_add_LEFT;
        vector<int> left_mask_LEFT, right_mask_LEFT;
        receivers[left_son(MyNode)] >> to_add_LEFT >> left_mask_LEFT >> right_mask_LEFT;
        
        long long to_add_RIGHT;
        vector<int> left_mask_RIGHT, right_mask_RIGHT;
        receivers[right_son(MyNode)] >> to_add_RIGHT >> left_mask_RIGHT >> right_mask_RIGHT;

        ans += to_add_LEFT + to_add_RIGHT;
        ans += solveFast(right_mask_LEFT, left_mask_RIGHT);
        if(MyNode == 0) {
            cout << ans << "\n";
            return 0;
        } else {
            senders[father(MyNode)] << ans << left_mask_LEFT << right_mask_RIGHT << submit;
            return 0;
        }
    } else {
        vector<int> height(n, 0);
        vector<int> prev_height(n, 0);

        for(int j = left; j <= right; ++j) {
            for(int i = n - 1; i >= 0; --i) {
                int cell = ask(i, j);
                if(cell == 1) {
                    height[i] = prev_height[i] + 1;
                } else {
                    height[i] = 0;
                }
            }
            ans += solveSimple(height);
            prev_height = height;
        }

        vector<int> right_mask = height;

        height = vector<int> (n, 0);
        prev_height = vector<int> (n, 0);

        for(int j = right; j >= left; --j) {
            for(int i = n - 1; i >= 0; --i) {
                int cell = ask(i, j);
                if(cell == 1) {
                    height[i] = prev_height[i] + 1;
                } else {
                    height[i] = 0;
                }
            }
            prev_height = height;
        }

        vector<int> left_mask = height;
        senders[father(MyNode)] << ans << left_mask << right_mask << submit;

        cerr << ans << "\n";
        return 0;
    }
}