Skip to content

Commit f36cadf

Browse files
committed
forest queries
1 parent 0ced315 commit f36cadf

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

forestQueries.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "bits/stdc++.h"
2+
using namespace std;
3+
#define ll long long
4+
#define mod 1000000007
5+
6+
#define N 1001
7+
ll forest[N][N];
8+
9+
ll getSum(ll sx, ll sy, ll ex, ll ey)
10+
{
11+
return forest[ex][ey] - forest[sx-1][ey] - forest[ex][sy-1] + forest[sx-1][sy-1];
12+
}
13+
14+
void solve()
15+
{
16+
ll n, q;
17+
char tree;
18+
19+
cin >> n >> q;
20+
21+
for(int i=1; i<=n; i++)
22+
{
23+
for(int j=1; j<=n; j++)
24+
{
25+
cin >> tree;
26+
forest[i][j] = tree == '*';
27+
forest[i][j] += forest[i][j-1];
28+
}
29+
}
30+
31+
for(int i=1; i<=n; i++)
32+
{
33+
for(int j=1; j<=n; j++)
34+
{
35+
forest[i][j] += forest[i-1][j];
36+
}
37+
}
38+
39+
ll a, b, c, d;
40+
for(int i=1; i<=q; i++)
41+
{
42+
cin >> a >> b >> c >> d;
43+
cout << getSum(a, b, c, d) << "\n";
44+
}
45+
46+
}
47+
48+
int main()
49+
{
50+
ios_base::sync_with_stdio(false);
51+
cin.tie(NULL);
52+
cout.tie(NULL);
53+
solve();
54+
}

0 commit comments

Comments
 (0)