結果

問題 No.755 Zero-Sum Rectangle
ユーザー tonegawatonegawa
提出日時 2020-08-17 14:10:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 269 ms / 2,000 ms
コード長 2,758 bytes
コンパイル時間 1,794 ms
コンパイル使用メモリ 134,756 KB
実行使用メモリ 9,088 KB
最終ジャッジ日時 2024-04-19 18:51:20
合計ジャッジ時間 7,485 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
9,088 KB
testcase_01 AC 192 ms
5,376 KB
testcase_02 AC 147 ms
5,376 KB
testcase_03 AC 210 ms
5,376 KB
testcase_04 AC 237 ms
5,376 KB
testcase_05 AC 260 ms
5,376 KB
testcase_06 AC 167 ms
5,376 KB
testcase_07 AC 155 ms
5,376 KB
testcase_08 AC 208 ms
5,376 KB
testcase_09 AC 244 ms
5,376 KB
testcase_10 AC 269 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
evil_1 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define re(c, b) for(ll c=0;c<b;c++)
#define rep(a,b,c) for(ll a=b;a<c;a++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

void get(vll &a){re(i,a.size()) scanf("%lld",&a[i]);}
void get(vvl &a){re(i,a.size()) re(j,a[0].size()) scanf("%lld",&a[i][j]);}
void print(vll &a){re(i,a.size()) cout<<a[i]<<(i==a.size()-1?"\n":" ");}
void print(vvl &a){re(i,a.size()) re(j,a[0].size())cout<<a[i][j]<<(j==a[0].size()-1?"\n":" ");}

struct Accum2D{
  map<ll, ll> x, y;
  ll mode = 1;
  vvl dat;
  vvl v;
  Accum2D(vvl point, ll h=0, ll w=0){
    if(point.size()==0) mode=0;
    for(int i=0;i<point.size();i++){
      if(x.find(point[i][0])==x.end()) x.emplace(point[i][0], 0);
      if(y.find(point[i][1])==y.end()) y.emplace(point[i][1], 0);
    }
    ll k = 0;
    for(auto itr = x.begin();itr!=x.end();itr++) (*itr).second = k, k++;
    k = 0;
    for(auto itr = y.begin();itr!=y.end();itr++) (*itr).second = k, k++;
    if(mode==1) {
      dat.resize(x.size(), vll(y.size(), 0));
      v.resize(x.size(), vll(y.size(), 0));
    }else {
      dat.resize(h+1, vll(w+1, 0));
      v.resize(h+1, vll(w+1, 0));
    }
  }
  void insert(ll a, ll b, ll x){
    dat[a][b] = x;
  }
  void calc(){
    for(int j=0;j<v[0].size();j++){
      for(int i=0;i<v.size();i++){
        v[i][j] = (i==0?0:v[i-1][j]) + dat[i][j];
      }
    }
    for(int i=0;i<v.size();i++){
      for(int j=0;j<v[0].size();j++){
        v[i][j] = v[i][j] + (j==0?0:v[i][j-1]);
      }
    }
  }
  ll get(ll a, ll b, ll c, ll d){
    if(a>c) swap(a, c);
    if(b>d) swap(b, d);
    if(mode==1){
      a = x.at(a), b = y.at(b);
      c = x.at(c), d = y.at(d);
    }
    ll r = v[c][d];
    ll u = (a==0?0:v[a-1][d]);
    ll l = (b==0?0:v[c][b-1]);
    ll o = (a==0||b==0?0:v[a-1][b-1]);
    return r - u - l + o;
  }
};
int main() {
  ll n, m;std::cin >> n >> m;
  vvl d = VV(m, m, 0, ll);
  get(d);
  Accum2D ac(vvl{}, m, m);
  re(i, m) re(j, m) ac.insert(i, j, d[i][j]);
  ac.calc();
  re(t, n){
    ll x, y;std::cin >> x >> y;
    x--, y--;
    ll ans = 0;
    for(int i=0;i<=x;i++){
      for(int j=0;j<=y;j++){
        for(int l=x;l<m;l++){
          for(int r=y;r<m;r++){
            ll s = ac.get(i, j, l, r);
            if(s==0) ans++;
          }
        }
      }
    }
    std::cout << ans << '\n';
  }
}
0