Problem:
Processing Machine
Aizu Kogyo undertakes manufacturing work using a processing machine. The processing machine owned by Aizu Kogyo is shown on the left in Figure 1. This machine can process any rectangular object, and its width, depth, and height can be freely set. There is a blade 1 cm wide and 1 cm deep on the top of this rectangular object. This blade can be moved directly above the position where you want to cut. The position where you want to cut is specified by the coordinates of the compartment as shown in the right side of Figure 1.
Figure 1:  3D view of the machine and the coordinates of the compartment
The machine will cut a rectangular object by repeating the following steps.
Move the blade to directly above the specified section.
Lower the blade until it touches the object to be machined, and then cut a new section of the specified length.
Raise the blade and bring it back to the initial height.
An example of shaving one of the end sections is shown in Figure 2.
Figure 2: Example of cutting using a processing machine
At Aizu Industries, we determine the processing fee based on the amount of surface area after processing. Until now, this has been measured manually by craftsmen, but we would like to be able to calculate this using a program.
Given the size of the rectangular object to be machined and information on how the rectangular object is to be machined, write a program that calculates how much the surface area will be after machining.
Input
The input is given in the following form.
$W$ $D$ $H$ $C$
$x_1$ $y_1$ $z_1$
$x_2$ $y_2$ $z_2$
:
$x_C$ $y_C$ $z_C$
In the first line, the width $W$ ($1 \leq W \leq 100,000$), depth $D$ ($1 \leq D \leq 100,000$), height $H$ ($1 \leq H \leq 100,000$), and the number of cuts $C$ ($0 \leq C \leq 100,000$) of the rectangle to be machined are given. In the following $C$ lines, the information on how to cut the rectangle is given. $x_i$ ($0 \leq x_i < W$) and $y_i$ ($0 \leq y_i < D$) are the coordinates of the plot to be cut. Also, $z_i$ ($1 \leq z_i < H$) is an integer that represents how much the plot should be cut from its current height. However, for each parcel, no input is given such that the height after shaving will be less than 1.
Output
Output the surface area after processing a line.
Sample Input and Output
Sample Input 1
3 4 5 2
0 0 4
1 1 4
Sample Output 1
110
Sample Input 2
1 2 3 0
Sample Output 2
22
Since Sample Input 2 is a case where the surface area is never scraped, the surface area is the same as the surface area of ​​the given rectangular.

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
//#include<atcoder/all>

using namespace std;
//using namespace atcoder;
#define rep(i,n) for(ll i=0; i<n; i++)
#define rrep(i,n) for(ll i=n-1; i>=0; i--)
#define print(a) cout<<a<<endl
typedef long long ll;
#define yn(flg) if(flg){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
#define YN(flg) if(flg){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define so(a) sort(a.begin(),a.end())
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define vs vector<string>
#define pb push_back
#define a2i(a,s) (ll)(a-s)
#define i2a(s,a) (char)(s+a)
#define ssize(a) a.size()
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<pair<ll,ll>,ll> P3;
typedef pair<pair<ll,ll>,pair<ll,ll>> P4;

typedef pair<ll, ll> Pll;
typedef pair<ll,Pll> Plll;
typedef pair<Pii, int> Piii;
const ll INF = 1000000000000000000;

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

