// Problem : APIO '19 P3 - Street Lamps
// Contest : DMOJ
// URL : https://dmoj.ca/problem/apio19p3
// Memory Limit : 512 MB
// Time Limit : 2500 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
using namespace std;
struct FenwickTree{
int bit[300005];
int lim;
vector<int> vec;
int query(int n){
//cout << "q: " << n << endl;
int ret = 0;
while(n){
ret += bit[n];
n -= n&-n;
}
return ret;
}
void add(int n, int v){
//cout << "a: " << n << endl;
while(n <= lim){
bit[n] += v;
vec.push_back(n);
n += n&-n;
}
}
void clr(){
//cout << "c: " << n << endl;
for(int n : vec){
bit[n] = 0;
}
vec.clear();
}
};
int N, Q;
bool state[300005];
set<int> st;
multiset<int> rhthand;
vector<pair<pair<int, int>, int>> upd, qu;
int ans[300005];
FenwickTree bit;
void solve(int l, int r, vector<pair<pair<int, int>, int>> upd, vector<pair<pair<int, int>, int>> qu){
if(l > r){
return;
}
//cout << l << " " << r << endl;
int mid = (l+r)>>1;
vector<pair<pair<int, int>, int>> lftu, rhtu, lftq, rhtq;
int idx = 0;
int cnt = 0;
for(auto q : qu){
while(idx < (int) upd.size() && abs(upd[idx].second) > q.second){
if(upd[idx].first.first <= mid){
if(upd[idx].first.second >= mid){
//cout << upd[idx].second << endl;
bit.add(1, upd[idx].second);
bit.add(upd[idx].first.second+1, -upd[idx].second);
if(upd[idx].second > 0){
cnt++;
rhthand.insert(upd[idx].first.second);
}
else{
cnt--;
rhthand.erase(rhthand.lower_bound(upd[idx].first.second));
}
}
lftu.push_back(upd[idx]);
}
else{
rhtu.push_back(upd[idx]);
}
idx++;
}
assert(cnt <= 1);
if(q.first.first >= mid){
if(cnt && *rhthand.begin() >= q.first.second){
ans[q.second] -= q.second;
}
ans[q.second] += bit.query(q.first.second);
if(q.first.first > mid){
rhtq.push_back(q);
}
}
else{
lftq.push_back(q);
}
}
bit.clr();
rhthand.clear();
solve(l, mid-1, lftu, lftq);
solve(mid+1, r, rhtu, rhtq);
}
int main(){
cin.sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> Q;
st.insert(0);
st.insert(N+1);
for(int i = 1; i<=N; i++){
char c;
cin >> c;
state[i] = (c == '1');
if(!state[i]){
st.insert(i);
}
}
for(int i = 1; i<=N; i++){
if(state[i]){
int idx = *st.lower_bound(i);
idx--;
upd.push_back({{i, idx}, Q+1});
i = idx;
}
}
for(int q = Q; q; q--){
string s;
cin >> s;
if(s == "query"){
int l, r;
cin >> l >> r;
r--;
qu.push_back({{l, r}, q});
}
else{
int i;
cin >> i;
state[i] ^= 1;
const int coe = state[i] ? 1 : -1;
int r = i, l = i;
if(state[i]){
st.erase(i);
}
if(state[i+1]){
r = *st.lower_bound(i)-1;
upd.push_back({{i+1, r}, -coe*q});
}
if(state[i-1]){
l = *(--st.lower_bound(i))+1;
upd.push_back({{l, i-1}, -coe*q});
}
upd.push_back({{l, r}, coe*q});
if(!state[i]){
st.insert(i);
}
ans[q] = -1;
}
}
bit.lim = N;
solve(1, N, upd, qu);
/*
for(auto q : upd){
cout << q.first.first << " " << q.first.second << " " << q.second << "\n";
}
*/
for(int q = Q; q; q--){
if(ans[q] != -1){
cout << ans[q] << "\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
127 ms |
35664 KB |
Output is correct |
2 |
Correct |
138 ms |
34232 KB |
Output is correct |
3 |
Correct |
178 ms |
34924 KB |
Output is correct |
4 |
Correct |
337 ms |
45496 KB |
Output is correct |
5 |
Correct |
351 ms |
39868 KB |
Output is correct |
6 |
Correct |
331 ms |
45496 KB |
Output is correct |
7 |
Correct |
221 ms |
38836 KB |
Output is correct |
8 |
Correct |
186 ms |
24208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2408 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
286 ms |
47200 KB |
Output is correct |
6 |
Correct |
378 ms |
54192 KB |
Output is correct |
7 |
Correct |
317 ms |
48312 KB |
Output is correct |
8 |
Correct |
180 ms |
33620 KB |
Output is correct |
9 |
Correct |
56 ms |
22740 KB |
Output is correct |
10 |
Correct |
59 ms |
25524 KB |
Output is correct |
11 |
Correct |
59 ms |
27584 KB |
Output is correct |
12 |
Correct |
247 ms |
49816 KB |
Output is correct |
13 |
Correct |
175 ms |
35764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
252 ms |
41528 KB |
Output is correct |
6 |
Correct |
301 ms |
41640 KB |
Output is correct |
7 |
Correct |
334 ms |
46368 KB |
Output is correct |
8 |
Correct |
275 ms |
62640 KB |
Output is correct |
9 |
Correct |
135 ms |
40380 KB |
Output is correct |
10 |
Correct |
131 ms |
47632 KB |
Output is correct |
11 |
Correct |
141 ms |
39772 KB |
Output is correct |
12 |
Correct |
126 ms |
49072 KB |
Output is correct |
13 |
Correct |
142 ms |
39608 KB |
Output is correct |
14 |
Correct |
133 ms |
50092 KB |
Output is correct |
15 |
Correct |
232 ms |
47216 KB |
Output is correct |
16 |
Correct |
173 ms |
33968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
127 ms |
35664 KB |
Output is correct |
9 |
Correct |
138 ms |
34232 KB |
Output is correct |
10 |
Correct |
178 ms |
34924 KB |
Output is correct |
11 |
Correct |
337 ms |
45496 KB |
Output is correct |
12 |
Correct |
351 ms |
39868 KB |
Output is correct |
13 |
Correct |
331 ms |
45496 KB |
Output is correct |
14 |
Correct |
221 ms |
38836 KB |
Output is correct |
15 |
Correct |
186 ms |
24208 KB |
Output is correct |
16 |
Correct |
1 ms |
2408 KB |
Output is correct |
17 |
Correct |
1 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
286 ms |
47200 KB |
Output is correct |
21 |
Correct |
378 ms |
54192 KB |
Output is correct |
22 |
Correct |
317 ms |
48312 KB |
Output is correct |
23 |
Correct |
180 ms |
33620 KB |
Output is correct |
24 |
Correct |
56 ms |
22740 KB |
Output is correct |
25 |
Correct |
59 ms |
25524 KB |
Output is correct |
26 |
Correct |
59 ms |
27584 KB |
Output is correct |
27 |
Correct |
247 ms |
49816 KB |
Output is correct |
28 |
Correct |
175 ms |
35764 KB |
Output is correct |
29 |
Correct |
1 ms |
2652 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
1 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2648 KB |
Output is correct |
33 |
Correct |
252 ms |
41528 KB |
Output is correct |
34 |
Correct |
301 ms |
41640 KB |
Output is correct |
35 |
Correct |
334 ms |
46368 KB |
Output is correct |
36 |
Correct |
275 ms |
62640 KB |
Output is correct |
37 |
Correct |
135 ms |
40380 KB |
Output is correct |
38 |
Correct |
131 ms |
47632 KB |
Output is correct |
39 |
Correct |
141 ms |
39772 KB |
Output is correct |
40 |
Correct |
126 ms |
49072 KB |
Output is correct |
41 |
Correct |
142 ms |
39608 KB |
Output is correct |
42 |
Correct |
133 ms |
50092 KB |
Output is correct |
43 |
Correct |
232 ms |
47216 KB |
Output is correct |
44 |
Correct |
173 ms |
33968 KB |
Output is correct |
45 |
Correct |
59 ms |
24364 KB |
Output is correct |
46 |
Correct |
74 ms |
25660 KB |
Output is correct |
47 |
Correct |
176 ms |
30404 KB |
Output is correct |
48 |
Correct |
348 ms |
54196 KB |
Output is correct |
49 |
Correct |
66 ms |
28604 KB |
Output is correct |
50 |
Correct |
68 ms |
28348 KB |
Output is correct |
51 |
Correct |
67 ms |
30656 KB |
Output is correct |
52 |
Correct |
72 ms |
33728 KB |
Output is correct |
53 |
Correct |
74 ms |
31940 KB |
Output is correct |
54 |
Correct |
74 ms |
32192 KB |
Output is correct |