-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcses_1739.cpp
More file actions
102 lines (96 loc) · 2.07 KB
/
cses_1739.cpp
File metadata and controls
102 lines (96 loc) · 2.07 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <bits/extc++.h>
using namespace std;
inline int lowbit(int x) { return x & -x; }
class BIT
{
int n;
vector<long long> bit;
public:
void init(int _n)
{
n = _n;
bit.resize(n);
for (auto &b : bit)
b = 0;
}
long long query(int x) const
{
long long sum = 0;
for (; x; x -= lowbit(x))
sum += bit[x];
return sum;
}
void modify(int x, int val)
{
for (; x <= n; x += lowbit(x))
bit[x] += val;
}
};
class BIT2D
{
int m;
vector<BIT> bit1D;
public:
void init(int _m, int _n)
{
m = _m;
bit1D.resize(m);
for (auto &b : bit1D)
b.init(_n);
}
long long query(int x, int y) const
{
long long sum = 0;
for (; x; x -= lowbit(x))
sum += bit1D[x].query(y);
return sum;
}
void modify(int x, int y, int val)
{
for (; x <= m; x += lowbit(x))
bit1D[x].modify(y, val);
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<string> forest(n);
for (auto &str : forest)
cin >> str;
BIT2D bit2d;
bit2d.init(n + 87, n + 87);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (forest[i][j] == '*')
bit2d.modify(i + 2, j + 2, 1);
while (q--)
{
int op;
cin >> op;
if (op == 1)
{
int x, y;
cin >> x >> y;
if (forest[x - 1][y - 1] == '.')
{
bit2d.modify(x + 1, y + 1, 1);
forest[x - 1][y - 1] = '*';
}
else
{
bit2d.modify(x + 1, y + 1, -1);
forest[x - 1][y - 1] = '.';
}
}
else
{
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
cout << bit2d.query(x2 + 1, y2 + 1) - bit2d.query(x2 + 1, y1) - bit2d.query(x1, y2 + 1) + bit2d.query(x1, y1) << '\n';
}
}
return 0;
}