結果

問題 No.755 Zero-Sum Rectangle
ユーザー gazellegazelle
提出日時 2018-12-03 00:59:17
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 138 ms / 2,000 ms
コード長 2,014 bytes
コンパイル時間 1,286 ms
コンパイル使用メモリ 128,640 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-13 21:02:09
合計ジャッジ時間 6,270 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 100 ms
4,376 KB
testcase_02 AC 78 ms
4,376 KB
testcase_03 AC 110 ms
4,376 KB
testcase_04 AC 122 ms
4,380 KB
testcase_05 AC 132 ms
4,376 KB
testcase_06 AC 87 ms
4,376 KB
testcase_07 AC 81 ms
4,376 KB
testcase_08 AC 111 ms
4,376 KB
testcase_09 AC 128 ms
4,376 KB
testcase_10 AC 138 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
evil_1 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<unordered_map>
#include<queue>
#include<iomanip>
#include<math.h>
#include<bitset>
#include<cassert>
#include<random>
#include<time.h>
using namespace std;
using ll=long long;
using ld=long double;
using P=pair<int,int>;
#define MOD 1000000007LL
#define INF 1000000000LL
#define EPS 1e-10
#define FOR(i,n,m) for(ll i=n;i<(ll)m;i++)
#define REP(i,n) FOR(i,0,n)
#define DUMP(a) REP(d,a.size()){cout<<a[d];if(d!=a.size()-1)cout<<" ";else cout<<endl;}
#define ALL(v) v.begin(),v.end()
#define UNIQUE(v)  sort(ALL(v));v.erase(unique(ALL(v)),v.end());
#define pb push_back

/* --------------------------------------- */
class AccumulSum {
public:
	AccumulSum(int h,int w,vector<vector<ll> >& a) {
		_h=h;
		_w=w;
		_sum.resize(_h+1);
		for(int i=0; i<_h+1; ++i) {
			_sum[i].resize(_w+1);
			for(int j=0; j<_w+1; ++j) {
				if(i==0||j==0) _sum[i][j]=0;
				else _sum[i][j]=a[i-1][j-1];
			}
		}
		for(int i=0; i<_h+1; ++i) {
			for(int j=1; j<_w+1; ++j) _sum[i][j]+=_sum[i][j-1];
		}
		for(int i=0; i<_w+1; ++i) {
			for(int j=1; j<_h+1; ++j) _sum[j][i]+=_sum[j-1][i];
		}
	}
	// the coordinates of ul and dr should be 0-indexed
	long long query(pair<int,int> ul,pair<int,int> dr) {
		ll ret=0;
		ret+=_sum[dr.first+1][dr.second+1];
		ret-=_sum[dr.first+1][ul.second];
		ret-=_sum[ul.first][dr.second+1];
		ret+=_sum[ul.first][ul.second];
		return ret;
	}
private:
	int _h;
	int _w;
	vector<vector<ll> > _sum;
};

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	ll n, m;
	cin >> n >> m;
	vector<vector<ll>> a(m,vector<ll>(m));
	REP(i,m) REP(j,m) cin >> a[i][j];
	AccumulSum acm(m, m, a);
	REP(query,n) {
		ll x, y;
		cin >> x >> y;
		x--;
		y--;
		ll ans = 0;
		for(ll i = 0; i <= x; i++) for(ll j = 0; j <= y; j++) {
			for(ll k = x; k < m; k++) for(ll l = y; l < m; l++) {
				if(acm.query(P(i, j), P(k, l)) == 0) ans++;
			}
		}
		cout << ans << endl;
	}
	return 0;
}

/* --------------------------------------- */
0