//Make CSP great again
//Vengeance
#include <bits/stdc++.h>
#define TASK "TESTCODE"
#define Log2(x) 31 - __builtin_clz(x)
using namespace std;
const int N = 3e5 + 1;
struct SegmentTree
{
struct FenwickTree
{
vector<int> tree;
int sz;
void Assign(int _sz)
{
sz = _sz;
tree.resize(sz, 0);
}
void add(int pos, int val)
{
for (; pos >= 0; pos = (pos & (pos + 1)) - 1)
{
tree[pos] += val;
}
}
int sum(int pos)
{
int ret = 0;
for (; pos < sz; pos |= pos + 1)
{
ret += tree[pos];
}
return ret;
}
} tree[4 * N + 1];
vector<int> num[4 * N + 1];
void add_num(int v, int TreeL, int TreeR, int L, int R, int val)
{
if (L > R)
{
return ;
}
if (TreeL == L && TreeR == R)
{
num[v].push_back(val);
return ;
}
int mid = (TreeL + TreeR)/2;
add_num(v * 2, TreeL, mid, L, min(R, mid), val);
add_num(v * 2 + 1, mid + 1, TreeR, max(L, mid + 1), R, val);
}
void build(int v, int TreeL, int TreeR)
{
sort(num[v].begin(), num[v].end());
num[v].erase(unique(num[v].begin(), num[v].end()), num[v].end());
tree[v].Assign(num[v].size());
if (TreeL == TreeR)
{
return ;
}
int mid = (TreeL + TreeR)/2;
build(v * 2, TreeL, mid);
build(v * 2 + 1, mid + 1, TreeR);
}
int convert(int v, int x)
{
return lower_bound(num[v].begin(), num[v].end(), x) - num[v].begin();
}
void add_val(int v, int TreeL, int TreeR, int L, int R, int num, int val)
{
if (L > R)
{
return ;
}
if (TreeL == L && TreeR == R)
{
tree[v].add(convert(v, num), val);
return ;
}
int mid = (TreeL + TreeR)/2;
add_val(v * 2, TreeL, mid, L, min(R, mid), num, val);
add_val(v * 2 + 1, mid + 1, TreeR, max(L, mid + 1), R, num, val);
}
int get(int v, int TreeL, int TreeR, int pos, int fn)
{
int ret = tree[v].sum(convert(v, fn));
if (TreeL == TreeR)
{
return ret;
}
int mid = (TreeL + TreeR)/2;
if (pos <= mid)
{
return ret + get(v * 2, TreeL, mid, pos, fn);
}
else
{
return ret + get(v * 2 + 1, mid + 1, TreeR, pos, fn);
}
}
} tree;
char lamp[N + 1], st_lamp[N + 1];
int n, q, a[N + 1], b[N + 1];
set<tuple<int, int, int> > st_state, cur;
void read()
{
cin >> n >> q;
for (int i = 1; i <= n; ++ i)
{
cin >> st_lamp[i];
lamp[i] = st_lamp[i];
}
int l = -1, r = -1;
for (int i = 1; i <= n; ++ i)
{
if (lamp[i] == '0')
{
if (l != -1 && r != -1)
{
st_state.insert({l, r, 0});
l = r = -1;
}
}
else
{
if (l == -1)
{
l = i;
}
r = i + 1;
}
}
if (l != -1 && r != -1)
{
st_state.insert({l, r, 0});
}
cur = st_state;
}
set<tuple<int, int, int> > :: iterator contain(int x)
{
set<tuple<int, int, int> > :: iterator it = cur.upper_bound({x, N + 10, N + 10});
if (it != cur.begin())
{
--it;
}
if (it == cur.end())
{
return it;
}
int l, r, t;
tie(l, r, t) = *it;
if (l <= x && x <= r)
{
return it;
}
else
{
return cur.end();
}
}
void solve()
{
for (auto &[l, r, t] : cur)
{
//cerr << l << ' ' << r << '\n';
tree.add_num(1, 1, n + 1, l, r, r);
}
for (int i = 1; i <= q; ++ i)
{
string s;
cin >> s;
if (s == "query")
{
cin >> a[i] >> b[i];
}
else
{
cin >> a[i];
if (lamp[a[i]] == '0')
{
lamp[a[i]] = '1';
int l = a[i], r = a[i] + 1;
auto it = contain(a[i] + 1);
if (it != cur.end())
{
r = get<1> (*it);
cur.erase(it);
}
it = contain(a[i]);
if (it != cur.end())
{
l = get<0> (*it);
cur.erase(it);
}
//cerr << l << ' ' << r << '\n';
cur.insert({l, r, i});
tree.add_num(1, 1, n + 1, l, r, r);
}
else
{
lamp[a[i]] = '0';
auto it = contain(a[i]);
int l = get<0> (*it), r = get<1> (*it);
cur.erase(it);
//cerr << l << ' ' << r << '\n';
if (l < a[i])
{
cur.insert({l, a[i], i});
tree.add_num(1, 1, n + 1, l, a[i], a[i]);
}
if (r > a[i] + 1)
{
cur.insert({a[i] + 1, r, i});
tree.add_num(1, 1, n + 1, a[i] + 1, r, r);
}
}
}
}
tree.build(1, 1, n + 1);
cur = st_state;
copy(st_lamp + 1, st_lamp + n + 1, lamp + 1);
for (auto &[l, r, t] : cur)
{
//cerr << l << ' ' << r << '\n';
//tree.add_val(1, 1, n + 1, l, r, r, 1);
}
for (int i = 1; i <= q; ++ i)
{
if (b[i]) // query
{
int res = tree.get(1, 1, n + 1, a[i], b[i]);
auto it = contain(a[i]);
if (it != cur.end() && get<1> (*it) >= b[i])
{
res += i - get<2> (*it);
}
cout << res << '\n';
}
else // update
{
if (lamp[a[i]] == '0')
{
lamp[a[i]] = '1';
int l = a[i], r = a[i] + 1;
auto it = contain(a[i] + 1);
if (it != cur.end())
{
r = get<1> (*it);
tree.add_val(1, 1, n + 1, a[i] + 1, r, r, i - get<2> (*it));
cur.erase(it);
}
it = contain(a[i]);
if (it != cur.end())
{
l = get<0> (*it);
tree.add_val(1, 1, n + 1, l, a[i], a[i], i - get<2> (*it));
cur.erase(it);
}
cur.insert({l, r, i});
}
else
{
lamp[a[i]] = '0';
auto it = contain(a[i]);
int l, r, t;
tie(l, r, t) = *it;
cur.erase(it);
tree.add_val(1, 1, n + 1, l, r, r, i - t);
if (l < a[i])
{
cur.insert({l, a[i], i});
//tree.add_val(1, 1, n + 1, l, a[i], a[i], i - 1 - t);
}
if (r > a[i] + 1)
{
cur.insert({a[i] + 1, r, i});
//tree.add_val(1, 1, n + 1, a[i] + 1, r, r, i - 1 - t);
}
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen(TASK".INP", "r"))
{
freopen(TASK".INP", "r", stdin);
//freopen(TASK".OUT", "w", stdout);
}
int t = 1;
bool typetest = false;
if (typetest)
{
cin >> t;
}
for (int __ = 1; __ <= t; ++ __)
{
//cout << "Case " << __ << ": ";
read();
solve();
}
}
Compilation message
street_lamps.cpp: In function 'void solve()':
street_lamps.cpp:222:16: warning: unused structured binding declaration [-Wunused-variable]
222 | for (auto &[l, r, t] : cur)
| ^~~~~~~~~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:289:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
289 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
66156 KB |
Output is correct |
2 |
Correct |
39 ms |
65976 KB |
Output is correct |
3 |
Correct |
33 ms |
66084 KB |
Output is correct |
4 |
Correct |
34 ms |
66068 KB |
Output is correct |
5 |
Correct |
34 ms |
66028 KB |
Output is correct |
6 |
Correct |
34 ms |
66064 KB |
Output is correct |
7 |
Correct |
33 ms |
66016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
211 ms |
74396 KB |
Output is correct |
2 |
Correct |
238 ms |
74828 KB |
Output is correct |
3 |
Correct |
310 ms |
75884 KB |
Output is correct |
4 |
Correct |
724 ms |
100100 KB |
Output is correct |
5 |
Correct |
838 ms |
101328 KB |
Output is correct |
6 |
Correct |
762 ms |
100392 KB |
Output is correct |
7 |
Correct |
237 ms |
75452 KB |
Output is correct |
8 |
Correct |
229 ms |
76912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
66140 KB |
Output is correct |
2 |
Correct |
34 ms |
66128 KB |
Output is correct |
3 |
Correct |
35 ms |
66128 KB |
Output is correct |
4 |
Correct |
39 ms |
66076 KB |
Output is correct |
5 |
Correct |
1006 ms |
100392 KB |
Output is correct |
6 |
Correct |
941 ms |
103636 KB |
Output is correct |
7 |
Correct |
768 ms |
100580 KB |
Output is correct |
8 |
Correct |
241 ms |
76816 KB |
Output is correct |
9 |
Correct |
112 ms |
71644 KB |
Output is correct |
10 |
Correct |
129 ms |
72120 KB |
Output is correct |
11 |
Correct |
120 ms |
72232 KB |
Output is correct |
12 |
Correct |
232 ms |
75472 KB |
Output is correct |
13 |
Correct |
240 ms |
76996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
66132 KB |
Output is correct |
2 |
Correct |
34 ms |
66168 KB |
Output is correct |
3 |
Correct |
34 ms |
66196 KB |
Output is correct |
4 |
Correct |
34 ms |
66180 KB |
Output is correct |
5 |
Correct |
409 ms |
94380 KB |
Output is correct |
6 |
Correct |
575 ms |
97548 KB |
Output is correct |
7 |
Correct |
778 ms |
99844 KB |
Output is correct |
8 |
Correct |
968 ms |
102224 KB |
Output is correct |
9 |
Correct |
264 ms |
74416 KB |
Output is correct |
10 |
Correct |
267 ms |
73656 KB |
Output is correct |
11 |
Correct |
273 ms |
74596 KB |
Output is correct |
12 |
Correct |
255 ms |
73592 KB |
Output is correct |
13 |
Correct |
278 ms |
74444 KB |
Output is correct |
14 |
Correct |
255 ms |
73640 KB |
Output is correct |
15 |
Correct |
239 ms |
75412 KB |
Output is correct |
16 |
Correct |
241 ms |
76976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
66156 KB |
Output is correct |
2 |
Correct |
39 ms |
65976 KB |
Output is correct |
3 |
Correct |
33 ms |
66084 KB |
Output is correct |
4 |
Correct |
34 ms |
66068 KB |
Output is correct |
5 |
Correct |
34 ms |
66028 KB |
Output is correct |
6 |
Correct |
34 ms |
66064 KB |
Output is correct |
7 |
Correct |
33 ms |
66016 KB |
Output is correct |
8 |
Correct |
211 ms |
74396 KB |
Output is correct |
9 |
Correct |
238 ms |
74828 KB |
Output is correct |
10 |
Correct |
310 ms |
75884 KB |
Output is correct |
11 |
Correct |
724 ms |
100100 KB |
Output is correct |
12 |
Correct |
838 ms |
101328 KB |
Output is correct |
13 |
Correct |
762 ms |
100392 KB |
Output is correct |
14 |
Correct |
237 ms |
75452 KB |
Output is correct |
15 |
Correct |
229 ms |
76912 KB |
Output is correct |
16 |
Correct |
40 ms |
66140 KB |
Output is correct |
17 |
Correct |
34 ms |
66128 KB |
Output is correct |
18 |
Correct |
35 ms |
66128 KB |
Output is correct |
19 |
Correct |
39 ms |
66076 KB |
Output is correct |
20 |
Correct |
1006 ms |
100392 KB |
Output is correct |
21 |
Correct |
941 ms |
103636 KB |
Output is correct |
22 |
Correct |
768 ms |
100580 KB |
Output is correct |
23 |
Correct |
241 ms |
76816 KB |
Output is correct |
24 |
Correct |
112 ms |
71644 KB |
Output is correct |
25 |
Correct |
129 ms |
72120 KB |
Output is correct |
26 |
Correct |
120 ms |
72232 KB |
Output is correct |
27 |
Correct |
232 ms |
75472 KB |
Output is correct |
28 |
Correct |
240 ms |
76996 KB |
Output is correct |
29 |
Correct |
35 ms |
66132 KB |
Output is correct |
30 |
Correct |
34 ms |
66168 KB |
Output is correct |
31 |
Correct |
34 ms |
66196 KB |
Output is correct |
32 |
Correct |
34 ms |
66180 KB |
Output is correct |
33 |
Correct |
409 ms |
94380 KB |
Output is correct |
34 |
Correct |
575 ms |
97548 KB |
Output is correct |
35 |
Correct |
778 ms |
99844 KB |
Output is correct |
36 |
Correct |
968 ms |
102224 KB |
Output is correct |
37 |
Correct |
264 ms |
74416 KB |
Output is correct |
38 |
Correct |
267 ms |
73656 KB |
Output is correct |
39 |
Correct |
273 ms |
74596 KB |
Output is correct |
40 |
Correct |
255 ms |
73592 KB |
Output is correct |
41 |
Correct |
278 ms |
74444 KB |
Output is correct |
42 |
Correct |
255 ms |
73640 KB |
Output is correct |
43 |
Correct |
239 ms |
75412 KB |
Output is correct |
44 |
Correct |
241 ms |
76976 KB |
Output is correct |
45 |
Correct |
121 ms |
71076 KB |
Output is correct |
46 |
Correct |
158 ms |
71300 KB |
Output is correct |
47 |
Correct |
382 ms |
81348 KB |
Output is correct |
48 |
Correct |
774 ms |
99788 KB |
Output is correct |
49 |
Correct |
126 ms |
72500 KB |
Output is correct |
50 |
Correct |
122 ms |
72456 KB |
Output is correct |
51 |
Correct |
135 ms |
72752 KB |
Output is correct |
52 |
Correct |
130 ms |
73004 KB |
Output is correct |
53 |
Correct |
127 ms |
72868 KB |
Output is correct |
54 |
Correct |
151 ms |
73024 KB |
Output is correct |