CSES - Aalto Competitive Programming 2024 - wk4 - Mon - Results
Submission details
Task:Forest density
Sender:aarol
Submission time:2024-09-24 12:29:10 +0300
Language:C++ (C++11)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.94 sdetails
#3ACCEPTED0.92 sdetails

Code

#include <bits/stdc++.h>

using namespace std;

int n;

int main() {
  // ios::sync_with_stdio(false);
  // cin.tie(0);
  int q;
  cin >> n >> q;

  auto values = vector<string>(n, "");

  auto sumarr = vector<vector<int>>(n, vector<int>(n));

  for (int i = 0; i < n; i++) {
    cin >> values[i];
  }

  for (int i = 0; i < n; i++) {
    for (int y = 0; y < n; y++) {
      int c = 0;

      for (int yy = 0; yy <= y; yy++) {
        if (values[i][yy] == '*') c++;
      }
      if(i > 0) c += sumarr[i-1][y];
      sumarr[i][y] = c;
    }
  }

  for (int i = 0; i < q; i++) {
    int y1, x1, y2, x2;
    cin >> y1 >> x1 >> y2 >> x2;
    y1--;
    x1--;
    y2--;
    x2--;
    int A = sumarr[y2][x2];
    if (x1 > 0) A -= sumarr[y2][x1 - 1];
    if (y1 > 0) A -= sumarr[y1 - 1][x2];
    if (y1 > 0 && x1 > 0) A += sumarr[y1 - 1][x1 - 1];

    cout << A << endl;
  }

  return 0;
}

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