-
Notifications
You must be signed in to change notification settings - Fork 185
/
1739.cc
74 lines (67 loc) · 1.55 KB
/
1739.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// https://cses.fi/problemset/task/1739
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
int k;
vvi t;
void build() {
for (int i = k-1; i > 0; i--)
for (int j = k; j < 2*k; j++)
t[i][j] = t[i*2][j] + t[i*2+1][j];
for (int i = 0; i < 2*k; i++)
for (int j = k-1; j > 0; j--)
t[i][j] = t[i][j*2] + t[i][j*2+1];
}
void toggle(int y, int x) {
y += k-1, x += k-1;
t[y][x] = !t[y][x];
for (int j = x/2; j > 0; j /= 2)
t[y][j] = t[y][j*2] + t[y][j*2+1];
for (int i = y/2; i > 0; i /= 2) {
t[i][x] = t[i*2][x] + t[i*2+1][x];
for (int j = x/2; j > 0; j /= 2)
t[i][j] = t[i][j*2] + t[i][j*2+1];
}
}
int countx(int y, int l, int r) {
int c = 0;
l += k-1, r += k-1;
while (l <= r) {
if (l%2 == 1) c += t[y][l++];
if (r%2 == 0) c += t[y][r--];
l /= 2, r /= 2;
}
return c;
}
int count(int ly, int ry, int lx, int rx) {
ly += k-1, ry += k-1;
int c = 0;
while (ly <= ry) {
if (ly % 2 == 1) c += countx(ly++, lx, rx);
if (ry % 2 == 0) c += countx(ry--, lx, rx);
ly /= 2, ry /= 2;
}
return c;
}
int main() {
cin.tie(0), ios::sync_with_stdio(0);
int n, q;
cin >> n >> q;
k = 1;
while (k < n) k *= 2;
t = vvi(2*k, vi(2*k));
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (int j = 0; j < n; j++)
if (s[j] == '*') t[i+k][j+k] = 1;
}
build();
while (q--) {
int t, y1, x1, y2, x2;
cin >> t >> y1 >> x1;
if (t == 1) toggle(y1, x1);
else cin >> y2 >> x2, cout << count(y1, y2, x1, x2) << '\n';;
}
}