int main(){
//入力

	cin.tie(0);
	ios::sync_with_stdio(0);


	ll w,d,h,c;
	cin>>w>>d>>h>>c;
	vector<ll> x(c);
	vector<ll> y(c);
	vector<ll> z(c);
	map<Pll,ll> mp;
	map<ll,vector<Pll> > mpi;
	map<ll,vector<Pll> > mpj;
	set<ll> si;
	set<ll> sj;
	set<Pll> st;
	rep(i,c){
		cin>>x[i]>>y[i]>>z[i];
		mp[{x[i],y[i]}]+=z[i];
		st.insert({x[i],y[i]});
	}
	for(Pll i:st){
		ll a=i.first,b=i.second;
		mpi[a].push_back({b,mp[{a,b}]});
		mpj[b].push_back({a,mp[{a,b}]});
		si.insert(a);
		sj.insert(b);
	}


//ソート
	for(ll i:si){
		sort(mpi[i].begin(),mpi[i].end());
	}
	for(ll i:sj){
		sort(mpj[i].begin(),mpj[i].end());
	}
//判定
	ll ans=0;
	ans=2*d*w;
	ans+=2*h*w;
	ans+=2*d*h;
	//hw側
	for(ll i:si){
		ll ny=0,nc=0;
		for(Pll j:mpi[i]){
			ll Y=j.first,C=j.second;
			if(Y==0)ans-=C;
			else{
				if(ny==Y-1){
					ans+=abs(C-nc);
				}
				else{
					ans+=nc;
					ans+=C;
				}
			}
			ny=Y;
			nc=C;
		}
		if(ny==d-1){
			ans-=nc;
		}else{
			ans+=nc;
		}
	}
//dh側
	for(ll i:sj){
		ll nx=0,nc=0;
		for(Pll j:mpj[i]){
			ll X=j.first,C=j.second;
			if(X==0)ans-=C;
			else{
				if(nx==X-1){
					ans+=abs(C-nc);
				}else{
					ans+=nc;
					ans+=C;
				}
			}
			nx=X;
			nc=C;
		}
		if(nx==w-1){
			ans-=nc;
		}else{
			ans+=nc;
		}
	}
	cout<<ans<<endl;


	return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 38980
final_rank: 7

code_snippet: #include<bits/stdc++.h>
//#include<atcoder/all>

using namespace std;
//using namespace atcoder;
#define rep(i,n) for(ll i=0; i<n; i++)
#define rrep(i,n) for(ll i=n-1; i>=0; i--)
#define print(a) cout<<a<<endl
typedef long long ll;
#define yn(flg) if(flg){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
#define YN(flg) if(flg){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define so(a) sort(a.begin(),a.end())
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define vs vector<string>
#define pb push_back
#define a2i(a,s) (ll)(a-s)
#define i2a(s,a) (char)(s+a)
#define ssize(a) a.size()
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<pair<ll,ll>,ll> P3;
typedef pair<pair<ll,ll>,pair<ll,ll>> P4;

typedef pair<ll, ll> Pll;
typedef pair<ll,Pll> Plll;
typedef pair<Pii, int> Piii;
const ll INF = 1000000000000000000;

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

int main(){
//入力

	cin.tie(0);
	ios::sync_with_stdio(0);


	ll w,d,h,c;
	cin>>w>>d>>h>>c;
	vector<ll> x(c);
	vector<ll> y(c);
	vector<ll> z(c);
	map<ll,vector<Pll> > mpi;
	map<ll,vector<Pll> > mpj;
	set<ll> si;
	set<ll> sj;

	rep(i,c){
		cin>>x[i]>>y[i]>>z[i];
		mpi[x[i]].push_back({y[i],z[i]});
		mpj[y[i]].push_back({x[i],z[i]});
		si.insert(x[i]);
		sj.insert(y[i]);
	}

//ソート
	for(ll i:si){
		sort(mpi[i].begin(),mpi[i].end());
	}
	for(ll i:sj){
		sort(mpj[i].begin(),mpj[i].end());
	}
//判定
	ll ans=0;
	ans=2*d*w;
	ans+=2*h*w;
	ans+=2*d*h;
	//hw側
	for(ll i:si){
		ll ny=0,nc=0;
		for(Pll j:mpi[i]){
			ll Y=j.first,C=j.second;
			if(Y==0)ans-=C;
			else{
				if(ny==Y-1){
					ans+=abs(C-nc);
				}else{
					ans+=nc;
					ans+=C;
				}
			}
			ny=Y;
			nc=C;
		}
		if(ny==d-1){
			ans-=nc;
		}else{
			ans+=nc;
		}
	}
//dh側
	for(ll i:sj){
		ll nx=0,nc=0;
		for(Pll j:mpj[i]){
			ll X=j.first,C=j.second;
			if(X==0)ans-=C;
			else{
				if(nx==X-1){
					ans+=abs(C-nc);
				}else{
					ans+=nc;
					ans+=C;
				}
			}
			nx=X;
			nc=C;
		}
		if(nx==w-1){
			ans-=nc;
		}else{
			ans+=nc;
		}
	}
	cout<<ans<<endl;


	return 0;
}
accuracy: 0.5384615384615384
time_ms: 170
memory_kb: 26572
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef double dou;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
struct four_ll_pair{ll ff,fs,sf,ss;};
typedef pair<char,ll> pcl;
struct three_pcl_pair{pcl l,c,r;};
typedef string str;
typedef vector<str> vstr;
typedef map<ll,ll> mll;
typedef priority_queue<ll> maxpq;
typedef priority_queue<ll,vll,greater<ll>> minpq;
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define f0r(i,b) for(ll i=0;i<b;i++)
#define SORT(a) sort(a.begin(),a.end());
#define llin(a,n) f0r(i,n) {cin>>a[i];}
#define REVERSE(a) reverse(a.begin(),a.end());
const ll llmax=9223372036854775783LL;
const ll mod=998244353LL;
const ll MOD=1000000007LL;
str ALP="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
str alp="abcdefghijklmnopqrstuvwxyz";
void Yn(bool b) {cout<<(b?"Yes":"No")<<endl;}
void YN(bool b) {cout<<(b?"YES":"NO")<<endl;}
vll Remove_duplicates(vll a){
    a.erase(unique(a.begin(),a.end()),a.end());
    return a;
}
vll coordinate_compression(vll v){
    ll n=v.size();
    vll b=v;SORT(b);
    b=Remove_duplicates(b);
    vll ans(n);
    f0r(i,n){
        ll pos=lower_bound(b.begin(),b.end(),v[i])-b.begin();
        ans[i]=pos+1;
    }
    return ans;
}
namespace mod_calc{
    ll mod_pow(ll base,ll a,ll m){
        ll result=1;
        while(a>0){
            if (a%2==1) result=(result*base)%m;
            base=(base*base)%m;
            a>>=1;
        }
        return result;
    }
    ll mod_divide(ll a,ll b,ll m){
        return a*mod_pow(b,m-2,m)%m;
    }
    ll mod_fraction(ll n,ll a,ll b,ll m){
        return mod_divide((n*a%m),b,m);
    }
    ll mod_com(ll a,ll b,ll m){
        ll ans=1;
        if (a<b) return 0;
        if (a==b) return 1;
        for(ll i=0;i<b;i++) ans=mod_fraction(ans,a-i,1+i,m);
        return ans;
    }
}
struct Cumulative_Sum_2D{
    vvll data;
    Cumulative_Sum_2D(ll W,ll H):data(W+1,vll(H+1,0)){}
    void input(ll x,ll y,ll z){
        x++;y++;
        data[x][y]=z;
    }
    void build(){
        FOR(i,1,data.size()) FOR(j,1,data[i].size()) data[i][j]=data[i][j-1]+data[i-1][j]-data[i-1][j-1];
    }
    ll query(ll sx,ll sy,ll gx,ll gy){
        return(data[gx][gy]-data[sx][gy]-data[gx][sy]+data[sx][sy]);
    }
};
struct unionfind{
    vll parent,ver_size;
    unionfind(ll n):parent(n),ver_size(n,1){
        iota(parent.begin(),parent.end(),0); 
    }
    ll find(ll node,vll&parent){
        vll path;
        while(parent[node]!=node){
            path.push_back(node);
            node=parent[node];
        }
        for(ll u:path) parent[u]=node;
        return node;
    }
    void unionSets(ll u,ll w,vll&ver_size,vll&parent){
        ll rootU=find(u,parent),rootW=find(w,parent);
        if(rootU!=rootW){
            if(ver_size[rootU]<ver_size[rootW]){
                parent[rootU]=rootW;
                ver_size[rootW]+=ver_size[rootU];
            }else{
                parent[rootW]=rootU;
                ver_size[rootU]+=ver_size[rootW];
            }
        }
    }
};
namespace divisor_calc{
    ll gcd(ll a,ll b){
        while(b!=0){
            ll tmp=a%b;
            a=b;
            b=tmp;
        }
        return a;
    }
    ll lcm(ll a,ll b){
        return a*b/gcd(a,b);
    }
    vll factorizations_vll(ll n){
        ll mema=n;
        vll ftz_ans;
        for(ll i=2;i*i<=mema;i++){
            while(mema%i==0){
                mema/=i;
                ftz_ans.push_back(i);
            }
        }
        if(mema!=1) ftz_ans.push_back(mema);
        return ftz_ans;
    }
    mll factorizations_mll(ll n){
        ll mema=n;
        mll ftz_ans;
        for(ll i=2;i*i<=mema;i++){
            while(mema%i==0){
                mema/=i;
                ftz_ans[i]++;
            }
        }
        if(mema!=1) ftz_ans[mema]++;
        return ftz_ans;
    }
}
namespace all_distances{
    dou euclidean_distance(dou ax,dou ay,dou bx,dou by){
        return sqrt(pow(ax-bx,2)+pow(ay-by,2));
    }
    dou manhattan_distance(dou ax,dou ay,dou bx,dou by){
        return abs(ax-bx)+abs(ay-by);
    }
    dou chebyshev_distance(dou ax,dou ay,dou bx,dou by){
        return max(abs(ax-bx),abs(ay-by));
    }
}
static ll bit_ceil(ll n){
    if(n<=1)return 1;
    ll x=1;
    while(x<n)x<<=1;
    return x;
}
template<class S,S(*op)(S,S),S(*e)()>struct segtree{
    public:
    segtree():segtree(0){}
    explicit segtree(ll n):segtree(vector<S>(n,e())){}
    explicit segtree(const vector<S>& v):_n((ll)v.size()){
        size=(ll)bit_ceil(_n);
        d=vector<S>(2*size,e());
        for(ll i=0;i<_n;i++)d[size+i]=v[i];
        for(ll i=size-1;i>=1;i--)update(i);
    }

    void set(ll p,S x){
        assert(0<=p&&p<_n);
        p+=size;
        d[p]=x;
        for(p>>=1;p>=1;p>>=1)update(p);
    }
    S get(ll p)const{
        assert(0<=p&&p<_n);
        return d[p+size];
    }
    S prod(ll l,ll r)const{
        assert(0<=l&&l<=r&&r<=_n);
        S sml=e(),smr=e();
        l+=size;
        r+=size;
        while(l<r){
            if(l&1)sml=op(sml,d[l++]);
            if(r&1)smr=op(d[--r],smr);
            l>>=1;
            r>>=1;
        }
        return op(sml,smr);
    }
    S all_prod()const{return d[1];}
    private:
    ll _n,size;
    vector<S> d;
    void update(ll k){d[k]=op(d[2*k],d[2*k+1]);}
};
ll sum_op(ll a,ll b){return a+b;}       ll sum_e(){return 0LL;}
ll max_op(ll a,ll b){return max(a,b);}  ll max_e(){return -1LL;}
ll min_op(ll a,ll b){return min(a,b);}  ll min_e(){return llmax;}
ll xor_op(ll a,ll b){return a^b;}  ll xor_e(){return 0LL;}
four_ll_pair second_largest_op(four_ll_pair a,four_ll_pair b){
    ll FF,FS,SF,SS;
    if(a.ff==b.ff){
        FF=a.ff;FS=a.fs+b.fs;
        if(a.sf==b.sf){
            SF=a.sf;SS=a.ss+b.ss;
        }else{
            SF=max(a.sf,b.sf);
            SS=(SF==a.sf?a.ss:b.ss);
        }
    }else{
        FF=max(a.ff,b.ff);FS=(FF==a.ff?a.fs:b.fs);
        if(a.sf==b.sf){
            SF=a.sf;SS=a.ss+b.ss;
        }else{
            SF=max(a.sf,b.sf);SS=(SF==a.sf?a.ss:b.ss);
        }
        if(SF==min(a.ff,b.ff)){
            SS+=(SF==a.ff?a.fs:b.fs);
        }else{
            SF=max(SF,min(a.ff,b.ff));SS=(SF==min(a.ff,b.ff)?(SF==a.ff?a.fs:b.fs):SS);
        }
    }
    return {FF,FS,SF,SS};
} 
four_ll_pair second_largest_e(){return {0,0,0,0};}
three_pcl_pair max_combo_op(three_pcl_pair a,three_pcl_pair b){
    ll Max=max(a.c.second,b.c.second);
    if(a.r.first==b.l.first) Max=max(Max,a.r.second+b.l.second);
    char check=((a.c.first=='T'&&b.c.first=='T')?'T':'F');
    if(a.l.first!=b.l.first) check='F';
    pcl re_l=a.l,re_r=b.r;
    if(re_l.first=='0')re_l=b.l;
    else if(a.c.first=='T'&&a.r.first==b.l.first) re_l.second+=b.l.second;
    if(re_r.first=='0')re_r=a.r;
    else if(b.c.first=='T'&&a.r.first==b.l.first) re_r.second+=a.r.second;
    return {re_l,{check,Max},re_r};
}
three_pcl_pair max_combo_e(){return {{'0',0},{'T',0},{'0',0}};}
ll sum_mod_op(ll a,ll b){return (a+b)%mod;} ll sum_mod_e(){return 0LL;}
vll bfsdis(ll s,vvll graph){
    vll dist(graph.size(),1145141919810);
    queue<ll> q;
    dist[s]=0;
    q.push(s);
    while(!q.empty()){
        ll u=q.front();q.pop();
        for(ll v:graph[u]){
            if (dist[v]==1145141919810) {
                dist[v]=dist[u]+1;
                q.push(v);
            }
        }
    }
    return dist;
}
ll Ackermann_function(ll m,ll n){
    if(m<=1) return n+1+m;
    else if(m==2) return 2*n+3;
    else{
        ll ans=1LL;
        f0r(i,n+3) ans*=2LL;
        return ans-3LL;
    }
}
ll ans=0,k;
vector<vector<char>> s;
vector<vector<bool>> a;
void dfs(ll i,ll j,ll dist){
    a[i][j]=false;
    if(dist==k) {ans++; a[i][j]=true; return;}
    if(s[i][j+1]=='.'&&a[i][j+1]) dfs(i,j+1,dist+1);
    if(s[i][j-1]=='.'&&a[i][j-1]) dfs(i,j-1,dist+1);
    if(s[i+1][j]=='.'&&a[i+1][j]) dfs(i+1,j,dist+1);
    if(s[i-1][j]=='.'&&a[i-1][j]) dfs(i-1,j,dist+1);
    a[i][j]=true;
}
int main(){
    ll w,d,h,c;cin>>w>>d>>h>>c;
    map<pll,ll> m;
    ll Ans=(w*d+d*h+h*w)*2;
    f0r(i,c){
        ll x,y,z;cin>>x>>y>>z;
        ll t=((m.find({x,y})==m.end())?h:m[{x,y}])-z;
            ll r,l,ans=0;
            if(x==w-1) r=0;
            else if(m.find({x+1,y})==m.end()) r=h;
            else r=m[{x+1,y}];
            if(x==0) l=0;
            else if(m.find({x-1,y})==m.end()) l=h;
            else l=m[{x-1,y}];
            if(l>r) swap(l,r);
            if(t<=l){
                ans+=2*min(l-t,z);
            }
            if(r<=t+z){
                ans-=2*min(t+z-r,z);
            }
            Ans+=ans;
            ans=0;
            if(y==d-1) r=0;
            else if(m.find({x,y+1})==m.end()) r=h;
            else r=m[{x,y+1}];
            if(y==0) l=0;
            else if(m.find({x,y-1})==m.end()) l=h;
            else l=m[{x,y-1}];
            if(l>r) swap(l,r);
            if(t<=l){
                ans+=2*min(l-t,z);
            }
            if(r<=t+z){
                ans-=2*min(t+z-r,z);
            }
            Ans+=ans;
            
        m[{x,y}]=t;
    }
    cout<<Ans<<endl;
}
accuracy: 1
time_ms: 90
memory_kb: 9688
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) a.begin(), a.end()

