Algorithm-Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub UScuber/Algorithm-Library

:heavy_check_mark: test/aoj/JOI/Planetary-Exploration2.test.cpp

Depends on

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/0560"

#include "../../../template/template.hpp"

#include "../../../SegmentTree/2d-segtree.hpp"

using Data = int;
Data op(const Data &a, const Data &b){ return a + b; }
Data e(){ return 0; }
using Seg2D = SegmentTree2D<Data,op,e>;

const string t = "JOI";
int main(){
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n,m,K;
  cin >> n >> m >> K;
  vector<Seg2D> cnt(3, Seg2D(n, m));
  rep(i, n){
    string s; cin >> s;
    rep(j, m){
      rep(k, 3) if(s[j] == t[k]){
        cnt[k].set(i, j, 1);
        break;
      }
    }
  }
  rep(i, 3) cnt[i].build();
  rep(_, K){
    int a,b,c,d;
    cin >> a >> b >> c >> d;
    a--; b--;
    rep(i, 3) cout << cnt[i].query(a, b, c, d) << " \n"[i == 2];
  }
}
#line 1 "test/aoj/JOI/Planetary-Exploration2.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/0560"

#line 1 "template/template.hpp"
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <bitset>
#include <cctype>
#include <climits>
#include <functional>
#include <cassert>
#include <numeric>
#include <cstring>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define per(i, n) for(int i = (n) - 1; i >= 0; i--)
using ll = long long;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
constexpr int mod = 1000000007;
using namespace std;
template<class T, class U>
bool chmax(T &a, const U &b){ return a < b ? (a = b, 1) : 0; }
template<class T, class U>
bool chmin(T &a, const U &b){ return a > b ? (a = b, 1) : 0; }
#line 4 "test/aoj/JOI/Planetary-Exploration2.test.cpp"

#line 1 "SegmentTree/2d-segtree.hpp"
template <class T, T(*op)(const T&,const T&), T(*e)()>
struct SegmentTree2D {
  int h,w, logh,logw;
  vector<vector<T>> d;
  SegmentTree2D() : SegmentTree2D(0, 0){}
  SegmentTree2D(const int _h, const int _w){
    h = w = 1;
    logh = logw = 1;
    while((h <<= 1) < _h) logh++;
    while((w <<= 1) < _w) logw++;
    d.assign(h * 2, vector<T>(w * 2, e()));
  }
  void set(const int i, const int j, const T x){
    d[i + h][j + w] += x;
  }
  void build(){
    for(int i = 2*h-1; i >= h ; i--){
      for(int j = w - 1; j >= 1; j--)
        updateX(i, j);
    }
    for(int i = h-1; i >= 1; i--){
      for(int j = 2*w-1; j >= 1; j--)
        updateY(i, j);
    }
  }
  void update(int py, int px, const T x){
    assert(0 <= py && py < h);
    assert(0 <= px && px < w);
    py += h, px += w;
    d[py][px] += x;
    for(int j = 1; j <= logw; j++){
      updateX(py, px >> j);
    }
    for(int i = 1; i <= logh; i++){
      for(int j = 0; j <= logw; j++){
        updateY(py >> i, px >> j);
      }
    }
  }
  T get(const int py, const int px){
    assert(0 <= py && py < h);
    assert(0 <= px && px < w);
    return d[py + h][px + w];
  }
  T query(int ly, int lx, int ry, int rx){
    assert(0 <= ly && ly <= ry && ry <= h);
    assert(0 <= lx && lx <= rx && rx <= w);
    T sml = e(), smr = e();
    ly += h;
    ry += h;
    while(ly < ry){
      if(ly & 1) sml = op(sml, query_sub(lx, rx, ly++));
      if(ry & 1) smr = op(query_sub(lx, rx, --ry), smr);
      ly >>= 1;
      ry >>= 1;
    }
    return op(sml, smr);
  }
  private:
  T query_sub(int lx, int rx, const int y){
    T sml = e(), smr = e();
    lx += w;
    rx += w;
    while(lx < rx){
      if(lx & 1) sml = op(sml, d[y][lx++]);
      if(rx & 1) smr = op(d[y][--rx], smr);
      lx >>= 1;
      rx >>= 1;
    }
    return op(sml, smr);
  }
  inline void updateX(const int i, const int j){
    d[i][j] = op(d[i][2*j], d[i][2*j+1]);
  }
  inline void updateY(const int i, const int j){
    d[i][j] = op(d[2*i][j], d[2*i+1][j]);
  }
};
#line 6 "test/aoj/JOI/Planetary-Exploration2.test.cpp"

using Data = int;
Data op(const Data &a, const Data &b){ return a + b; }
Data e(){ return 0; }
using Seg2D = SegmentTree2D<Data,op,e>;

const string t = "JOI";
int main(){
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n,m,K;
  cin >> n >> m >> K;
  vector<Seg2D> cnt(3, Seg2D(n, m));
  rep(i, n){
    string s; cin >> s;
    rep(j, m){
      rep(k, 3) if(s[j] == t[k]){
        cnt[k].set(i, j, 1);
        break;
      }
    }
  }
  rep(i, 3) cnt[i].build();
  rep(_, K){
    int a,b,c,d;
    cin >> a >> b >> c >> d;
    a--; b--;
    rep(i, 3) cout << cnt[i].query(a, b, c, d) << " \n"[i == 2];
  }
}
Back to top page