utsubo’s blog

競技プログラミングとか.

yukicoder No.60 魔法少女

感想

二次元imos法で各座標に加わるダメージを計算する。
いもす法 - いもす研 (imos laboratory)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9+7;

int main(void) {
	int N,K;
	cin >> N >> K;
	vector<vector<ll>> enemys(1510,vector<ll>(1510));
	vector<vector<ll>> area(1510,vector<ll>(1510));
	const int GETA = 500;
	for(int i=0;i<N;i++){
		int x,y,hp;cin >> x >> y >> hp;
		enemys[y+GETA][x+GETA] += hp;
	}
	for(int i=0;i<K;i++){
		int x,y,w,h,d;cin >> x >> y >> w >> h >> d;
		y += GETA; x += GETA;
		area[y][x] += d;
		area[y+h+1][x+w+1] += d;
		area[y+h+1][x] -= d;
		area[y][x+w+1] -= d;
	}
	for(int i=0;i<1510;i++){
		for(int j=0;j<1510-1;j++){
			area[i][j+1] += area[i][j];
		}
	}
	for(int i=0;i<1510;i++){
		for(int j=0;j<1510-1;j++){
			area[j+1][i] += area[j][i];
		}
	}

	ll ret = 0;
	for(int i=0;i<1510;i++){
		for(int j=0;j<1510;j++){
			ret += max(0LL,enemys[i][j] - area[i][j]);
		}
	}
	cout << ret << endl;
	return 0;
}

imos法好き。