vector<string> rotate(vector<string> s)
{
    int n = s.size();
    vector<vector<char>> res(s.size(), vector<char>(s.size()));
    rep(i, n)
    {
        rep(j, n)
        {
            res[j][n - i - 1] = s[i][j];
        }
    }
    vector<string> p;
    for (auto x : res)
    {
        string q;
        for (auto y : x)
        {
            q += y;
        }
        p.push_back(q);
    }
    return p;
}

int main()
{
    ll w, d, h, c;
    cin >> w >> d >> h >> c;
    map<pair<ll, ll>, ll> mp;
    ll ans = 2 * (w * h + h * d + d * w);
    rep(i, c)
    {
        ll x, y, z;
        cin >> x >> y >> z;
        if (mp.count({x, y}) == 0)
        {
            mp[{x, y}] = h;
        }
        if (x != 0 && mp[{x - 1, y}] == 0)
        {
            mp[{x - 1, y}] = h;
        }
        if (x != w - 1 && mp[{x + 1, y}] == 0)
        {
            mp[{x + 1, y}] = h;
        }
        if (y != 0 && mp[{x, y - 1}] == 0)
        {
            mp[{x, y - 1}] = h;
        }
        if (y != d - 1 && mp[{x, y + 1}] == 0)
        {
            mp[{x, y + 1}] = h;
        }
        // c-h_2+h_1
        // ans += c + mp[{x - 1, y}] - mp[{x, y}];
        // ans += c + mp[{x + 1, y}] - mp[{x, y}];
        // ans += c + mp[{x, y - 1}] - mp[{x, y}];
        // ans += c + mp[{x, y + 1}] - mp[{x, y}];
        ans += abs(mp[{x, y}] - z - mp[{x - 1, y}]) - abs(mp[{x, y}] - mp[{x - 1, y}]);
        ans += abs(mp[{x, y}] - z - mp[{x + 1, y}]) - abs(mp[{x, y}] - mp[{x + 1, y}]);
        ans += abs(mp[{x, y}] - z - mp[{x, y - 1}]) - abs(mp[{x, y}] - mp[{x, y - 1}]);
        ans += abs(mp[{x, y}] - z - mp[{x, y + 1}]) - abs(mp[{x, y}] - mp[{x, y + 1}]);
        mp[{x, y}] -= z;
        // cout << ans << endl;
    }
    // for (auto x : mp)
    //     cout << x.first.first << " " << x.first.second << "_" << x.second << endl;
    cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 340
memory_kb: 34816
final_rank: 11

code_snippet: //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
using ll = long long;
const ll INF32 = 2e9;
const ll INF64 = 4e18;
#define int long long

void printYN(bool ok){
    if(ok)cout << "Yes" << endl;
    else cout << "No" << endl;
    return;
}

void rorate(int type, vector<string> &P){
    int N = P.size();
    vector<string> res(N);
    if(type==1){
        rep(i, N)rep(j, N){
            res[i].push_back(P[N-j-1][i]);
        }
    }
    else if(type==2){
        rep(i, N)rep(j, N){
            res[i].push_back(P[N-i-1][N-1-j]);
        }
    }
    else if(type==3){
        rep(i, N)rep(j, N){
            res[i].push_back(P[j][N-1-i]);
        }
    } else {
        res = P;
    }
    P = res;
    return;
}

signed main(){
    int W, D, H, C;
    cin >> W >> D >> H >> C;
    map<pair<int, int>, int> height;
    int now = H*W*2+W*D*2+D*H*2;
    const vector<int> dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};
    rep(i, C){
        int x, y, z;
        cin >> x >> y >> z;
        int nowh = H;
        if(height.count({x,y}))nowh = height[{x,y}];
        int nxth = nowh-z;
        rep(j, dx.size()){
            int nx = x + dx[j], ny = y + dy[j];
            if(nx==-1||ny==-1||nx==W||ny==D){
                now-=nowh;
                now+=nxth;
            }
            else {
                if(!height.count({nx, ny}))height[{nx,ny}] = H;
                now-=abs(nowh-height[{nx,ny}]);
                now+=abs(nxth-height[{nx, ny}]);
            }
        }
        height[{x,y}] = nxth;
    }
    cout << now << endl;
    return 0;
}
accuracy: 1
time_ms: 250
memory_kb: 34584
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ld=long double;
#define fi first
#define se second
#define i128 __int128_t
#define all(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define si(a) (long)(a.size())
template<typename T>bool chmin(T&a,T b){return b<a?(a=b,1):0;}
template<typename T>bool chmax(T&a,T b){return b>a?(a=b,1):0;}
const int INF=1e9+100;
const long INFL=3e18+100;
struct _ {
   _() { cin.tie(0)->sync_with_stdio(0), cout.tie(0); }
} __;

