#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second #include "dzialka.h" #include "message.h" int my_id, nodes; const int N = 80100; // WEJSCIE vector<int> t[N]; // WYJSCIE: odleglosc od: lewej, prawej, dolu, gory vector<int> le, ri, lo, hi; // WYJSCIE: czy cale wiersze/kolumny sa usable vector<int> horiz, vert; // glebokosci usable int d[N]; // stos glebokosci, stos pozycji, wskaznik gory stosu int dep[N], pos[N], q; ll solve_one(int n, int m) { ll res = 0; for (int i = n-1; i >= 0; i--) { FOR(j,m) d[j] = t[i][j] ? d[j]+1 : 0; pos[0] = -1; dep[0] = -1; q = 0; //FOR(j,m) printf("%d ", d[j]); printf("\n"); fflush(stdout); ll sumh = 0; for (int j = 0; j < m; j++) { int last = j; while (dep[q] > d[j]) { sumh -= 1LL * (pos[q]-pos[q-1]) * dep[q]; last = pos[q]; q--; } //printf("j=%d, sumh=%lld\n", j, sumh); sumh += 1LL * (j-pos[q]) * d[j]; q++; dep[q] = d[j]; pos[q] = j; //printf("j=%d, sumh=%lld\n", j, sumh); res += sumh; } //printf("res=%lld\n", res); fflush(stdout); } return res; } int main() { my_id = MyNodeId(); if (my_id != 0) return 0; nodes = NumberOfNodes(); int n = GetFieldHeight(); int m = GetFieldWidth(); for (int i = 0; i < n; i++) { t[i].resize(m); for (int j = 0; j < m; j++) { t[i][j] = IsUsableCell(i,j); } } ll cur = solve_one(n, m); printf("%lld\n", cur); return 0; }
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 | #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second #include "dzialka.h" #include "message.h" int my_id, nodes; const int N = 80100; // WEJSCIE vector<int> t[N]; // WYJSCIE: odleglosc od: lewej, prawej, dolu, gory vector<int> le, ri, lo, hi; // WYJSCIE: czy cale wiersze/kolumny sa usable vector<int> horiz, vert; // glebokosci usable int d[N]; // stos glebokosci, stos pozycji, wskaznik gory stosu int dep[N], pos[N], q; ll solve_one(int n, int m) { ll res = 0; for (int i = n-1; i >= 0; i--) { FOR(j,m) d[j] = t[i][j] ? d[j]+1 : 0; pos[0] = -1; dep[0] = -1; q = 0; //FOR(j,m) printf("%d ", d[j]); printf("\n"); fflush(stdout); ll sumh = 0; for (int j = 0; j < m; j++) { int last = j; while (dep[q] > d[j]) { sumh -= 1LL * (pos[q]-pos[q-1]) * dep[q]; last = pos[q]; q--; } //printf("j=%d, sumh=%lld\n", j, sumh); sumh += 1LL * (j-pos[q]) * d[j]; q++; dep[q] = d[j]; pos[q] = j; //printf("j=%d, sumh=%lld\n", j, sumh); res += sumh; } //printf("res=%lld\n", res); fflush(stdout); } return res; } int main() { my_id = MyNodeId(); if (my_id != 0) return 0; nodes = NumberOfNodes(); int n = GetFieldHeight(); int m = GetFieldWidth(); for (int i = 0; i < n; i++) { t[i].resize(m); for (int j = 0; j < m; j++) { t[i][j] = IsUsableCell(i,j); } } ll cur = solve_one(n, m); printf("%lld\n", cur); return 0; } |