Submission details
Task:Forest density
Sender:discape
Submission time:2025-09-22 17:36:54 +0300
Language:C++ (C++20)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.56 sdetails
#3ACCEPTED0.54 sdetails

Code

#include <bits/stdc++.h>
using namespace std;
// clang-format off
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
template <typename T> istream &operator>>(istream &is, vector<T> &v) { T value; is >> value; v.push_back(value); return is; }
#define preamble ios::sync_with_stdio(0); cin.tie(0); dbg("INIT");
// clang-format on
#ifdef DO_DBG
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
template <typename T> using v = vector<T>;
template <typename T> using us = unordered_set<T>;
template <typename K, typename V> using um = unordered_map<K, V>;
constexpr int MOD = 1e9 + 7;
const int INF = 1e9;
const ld EPS = 1e-9;
#define loopi(n) for (int i = 0; i < n; i++)
#define loopj(n) for (int j = 0; j < n; j++)
#define loopk(n) for (int k = 0; k < n; k++)
#define loopz(n) for (int z = 0; z < n; z++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define sq(x) ((x) * (x))

int main() {
  int n, q;
  cin >> n >> q;
  // v<bool> forest(sq(n + 1));
  v<int> prefix(sq(n + 1));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      char c;
      cin >> c;
      int res = c == '*' ? 1 : 0;
      // forest[i * n + j] = res;
      int sum = res;
      if (i != 0) {
        sum += prefix[(i - 1) * n + j];
      }
      if (j != 0) {
        sum += prefix[i * n + j - 1];
      }
      if (j != 0 && i != 0) {
        sum -= prefix[(i - 1) * n + (j - 1)];
      }
      prefix[i * n + j] = sum;
      // cerr << sum << " ";
    }
    // cerr << "\n";
  }
  loopi(q) {
    int y1, x1, y2, x2;
    cin >> y1 >> x1 >> y2 >> x2;
    y1--;
    x1--;
    int a = prefix[y2 * n + x2];
    int b = prefix[y2 * n + x1];
    int c = prefix[y1 * n + x2];
    int d = prefix[y1 * n + x1];
    dbg(a, b, c, d);
    int sum = a - b - c + d;
    cout << sum << "\n";
  }
  dbg(prefix);
}

Test details

Test 1

Verdict: ACCEPTED

input
10 100
**.*.*.**.
*.**.*..*.
.*****.**.
**....***.
...

correct output
10
14
5
7
8
...

user output
10
14
5
7
8
...
Truncated

Test 2

Verdict: ACCEPTED

input
1000 200000
**.**.****..**.***..**.***.**....

correct output
41079
2824
15631
1548
8483
...

user output
41079
2824
15631
1548
8483
...
Truncated

Test 3

Verdict: ACCEPTED

input
1000 200000
******************************...

correct output
1000000
1000000
1000000
1000000
1000000
...

user output
1000000
1000000
1000000
1000000
1000000
...
Truncated