int main(){
    long W,H,D,C;
    cin>>W>>H>>D>>C;

    long now=(H+W)*D*2+H*W*2;
    map<pair<int,int>,int> ma;
    rep(_,C){
        int x,y,z;
        cin>>x>>y>>z;
        if(!ma.count({y,x})) ma[{y,x}]=D;

        vector<int> dd={0,1,0,-1,0};

        rep(i,4){
            int ny=y+dd[i];
            int nx=x+dd[i+1];
            if(ny<0||H<=ny||nx<0||W<=nx){
                now-=z;
            }
            else{
                if(!ma.count({ny,nx})) ma[{ny,nx}]=D;
                int h=ma[{y,x}];
                now-=abs(ma[{ny,nx}]-h);
                h-=z;
                now+=abs(ma[{ny,nx}]-h);
            }
        }
        ma[{y,x}]-=z;
    }
    cout<<now<<"\n";
}
accuracy: 1
time_ms: 250
memory_kb: 34816
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ll long long
const long long mod=998244353;
const long long hmod=46216567629137;
int main(){
    cin.tie(0)->sync_with_stdio(0);
    cout.tie(0);
    ll W,D,H,C;
    cin>>W>>D>>H>>C;
    ll x[C+1],y[C+1],z[C+1];
    map<ll,ll>M1[W+1],M2[D+1];
    for(int i=1;i<=C;i++){
        cin>>x[i]>>y[i]>>z[i];
        if(M1[x[i]].count(y[i])>=1) M1[x[i]][y[i]]+=z[i];
        else M1[x[i]][y[i]]=z[i];
        if(M2[y[i]].count(x[i])>=1) M2[y[i]][x[i]]+=z[i];
        else M2[y[i]][x[i]]=z[i];
    }
    ll ans=0;
    for(int i=0;i<W;i++){
        ll last=H;
        int last_pos=-2;
        for(auto p:M1[i]){
            ans+=p.second*2;
            if(last_pos+1==p.first){
                ans-=min(last,p.second)*2;
            }
            if(p.first==0 || p.first==D-1){
                ans-=p.second;
            }
            last_pos=p.first;
            last=p.second;
        }
    }
    for(int i=0;i<D;i++){
        ll last=H;
        int last_pos=-2;
        for(auto p:M2[i]){
            ans+=p.second*2;
            if(last_pos+1==p.first){
                ans-=min(last,p.second)*2;
            }
            if(p.first==0 || p.first==W-1){
                ans-=p.second;
            }
            last_pos=p.first;
            last=p.second;
        }
    }
    ans+=D*W*2;
    ll high1[W],high2[W],high3[D],high4[D];
    rep(i,W){
        high1[i]=H;
        high2[i]=H;//最後
    }
    rep(i,D){
        high3[i]=H;
        high4[i]=H;//最後
    }
    for(int i=1;i<=C;i++){
        if(y[i]==0){
            high1[x[i]]-=z[i];
        }
        if(y[i]==D-1){
            high2[x[i]]-=z[i];
        }
        if(x[i]==0){
            high3[y[i]]-=z[i];
        }
        if(x[i]==W-1){
            high4[y[i]]-=z[i];
        }
    }
    rep(i,W){
        ans+=high1[i]+high2[i];
    }
    rep(i,D){
        ans+=high3[i]+high4[i];
    }
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 40
memory_kb: 30780
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define oke cout << "Yes" << '\n';
#define dame cout << "No" << '\n';
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using HaiI = vector<vector<int>>;
using Hai2 = vector<vector<ll>>;
using HaiB = vector<vector<bool>>;
using Hai3 = vector<vector<vector<ll>>>;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout << fixed << setprecision(15);
	vector<int>gox={1,0,-1,0};
	vector<int>goy={0,1,0,-1};
	ll W,D,H,C;
	cin>>W>>D>>H>>C;
	map<pair<int,int>,int>L;
	ll wa=(W*D*2)+(W*H*2)+(D*H*2);
	rep(i,C){
		int x,y,z;
		cin>>x>>y>>z;
		if(!L.count({x,y})){
			L[{x,y}]=0;
		}
		if((x==0||x==W-1) && (y==0||y==D-1)){
			wa-=z*2;
		}else if((x==0||x==W-1) || (y==0||y==D-1)){
			wa-=z;
		}
		rep(j,4){
			int xx=x+gox[j],yy=y+goy[j];
			if(xx<0 || xx>=W || yy<0 || yy>=D){//蛇足かも
				continue;
			}

			if(!L.count({xx,yy})){
				wa+=z;
				continue;
			}
			if(L[{xx,yy}]>L[{x,y}]){
				wa-=z;
				if(L[{xx,yy}]<L[{x,y}]+z){
					ll Z=(z+L[{x,y}])-L[{xx,yy}];
					wa+=2*Z;
				}
			}else{
				wa+=z;
			}
		}
		L[{x,y}]+=z;
		if(L[{x,y}]==H){
			wa-=2;
		}
	}
	cout<<wa<<"\n";
}
accuracy: 1
time_ms: 80
memory_kb: 9668
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define oke cout << "Yes" << '\n';
#define dame cout << "No" << '\n';
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using HaiI = vector<vector<int>>;
using Hai2 = vector<vector<ll>>;
using HaiB = vector<vector<bool>>;
using Hai3 = vector<vector<vector<ll>>>;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout << fixed << setprecision(15);
	vector<int>gox={1,0,-1,0};
	vector<int>goy={0,1,0,-1};
	ll W,D,H,C;
	cin>>W>>D>>H>>C;
	map<pair<int,int>,int>L;
	ll wa=(W*D*2)+(W*H*2)+(D*H*2);
	rep(i,C){
		int x,y,z;
		cin>>x>>y>>z;
		if(!L.count({x,y})){
			L[{x,y}]=0;
		}
		if((x==0||x==W-1) && (y==0||y==D-1)){
			L[{x,y}]+=z;
			continue;
		}else if((x==0||x==W-1) || (y==0||y==D-1)){
			wa-=z;
		}
		rep(j,4){
			int xx=x+gox[j],yy=y+goy[j];
			if(xx<0 || xx>=W || yy<0 || yy>=D){//蛇足かも
				continue;
			}

			if(!L.count({xx,yy})){
				wa+=z;
				continue;
			}
			if(L[{xx,yy}]>L[{x,y}]){
				wa-=z;
				if(L[{xx,yy}]<L[{x,y}]+z){
					ll Z=(z+L[{x,y}])-L[{xx,yy}];
					wa+=2*Z;
				}
			}else{
				wa+=z;
			}
		}
		L[{x,y}]+=z;
		if(L[{x,y}]==H){
			wa-=2;
		}
	}
	cout<<wa<<"\n";
}
accuracy: 0.5
time_ms: 80
memory_kb: 9712
final_rank: 14

code_snippet: #include <bits/stdc++.h>
#include <iomanip>
#include <numbers>
#include <cassert>
#include <numeric>
#ifdef _MSC_VER
#include <intrin.h>
#endif
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pint = pair<int, int>;
template<typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
template<typename T>
bool chmax(T & a, const T & b) { if (a < b) { a = b; return true; } return false; }
template<typename T>
bool chmin(T & a, const T & b) { if (a > b) { a = b; return true; } return false; }
const int inf = 1e9;
const ll linf = 1e18;
const int dy[] = { 0,0,1,-1 }, dx[] = { 1,-1,0,0 };
int main() {
    ll w, d, h; int c; cin >> w >> d >> h >> c;
    ll ans = (w * d + w * h + d * h) * 2;
    map<pint, int> mp;
    rep(i, c) {
        int x, y, z; cin >> x >> y >> z;
        if (!mp.count({ x,y }))mp[{x, y}] = h;
        rep(j, 4) {
            int nx = x + dx[j], ny = y + dy[j];
            if (!mp.count({ nx,ny })) {
                if (nx < 0 || ny < 0 || nx >= w || ny >= d)mp[{nx, ny}] = 0;
                else mp[{nx, ny}] = h;
            }
            int now = mp[{x, y}];
            if (mp[{nx, ny}] < now) ans -= min(z, now - mp[{nx, ny}]);
            now -= z;
            if(mp[{nx, ny}] >= now)ans += min(z, mp[{nx, ny}] - now);
        }
        mp[{x, y}] -= z;
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 300
memory_kb: 34816
final_rank: 10

code_snippet: #include <bits/stdc++.h>
#include <iomanip>
#include <numbers>
#include <cassert>
#include <numeric>
#ifdef _MSC_VER
#include <intrin.h>
#endif
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pint = pair<int, int>;
template<typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
template<typename T>
bool chmax(T & a, const T & b) { if (a < b) { a = b; return true; } return false; }
template<typename T>
bool chmin(T & a, const T & b) { if (a > b) { a = b; return true; } return false; }
const int inf = 1e9;
const ll linf = 1e18;
const int dy[] = { 0,0,1,-1 }, dx[] = { 1,-1,0,0 };
int main() {
    ll w, d, h; int c; cin >> w >> d >> h >> c;
    ll ans = (w * d + w * h + d * h) * 2;
    map<pint, int> mp;
    rep(i, c) {
        int x, y, z; cin >> x >> y >> z;
        if (!mp.count({ x,y }))mp[{x, y}] = h;
        rep(j, 4) {
            int nx = x + dx[j], ny = y + dy[j];
            if (!mp.count({ nx,ny })) {
                if (nx < 0 || ny < 0 || nx >= w || ny >= h)mp[{nx, ny}] = 0;
                else mp[{nx, ny}] = h;
            }
            int now = mp[{x, y}];
            if (mp[{nx, ny}] < now) ans -= min(z, now - mp[{nx, ny}]);
            now -= z;
            if(mp[{nx, ny}] >= now)ans += min(z, mp[{nx, ny}] - now);
        }
        mp[{x, y}] -= z;
    }
    cout << ans << endl;
}
accuracy: 0.5769230769230769
time_ms: 290
memory_kb: 34816
final_rank: 12

code_snippet: #include <bits/stdc++.h>
#include <iomanip>
#include <numbers>
#include <cassert>
#include <numeric>
#ifdef _MSC_VER
#include <intrin.h>
#endif
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pint = pair<int, int>;
template<typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
template<typename T>
bool chmax(T & a, const T & b) { if (a < b) { a = b; return true; } return false; }
template<typename T>
bool chmin(T & a, const T & b) { if (a > b) { a = b; return true; } return false; }
const int inf = 1e9;
const ll linf = 1e18;
const int dy[] = { 0,0,1,-1 }, dx[] = { 1,-1,0,0 };
int main() {
    ll w, d, h; int c; cin >> w >> d >> h >> c;
    ll ans = (w * d + w * h + d * h) * 2;
    map<pint, int> mp;
    rep(i, c) {
        int x, y, z; cin >> x >> y >> z;
        if (!mp.count({ x,y }))mp[{x, y}] = h;
        rep(j, 4) {
            int nx = x + dx[j], ny = y + dy[j];
            if (!mp.count({ nx,ny })) {
                if (nx < 0 || ny < 0 || nx >= w || ny >= h)mp[{nx, ny}] = 0;
                else mp[{nx, ny}] = h;
            }
            int now = mp[{x, y}];
            if (mp[{nx, ny}] < now) ans -= min(z, now - mp[{nx, ny}]);
            now -= z;
            if(mp[{nx, ny}] >= now)ans += mp[{nx, ny}] - now;
        }
        mp[{x, y}] -= z;
    }
    cout << ans << endl;
}
accuracy: 0.23076923076923078
time_ms: 280
memory_kb: 34816
final_rank: 17

code_snippet: #include <bits/stdc++.h>
#include <iomanip>
#include <numbers>
#include <cassert>
#include <numeric>
#ifdef _MSC_VER
#include <intrin.h>
#endif
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pint = pair<int, int>;
template<typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
template<typename T>
bool chmax(T & a, const T & b) { if (a < b) { a = b; return true; } return false; }
template<typename T>
bool chmin(T & a, const T & b) { if (a > b) { a = b; return true; } return false; }
const int inf = 1e9;
const ll linf = 1e18;
const int dy[] = { 0,0,1,-1 }, dx[] = { 1,-1,0,0 };
int main() {
    ll w, d, h; int c; cin >> w >> d >> h >> c;
    ll ans = (w * d + w * h + d * h) * 2;
    map<pint, int> mp;
    rep(i, c) {
        int x, y, z; cin >> x >> y >> z;
        if (!mp.count({ x,y }))mp[{x, y}] = h;
        rep(j, 4) {
            int nx = x + dx[j], ny = y + dy[j], nz = z;
            if (!mp.count({ nx,ny })) {
                if (nx < 0 || ny < 0 || nx >= w || ny >= h)mp[{nx, ny}] = 0;
                else mp[{nx, ny}] = h;
            }
            int now = mp[{x, y}];
            if (mp[{nx, ny}] < now) {
                int sub = min(nz, now - mp[{nx, ny}]);
                now -= sub;
                ans -= sub;
                nz -= sub;
                if (nz == 0) continue;
            }
            now -= nz;
            ans += mp[{nx, ny}] - now;
        }
        mp[{x, y}] -= z;
    }
    cout << ans << endl;
}
accuracy: 0.23076923076923078
time_ms: 260
memory_kb: 34816
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)
#define all(a) (a).begin(),(a).end()

vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    
    ll W, D, H, Q;
    cin >> W >> D >> H >> Q;
    vector<ll> x(Q), y(Q), z(Q);
    rep(i, 0, Q) cin >> x[i] >> y[i] >> z[i];
    
    map<pair<ll, ll>, ll> height;
    ll ans = 2 * (W*D + D*H + H*W);
    rep(i, 0, Q) {
        ll A;
        if (height.count({x[i], y[i]})) A = height[{x[i], y[i]}];
        else A = H;
        
        rep(dir, 0, 4) {
            int nx = x[i] + dx[dir];
            int ny = y[i] + dy[dir];
            
            if (nx == -1 || nx == W || ny == -1 || ny == D) {
                ans -= z[i];
            } else {
                ll B;
                if (height.count({nx, ny})) B = height[{nx, ny}];
                else B = H;
                
                ans -= abs(A - B);
                ans += abs(A - z[i] - B);
            }
        }
        
        height[{x[i], y[i]}] = A - z[i];
    }
    
    cout << ans << '\n';
}
accuracy: 1
time_ms: 60
memory_kb: 11792
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 1LL<<60
int main(){
   
   ll w,d,h,c,ans=0;cin>>w>>d>>h>>c;
   map<pair<ll,ll>,ll> ma;
   vector<ll> f={0,1,0,-1,0,0};
   ans=2*w*d+2*w*h+2*d*h;
   for(ll i=0;i<c;i++){
    ll x,y,z;cin>>x>>y>>z;x++,y++;
    ma[{x,y}]+=z;
   }
   for(auto& i:ma){
    auto [x,y]=i.first;
    ll z=i.second;
    for(ll j=0;j<4;j++){
        ll nx=x+f[j],ny=y+f[j+1];
        if(nx==0||ny==0||nx==w+1||ny==d+1)ans-=z;
        else if(ma.find({nx,ny})==ma.end())ans+=z;
        else if(ma[{nx,ny}]<z)ans+=z-ma[{nx,ny}];
    }
   }
   cout<<ans<<endl;
}
accuracy: 1
time_ms: 80
memory_kb: 9692
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 1LL<<60
int main(){
   
   ll w,d,h,c,ans=0;cin>>w>>d>>h>>c;
   map<pair<ll,ll>,ll> ma;
   vector<ll> f={1,0,-1,0,1};
   ans=2*w*d+2*w*h+2*d*h;
   for(ll i=0;i<c;i++){
    ll x,y,z;cin>>x>>y>>z;x++,y++;
    ma[{x,y}]=max(ma[{x,y}],z);
   }
   for(auto& i:ma){
    auto [x,y]=i.first;
    ll z=i.second;
    for(ll j=0;j<4;j++){
        ll nx=x+f[j],ny=y+f[j+1];
        if(nx==0||ny==0||nx==w+1||ny==d+1)ans-=z;
        else if(ma.find({nx,ny})==ma.end())ans+=z;
        else if(ma[{nx,ny}]<z)ans+=z-ma[{nx,ny}];
    }
   }
   cout<<ans<<endl;
}
accuracy: 0.23076923076923078
time_ms: 90
memory_kb: 9700
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 1LL<<60
int main(){
   
   ll w,d,h,c,ans=0;cin>>w>>d>>h>>c;
   map<pair<ll,ll>,ll> ma,mf;
   for(ll i=0;i<=w+1;i++)ma[{i,0}]=ma[{i,d+1}]=h;
   for(ll i=0;i<=d+1;i++)ma[{0,i}]=ma[{w+1,i}]=h;
   ans=2*w*d+h*(w+d)*2;
   for(ll i=0;i<c;i++){
    ll x,y,z;cin>>x>>y>>z;x++,y++;
    mf[{x,y}]=max(mf[{x,y}],z);
   }
   for(auto ass:mf){
    auto [x,y]=ass.first;
    auto z=ass.second;
    ans-=2*min({ma[{x-1,y}],ma[{x+1,y}],z});
    if(z>max(ma[{x-1,y}],ma[{x+1,y}]))ans+=2*(z-max(ma[{x-1,y}],ma[{x+1,y}]));

    ans-=2*min({ma[{x,y-1}],ma[{x,y+1}],z});
    if(z>max(ma[{x,y-1}],ma[{x,y+1}]))ans+=2*(z-max(ma[{x,y-1}],ma[{x,y+1}]));
    ma[{x,y}]=z;
    // cout<<x<<" "<<y<<" "<<z<<" "<<ans<<endl;
   }
   cout<<ans<<endl;
}
accuracy: 0.23076923076923078
time_ms: 340
memory_kb: 65876
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 1LL<<60
#define B 'B'
#define R 'R'
#define G 'G'
#define Y 'Y'
int main(){
   
   ll w,d,h,c,ans=0;cin>>w>>d>>h>>c;
   map<pair<ll,ll>,ll> ma,mf;
   vector<ll> f={1,0,-1,0,1};
   for(ll i=0;i<=w+1;i++)ma[{i,0}]=ma[{i,d+1}]=h;
   for(ll i=0;i<=d+1;i++)ma[{0,i}]=ma[{w+1,i}]=h;
   ans=2*w*d+h*(w+d)*2;
   for(ll i=0;i<c;i++){
    ll x,y,z;cin>>x>>y>>z;x++,y++;
    mf[{x,y}]=max(mf[{x,y}],z);
   }
   for(auto ass:mf){
    auto [x,y]=ass.first;
    auto z=ass.second;
    if(z==h)ans-=2;
    ans-=2*min({ma[{x-1,y}],ma[{x+1,y}],z});
    if(z>max(ma[{x-1,y}],ma[{x+1,y}]))ans+=2*(z-max(ma[{x-1,y}],ma[{x+1,y}]));

    ans-=2*min({ma[{x,y-1}],ma[{x,y+1}],z});
    if(z>max(ma[{x,y-1}],ma[{x,y+1}]))ans+=2*(z-max(ma[{x,y-1}],ma[{x,y+1}]));
    ma[{x,y}]=z;
    // cout<<x<<" "<<y<<" "<<z<<" "<<ans<<endl;
   }
   cout<<ans<<endl;
}
accuracy: 0.23076923076923078
time_ms: 340
memory_kb: 65984
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 1LL<<60
#define B 'B'
#define R 'R'
#define G 'G'
#define Y 'Y'
int main(){
   
   ll w,d,h,c,ans=0;cin>>w>>d>>h>>c;
   map<pair<ll,ll>,ll> ma,mf;
   vector<ll> f={1,0,-1,0,1};
   for(ll i=0;i<=w+2;i++)ma[{i,0}]=ma[{i,d+2}]=h;
   for(ll i=0;i<=d+2;i++)ma[{0,i}]=ma[{w+2,i}]=h;
   ans=2*w*d+h*(w+d)*2;
   for(ll i=0;i<c;i++){
    ll x,y,z;cin>>x>>y>>z;x++,y++;
    mf[{x,y}]=max(mf[{x,y}],z);
   }
//    cout<<ans<<endl;
   for(auto ass:mf){
    auto [x,y]=ass.first;
    auto z=ass.second;
    if(z==h)ans-=2;
    if(x==w){
        ans-=2*min(ma[{x-1,y}],z);
        if(ma[{x-1,y}]<z)ans+=(z-ma[{x-1,y}]);
    }else if(x!=1){
        if(ma[{x-1,y}]<z)ans+=2*(z-ma[{x-1,y}]);
    }
    if(y==d){
        ans-=2*min(ma[{x,y-1}],z);
        if(ma[{x,y-1}]<z)ans+=(z-ma[{x,y-1}]);
    }else if(y!=1){
        if(ma[{x,y-1}]<z)ans+=2*(z-ma[{x,y-1}]);
    }
    ma[{x,y}]=z;
    // cout<<x<<" "<<y<<" "<<z<<" "<<ans<<endl;
   }
   cout<<ans<<endl;
}
accuracy: 0.11538461538461539
time_ms: 220
memory_kb: 53328
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int main(){
	int W,D,H,C;
	cin >> W >> D >> H >> C;
	map<pair<int,int>,int> data;
	for(;C--;){
		int x,y,z;
		cin >> x >> y >> z;
		data[make_pair(x,y)]+=z;
	}
	long long ans=(long long)W*D+(long long)D*H+(long long)H*W;
	ans*=2;
	for(auto[p,s] : data){
		for(int i=0;i<4;i++){
			pair<int,int> q=make_pair(p.first+dx[i],p.second+dy[i]);
			if(data.count(q)){
				if(i>=2) continue;
				ans+=abs(s-data[q]);
				continue;
			}
			if(q.first<0 || q.first>=W || q.second<0 || q.second>=D){
				ans-=s;
				continue;
			}
			ans+=s;
		}
	}
	cout << ans << '\n';
}
accuracy: 1
time_ms: 80
memory_kb: 9696
final_rank: 4