Skip to content

Commit 7135413

Browse files
Create Rooks Defenders.cpp
1 parent 99dfd15 commit 7135413

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <cstring>
4+
5+
using namespace std;
6+
7+
#define LEFT(n) (2*n)
8+
#define RIGHT(n) (2*n + 1)
9+
10+
const int MAX_N = 1e5 + 5;
11+
int sum_tree[3*MAX_N][2];
12+
13+
void update(int n, int left, int right, int index, int value, int tree_index)
14+
{
15+
if(index < left || right < index)
16+
{
17+
return;
18+
}
19+
20+
if(left == right)
21+
{
22+
sum_tree[n][tree_index] = value;
23+
//cout << (tree_index == 0 ? "Row" : "Column") << " Sum Tree [" << left << "," << right << "] = " << sum_tree[n][tree_index] << "\n";
24+
return;
25+
}
26+
27+
int mid = (left + right)/2;
28+
update(LEFT(n), left, mid, index, value, tree_index);
29+
update(RIGHT(n), mid + 1, right, index, value, tree_index);
30+
31+
sum_tree[n][tree_index] = sum_tree[LEFT(n)][tree_index] + sum_tree[RIGHT(n)][tree_index];
32+
//cout << (tree_index == 0 ? "Row" : "Column") << " Sum Tree [" << left << "," << right << "] = " << sum_tree[n][tree_index] << "\n";
33+
}
34+
35+
int get_sum(int n, int left, int right, int query_left, int query_right, int tree_index)
36+
{
37+
if(query_right < left || right < query_left)
38+
{
39+
return 0;
40+
}
41+
42+
if(query_left <= left && right <= query_right)
43+
{
44+
return sum_tree[n][tree_index];
45+
}
46+
47+
int mid = (left + right)/2;
48+
int left_sum = get_sum(LEFT(n), left, mid, query_left, query_right, tree_index);
49+
int right_sum = get_sum(RIGHT(n), mid + 1, right, query_left, query_right, tree_index);
50+
51+
return (left_sum + right_sum);
52+
}
53+
54+
int main()
55+
{
56+
ios_base::sync_with_stdio(false);
57+
cin.tie(NULL);
58+
59+
int n, no_of_queries;
60+
cin >> n >> no_of_queries;
61+
62+
memset(sum_tree, 0, sizeof(sum_tree));
63+
64+
const int ROW = 0, COLUMN = 1;
65+
vector <int> row_attackers(n + 1, 0), column_attackers(n + 1, 0);
66+
for(int i = 1; i <= no_of_queries; i++)
67+
{
68+
const int ADD = 1, REMOVE = 2, RECTANGLE = 3;
69+
int query;
70+
cin >> query;
71+
72+
switch(query)
73+
{
74+
case ADD: {
75+
int x, y;
76+
cin >> x >> y;
77+
78+
row_attackers[x]++;
79+
if(row_attackers[x] == 1)
80+
{
81+
update(1, 1, n, x, 1, ROW);
82+
}
83+
84+
column_attackers[y]++;
85+
if(column_attackers[y] == 1)
86+
{
87+
update(1, 1, n, y, 1, COLUMN);
88+
}
89+
}
90+
break;
91+
92+
case REMOVE : {
93+
int x, y;
94+
cin >> x >> y;
95+
96+
row_attackers[x]--;
97+
if(row_attackers[x] == 0)
98+
{
99+
update(1, 1, n, x, 0, ROW);
100+
}
101+
102+
column_attackers[y]--;
103+
if(column_attackers[y] == 0)
104+
{
105+
update(1, 1, n, y, 0, COLUMN);
106+
}
107+
}
108+
break;
109+
110+
case RECTANGLE :{
111+
int x1, y1, x2, y2;
112+
cin >> x1 >> y1 >> x2 >> y2;
113+
114+
if(x1 > x2)
115+
{
116+
swap(x1, x2);
117+
}
118+
119+
if(y1 > y2)
120+
{
121+
swap(y1, y2);
122+
}
123+
124+
int attacked_rows = get_sum(1, 1, n, x1, x2, ROW);
125+
int attacked_columns = get_sum(1, 1, n, y1, y2, COLUMN);
126+
//cout << "Rows attacked = " << attacked_rows << " Columns attacked = " << attacked_columns << "\n";
127+
128+
int every_cell_attacked = ( (attacked_rows == x2 - x1 + 1) || (attacked_columns == y2 - y1 + 1) );
129+
cout << (every_cell_attacked ? "Yes" : "No") << "\n";
130+
}
131+
}
132+
}
133+
134+
return 0;
135+
}
136+
 

0 commit comments

Comments
 (0)