#include "dzialka.h" #include "message.h" #include <algorithm> #include <iostream> #include <stack> #include <vector> using namespace std; #define LL long long #define PB push_back LL width, height; bool isLastNode; inline pair<int, int> getSampleInterval(int N, int id) { return make_pair( id * (N / NumberOfNodes() + 1), min((id + 1) * (N / NumberOfNodes() + 1), N) - 1); } int PrevId() { static int id = MyNodeId() - 1; return id; } int NextId() { static int id = MyNodeId() + 1; return id; } inline bool isIgnored(int N, int id) { return (getSampleInterval(N, id).first >= N); } int G[76000]; int G_DOWN[76000]; vector<int> toSend; LL score = 0; void calc_G_and_send_down(); void solve(); int main() { width = GetFieldWidth(); height = GetFieldHeight(); isLastNode = isIgnored(height, NextId()); auto p = getSampleInterval(height, MyNodeId()); // same as isIgnored(...) if (p.first >= height) {// Too many nodes - we can ignore this one. return 0; } calc_G_and_send_down(); solve(); if (MyNodeId() != 0) { int addr = 0; PutLL(addr, score); Send(addr); } else { for (int i = 1; i < NumberOfNodes(); ++i) { if (isIgnored(height, i)) { break; } Receive(i); score += GetLL(i); } cout << score << endl; } return 0; } void calc_G_and_send_down() { auto p = getSampleInterval(height, MyNodeId()); for (int i = 0; i < width; ++i) { if (p.first != 0) { G[i] = -1; } G_DOWN[i] = 0; } vector<int> toSend; toSend.clear(); int whenToSend = (width / 1000) + 1; if (whenToSend * 1000 < width) whenToSend++; if (whenToSend < 100) whenToSend = 100; for (int i = 0; i < width; ++i) { if (G[i] == -1) { int prev = PrevId(); Receive(prev); int countMsgs = GetInt(prev); for (int k = 0; k < countMsgs; k++) { G[i + k] = GetInt(prev); G_DOWN[i + k] = G[i + k]; } } for (int j = p.first; j <= p.second; ++j) { int nj = j - p.first + 1; bool usable = IsUsableCell(j, i); if (!usable) { G[i] = 0; } else { G[i]++; } } if (p.second == height - 1) { continue; // nie wysyłamy nic jeżeli mamy najwyższą kolumnę } toSend.PB(G[i]); if (i == width - 1 // ostatnia kolumna || toSend.size() > whenToSend) { int addr = NextId(); PutInt(addr, toSend.size()); for (auto msg : toSend) { PutInt(addr, msg); } Send(addr); toSend.clear(); } } } struct Block { int x; // na jakim przedziale int G; LL adds; // ile blokuje }; Block nBlock(int x, int G, LL b) { return { .x = x, .G = G, .adds = b }; } void solve() { auto p = getSampleInterval(height, MyNodeId()); for (int i = 0; i < width; ++i) { G[i] = G_DOWN[i]; } LL count_act; stack<Block> S; for (int j = p.first; j <= p.second; ++j) { count_act = 0; while (!S.empty()) S.pop(); S.push(nBlock(-1, -1, 0)); for (int i = 0; i < width; ++i) { bool usable = IsUsableCell(j, i); if (!usable) { G[i] = 0; } else { G[i]++; } while (true) { Block b = S.top(); if (G[i] <= b.G) { S.pop(); count_act -= b.adds; } else { break; } } Block b = S.top(); int x1 = b.x + 1; LL wdth = i - x1 + 1; LL hght = G[i]; LL adds = wdth * hght; count_act += adds; S.push(nBlock(i, G[i], adds)); score += count_act; } } }
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 | #include "dzialka.h" #include "message.h" #include <algorithm> #include <iostream> #include <stack> #include <vector> using namespace std; #define LL long long #define PB push_back LL width, height; bool isLastNode; inline pair<int, int> getSampleInterval(int N, int id) { return make_pair( id * (N / NumberOfNodes() + 1), min((id + 1) * (N / NumberOfNodes() + 1), N) - 1); } int PrevId() { static int id = MyNodeId() - 1; return id; } int NextId() { static int id = MyNodeId() + 1; return id; } inline bool isIgnored(int N, int id) { return (getSampleInterval(N, id).first >= N); } int G[76000]; int G_DOWN[76000]; vector<int> toSend; LL score = 0; void calc_G_and_send_down(); void solve(); int main() { width = GetFieldWidth(); height = GetFieldHeight(); isLastNode = isIgnored(height, NextId()); auto p = getSampleInterval(height, MyNodeId()); // same as isIgnored(...) if (p.first >= height) {// Too many nodes - we can ignore this one. return 0; } calc_G_and_send_down(); solve(); if (MyNodeId() != 0) { int addr = 0; PutLL(addr, score); Send(addr); } else { for (int i = 1; i < NumberOfNodes(); ++i) { if (isIgnored(height, i)) { break; } Receive(i); score += GetLL(i); } cout << score << endl; } return 0; } void calc_G_and_send_down() { auto p = getSampleInterval(height, MyNodeId()); for (int i = 0; i < width; ++i) { if (p.first != 0) { G[i] = -1; } G_DOWN[i] = 0; } vector<int> toSend; toSend.clear(); int whenToSend = (width / 1000) + 1; if (whenToSend * 1000 < width) whenToSend++; if (whenToSend < 100) whenToSend = 100; for (int i = 0; i < width; ++i) { if (G[i] == -1) { int prev = PrevId(); Receive(prev); int countMsgs = GetInt(prev); for (int k = 0; k < countMsgs; k++) { G[i + k] = GetInt(prev); G_DOWN[i + k] = G[i + k]; } } for (int j = p.first; j <= p.second; ++j) { int nj = j - p.first + 1; bool usable = IsUsableCell(j, i); if (!usable) { G[i] = 0; } else { G[i]++; } } if (p.second == height - 1) { continue; // nie wysyłamy nic jeżeli mamy najwyższą kolumnę } toSend.PB(G[i]); if (i == width - 1 // ostatnia kolumna || toSend.size() > whenToSend) { int addr = NextId(); PutInt(addr, toSend.size()); for (auto msg : toSend) { PutInt(addr, msg); } Send(addr); toSend.clear(); } } } struct Block { int x; // na jakim przedziale int G; LL adds; // ile blokuje }; Block nBlock(int x, int G, LL b) { return { .x = x, .G = G, .adds = b }; } void solve() { auto p = getSampleInterval(height, MyNodeId()); for (int i = 0; i < width; ++i) { G[i] = G_DOWN[i]; } LL count_act; stack<Block> S; for (int j = p.first; j <= p.second; ++j) { count_act = 0; while (!S.empty()) S.pop(); S.push(nBlock(-1, -1, 0)); for (int i = 0; i < width; ++i) { bool usable = IsUsableCell(j, i); if (!usable) { G[i] = 0; } else { G[i]++; } while (true) { Block b = S.top(); if (G[i] <= b.G) { S.pop(); count_act -= b.adds; } else { break; } } Block b = S.top(); int x1 = b.x + 1; LL wdth = i - x1 + 1; LL hght = G[i]; LL adds = wdth * hght; count_act += adds; S.push(nBlock(i, G[i], adds)); score += count_act; } } } |