-
Notifications
You must be signed in to change notification settings - Fork 154
/
1188 - Bit Inversions.cpp
94 lines (91 loc) · 2.4 KB
/
1188 - Bit Inversions.cpp
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
/*
Problem Name: Bit Inversions
Problem Link: https://cses.fi/problemset/task/1188
Author: Sachin Srivastava (mrsac7)
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w", stdout);
#endif
string s; cin>>s;
set<int> up, lo;
int n = s.size();
int l = 0, r = 0;
up.insert(0);
lo.insert(0);
multiset<int> ans;
int mx = 0;
while (l < n) {
r = l + 1;
while(r < n && s[r] == s[l]) r++;
up.insert(r);
lo.insert(-r);
ans.insert(r-l);
l = r;
}
int q; cin>>q;
while(q--) {
int x; cin>>x;
int r = n, l = 0;
if (x < n)
r = *up.upper_bound(x);
if (x > 1)
l = -*lo.upper_bound(1-x);
if (up.count(x) && up.count(x-1)) {
if (r-x)
ans.erase(ans.find(r-x));
if (x-1-l)
ans.erase(ans.find(x-1-l));
ans.erase(ans.find(1));
if (x != n)
up.erase(x), lo.erase(-x);
if (x-1)
up.erase(x-1), lo.erase(1-x);
ans.insert(r-l);
}
else if (up.count(x-1)) {
if (r-x+1)
ans.erase(ans.find(r-x+1));
if (x-1-l)
ans.erase(ans.find(x-1-l));
if (x-1)
up.erase(x-1), lo.erase(1-x);
if (x-l)
ans.insert(x-l);
if (r-x)
ans.insert(r-x);
up.insert(x); lo.insert(-x);
}
else if (up.count(x)) {
if (r-x)
ans.erase(ans.find(r-x));
if (x-l)
ans.erase(ans.find(x-l));
if (x != n)
up.erase(x), lo.erase(-x);
if (x-1-l)
ans.insert(x-1-l);
if (r-x+1)
ans.insert(r-x+1);
up.insert(x-1); lo.insert(1-x);
}
else {
if (r-l)
ans.erase(ans.find(r-l));
up.insert(x); lo.insert(-x);
up.insert(x-1); lo.insert(1-x);
if (x-1-l)
ans.insert(x-1-l);
if (r-x)
ans.insert(r-x);
ans.insert(1);
}
cout<<*ans.rbegin()<<' ';
}
}