#include "message.h"
#include "dzialka.h"
#include <bits/stdc++.h>
#define f first
#define s second
#define MP make_pair
#define PB push_back
#define LL long long
#define pii pair<int,int>
#define pll pair<LL,LL>
#define ALL(V) V.begin(),V.end()
#define f1(a,b) for(int a=1;a<=b;a++)
#define f0(a,b) for(int a=0;a<b;a++)
#define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define endl "\n"
using namespace std;
const int N=10000+69;
int t[N];
LL wyn=0, dod=0;
stack <pii> S;
/*int MyNodeId() {
return 0;
}
int GetFieldHeight() {
return 3;
}
int GetFieldWidth() {
return 3;
}
bool IsUsableCell(int a,int b) {
if(MP(a,b)==MP(1,0)||MP(a,b)==MP(1,2)) return 0;
return 1;
}*/
void popraw(int,int);
int32_t main(void) {
int nr=MyNodeId();
if(nr!=0) return 0;
int n=GetFieldHeight();
int m=GetFieldWidth();
f0(j,m) t[j]=n;
for(int i=n-1;i>=0;i--) {
dod=0;
while(!S.empty()) S.pop();
f0(j,m) {
if(IsUsableCell(i,j)==0) t[j]=i;
popraw(j,i);
wyn+=dod;
}
}
cout<<wyn<<endl;
}
void popraw(int v, int odl) {
if(S.empty()) {
S.push(MP(v,t[v]-odl));
dod=S.top().s;
return;
}
while(S.empty()==0&&t[S.top().f]>t[v]) {
dod-=S.top().s;
S.pop();
if(S.empty()==0) {
dod-=S.top().s;
S.top().s=(v-S.top().f)*(t[S.top().f]-odl);
dod+=S.top().s;
}
}
if(S.empty()==1) {
S.push(MP(v,t[v]-odl));
dod=S.top().s;
}
else if(S.top().f!=v) {
S.push(MP(v,t[v]-odl));
dod+=S.top().s;
}
}
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 | #include "message.h" #include "dzialka.h" #include <bits/stdc++.h> #define f first #define s second #define MP make_pair #define PB push_back #define LL long long #define pii pair<int,int> #define pll pair<LL,LL> #define ALL(V) V.begin(),V.end() #define f1(a,b) for(int a=1;a<=b;a++) #define f0(a,b) for(int a=0;a<b;a++) #define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define endl "\n" using namespace std; const int N=10000+69; int t[N]; LL wyn=0, dod=0; stack <pii> S; /*int MyNodeId() { return 0; } int GetFieldHeight() { return 3; } int GetFieldWidth() { return 3; } bool IsUsableCell(int a,int b) { if(MP(a,b)==MP(1,0)||MP(a,b)==MP(1,2)) return 0; return 1; }*/ void popraw(int,int); int32_t main(void) { int nr=MyNodeId(); if(nr!=0) return 0; int n=GetFieldHeight(); int m=GetFieldWidth(); f0(j,m) t[j]=n; for(int i=n-1;i>=0;i--) { dod=0; while(!S.empty()) S.pop(); f0(j,m) { if(IsUsableCell(i,j)==0) t[j]=i; popraw(j,i); wyn+=dod; } } cout<<wyn<<endl; } void popraw(int v, int odl) { if(S.empty()) { S.push(MP(v,t[v]-odl)); dod=S.top().s; return; } while(S.empty()==0&&t[S.top().f]>t[v]) { dod-=S.top().s; S.pop(); if(S.empty()==0) { dod-=S.top().s; S.top().s=(v-S.top().f)*(t[S.top().f]-odl); dod+=S.top().s; } } if(S.empty()==1) { S.push(MP(v,t[v]-odl)); dod=S.top().s; } else if(S.top().f!=v) { S.push(MP(v,t[v]-odl)); dod+=S.top().s; } } |
English