結果

問題 No.755 Zero-Sum Rectangle
ユーザー FF256grhyFF256grhy
提出日時 2018-12-03 01:15:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 437 ms / 2,000 ms
コード長 2,815 bytes
コンパイル時間 1,698 ms
コンパイル使用メモリ 170,700 KB
実行使用メモリ 8,760 KB
最終ジャッジ日時 2023-09-13 21:02:57
合計ジャッジ時間 8,766 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,760 KB
testcase_01 AC 307 ms
4,376 KB
testcase_02 AC 236 ms
4,376 KB
testcase_03 AC 332 ms
4,376 KB
testcase_04 AC 381 ms
4,380 KB
testcase_05 AC 405 ms
4,376 KB
testcase_06 AC 265 ms
4,380 KB
testcase_07 AC 247 ms
4,380 KB
testcase_08 AC 337 ms
4,376 KB
testcase_09 AC 383 ms
4,380 KB
testcase_10 AC 437 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
evil_1 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long   signed int LL;
typedef long long unsigned int LU;

#define incID(i, l, r) for(int i = (l)    ; i <  (r); i++)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); i++)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); i--)
#define  inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define  dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)

#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))

#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define PQ priority_queue

#define  ALL(v)  v.begin(),  v.end()
#define RALL(v) v.rbegin(), v.rend()
#define  FOR(it, v) for(auto it =  v.begin(); it !=  v.end(); ++it)
#define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it)

template<typename T> bool   setmin(T & a, T b) { if(b <  a) { a = b; return true; } else { return false; } }
template<typename T> bool   setmax(T & a, T b) { if(b >  a) { a = b; return true; } else { return false; } }
template<typename T> bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } }
template<typename T> bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// ---- ----

template<typename T> class Sum2 {
private:
	vector<vector<T>> a, s;
	int I, J;
	bool ok;
public:
	Sum2() { I = J = -1; }
	Sum2(int i, int j) { init(i, j); }
	void init(int i, int j) {
		I = i;
		J = j;
		a = vector<vector<T>>(I + 0, vector<T>(J + 0, 0));
		s = vector<vector<T>>(I + 1, vector<T>(J + 1, 0));
	}
	vector<T> & operator[](int i) {
		assert(inID(i, 0, I));
		ok = false;
		return a[i];
	}
	void calc() {
		inc(i, I) {
		inc(j, J) {
			s[i + 1][j + 1] = a[i][j] + s[i + 1][j] + s[i][j + 1] - s[i][j];
		}
		}
		ok = true;
	}
	T sum(int il, int ih, int jl, int jh) { // [il, ih) × [jl, jh)
		assert(ok);
		assert(inII(il, 0, I));
		assert(inII(ih, 0, I));
		assert(inII(jl, 0, J));
		assert(inII(jh, 0, J));
		assert(il <= ih);
		assert(jl <= jh);
		return s[ih][jh] - s[il][jh] - s[ih][jl] + s[il][jl];
	}
	const vector<vector<T>> & get_a() { return a; }
	const vector<vector<T>> & get_s() { return s; }
};

// ----

int main() {
	int n, m;
	cin >> n >> m;
	
	Sum2<LL> s(m, m);
	inc(i, m) {
	inc(j, m) {
		cin >> s[i][j];
	}
	}
	s.calc();
	
	inc(q, n) {
		int i, j;
		cin >> i >> j;
		
		int ans = 0;
		incID(il, 0, i) { incII(ih, i, m) {
		incID(jl, 0, j) { incII(jh, j, m) {
			if(s.sum(il, ih, jl, jh) == 0) { ans++; }
		} }
		} }
		
		cout << ans << endl;
	}
	
	return 0;
}
0