結果

問題 No.755 Zero-Sum Rectangle
ユーザー tanimani364tanimani364
提出日時 2020-06-29 20:28:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 123 ms / 2,000 ms
コード長 2,025 bytes
コンパイル時間 2,485 ms
コンパイル使用メモリ 205,196 KB
実行使用メモリ 8,008 KB
最終ジャッジ日時 2023-09-26 11:19:59
合計ジャッジ時間 7,061 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,008 KB
testcase_01 AC 87 ms
4,380 KB
testcase_02 AC 69 ms
4,376 KB
testcase_03 AC 93 ms
4,380 KB
testcase_04 AC 106 ms
4,376 KB
testcase_05 AC 116 ms
4,380 KB
testcase_06 AC 76 ms
4,380 KB
testcase_07 AC 70 ms
4,380 KB
testcase_08 AC 95 ms
4,384 KB
testcase_09 AC 107 ms
4,376 KB
testcase_10 AC 123 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
evil_1 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define popcount __builtin_popcount
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;

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;
}

ll gcd(ll n, ll m)
{
	ll tmp;
	while (m != 0)
	{
		tmp = n % m;
		n = m;
		m = tmp;
	}
	return n;
}

ll lcm(ll n, ll m)
{
	return abs(n) / gcd(n, m) * abs(m); //gl=xy
}

using namespace std;

template<typename T>
struct Sum_2D{
  vector<vector<T>>cul;
  bool flag;
  Sum_2D(int h,int w):cul(h+1,vector<T>(w+1)){
    flag=false;
  }

  void add(int x,int y,T val){//0インデックス
    ++x;++y;
    if(x>=cul.size()||y>=cul[0].size())return;
    cul[x][y]+=val;
  }

  void build(){
    flag=true;
    for(int i=1;i<cul.size();++i){
      for(int j=1;j<cul[i].size();++j){
        cul[i][j]+=cul[i][j-1]+cul[i-1][j]-cul[i-1][j-1];
      }
    }
  }

  T query(int lb_x,int lb_y,int ub_x,int ub_y){
    //lb_x,lb_y:左下(含む),ub_x,ub_y:右上(含まない)
    //0インデックスに直す
    assert(flag==true);
    return cul[ub_x][ub_y]-cul[ub_x][lb_y]-cul[lb_x][ub_y]+cul[lb_x][lb_y];
  }
};

void solve()
{
	int n,m;
	cin>>n>>m;
	Sum_2D<ll>sum(m,m);
	rep(i,m)rep(j,m){
		ll a;
		cin>>a;
		sum.add(i,j,a);
	}
	sum.build();
	rep(i,n){
		int x,y;
		cin>>x>>y;
		x--;y--;
		ll ans=0;
		REP(o,x+1,m+1)rep(p,x+1)rep(q,y+1)REP(r,y+1,m+1)if(sum.query(o,q,p,r)==0)++ans;
		cout<<ans<<"\n";
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(15);
	solve();
	return 0;
}
0