#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#define pb push_back
#define mp make_pair
#define taskname "A"
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef tree <int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const int maxn = 3e5 + 5;
const int logn = log2(maxn) + 1;
struct BIT{
vector<int> val, bit;
BIT(){val.clear();bit.clear();}
void init(){
sort(val.begin(),val.end());
val.erase(unique(val.begin(),val.end()),val.end());
bit.resize(val.size() , 0);
}
void update(int x , int delta){
for( ; x <= val.size() ; x += x & -x){
bit[x - 1] += delta;
}
}
void update_range(int l , int h , int delta){
if(val.size() == 0)return;
update(lower_bound(val.begin(),val.end(),l)-val.begin()+1 , delta);
update(upper_bound(val.begin(),val.end(),h)-val.begin()+1, -delta);
}
int query(int x){
int res = 0;
for( x = upper_bound(val.begin(),val.end(),x)-val.begin() ; x > 0 ; x &= x - 1){
res += bit[x - 1];
}
return res;
}
}BIT2d[maxn];
int type[maxn] , a[maxn] , b[maxn];
bool state[maxn];
int n , q;
void fakeupdate(int a , int b){
for( ; a > 0 ; a &= a - 1){
BIT2d[a].val.pb(b);
}
}
void update(int l , int h , int L , int H , int delta){
for( ; l <= n ; l += l & -l){
BIT2d[l].update_range(L,H,delta);
}
for(++h ; h <= n ; h += h & -h){
BIT2d[h].update_range(L,H,-delta);
}
}
int query(int a , int b){
int res = 0;
for( ; a > 0 ; a &= a - 1){
res += BIT2d[a].query(b);
}
return res;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
if(fopen(taskname".INP","r")){
freopen(taskname".INP", "r",stdin);
freopen(taskname".OUT", "w",stdout);
}
cin >> n >> q;
string s;cin >> s;
set<int> offset;offset.insert(n + 1);offset.insert(0);
for(int i = 1 ; i <= n ; ++i){
state[i] = (s[i - 1] == '1');
if(!state[i])offset.insert(i);
}
for(int i = 1 ; i <= q ; ++i){
cin >> s;
if(s[0] == 'q'){
type[i] = 0;
cin >> a[i] >> b[i];
fakeupdate(a[i],--b[i]);
}else{
type[i] = 1;
cin >> a[i];
}
}
for(int i = 1 ; i <= n ; ++i){
BIT2d[i].init();
}
for(int i = 1 ; i <= q ; ++i){
if(type[i] == 0){
int res = query(a[i] , b[i]);
auto it = offset.lower_bound(a[i]);
if(*prev(it) < a[i] && (*it) > b[i])res += i;
cout << res << '\n';
}else{
int x = a[i];
if(state[x] == 0){//merge seq
offset.erase(x);
auto it = offset.lower_bound(x);
update(*prev(it) + 1 , x , x, (*it) - 1 , -i);
}else{//split seq
auto it = offset.lower_bound(x);
update(*prev(it) + 1 , x , x, (*it) - 1 , i);
offset.insert(x);
}
state[i] ^= 1;
}
}
}
Compilation message
street_lamps.cpp: In member function 'void BIT::update(int, int)':
street_lamps.cpp:29:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for( ; x <= val.size() ; x += x & -x){
~~^~~~~~~~~~~~~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:79:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(taskname".INP", "r",stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
street_lamps.cpp:80:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(taskname".OUT", "w",stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
14464 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
165 ms |
24688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Incorrect |
14 ms |
14592 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
14592 KB |
Output is correct |
2 |
Correct |
14 ms |
14712 KB |
Output is correct |
3 |
Incorrect |
13 ms |
14592 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
13 ms |
14464 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |