#include<bits/stdc++.h>
using namespace std;
struct Fenwick
{
vector<int> s;
Fenwick(int n) : s(n+1){}
int qry(int poz)
{
int aux=0;
for(int i=poz;i<s.size();i+=(i&(-i)))
aux += s[i];
return aux;
}
void upd(int poz, int newv)
{
for(int i=poz;i>0;i-=(i&(-i)))
s[i] += newv;
}
};
struct FT2
{
vector<vector<int>> ys;
vector<Fenwick> ft;
FT2(int limx) : ys(limx+1){}
void fake_upd(int pozx, int pozy)
{
for(int i=pozx;i<ys.size();i+=(i&(-i)))
ys[i].push_back(pozy);
}
void init()
{
for(vector<int> &v:ys)
{
sort(v.begin(),v.end());
ft.push_back(v.size());
}
}
int ind(int x, int y)
{
return (int)(lower_bound(ys[x].begin(),ys[x].end(),y) - ys[x].begin() + 1);
}
int qry(int pozx, int pozy)
{
int aux=0;
for(int i=pozx;i>0;i-=(i&(-i)))
aux += ft[i].qry(ind(i,pozy));
return aux;
}
void upd(int pozx, int pozy, int newv)
{
for(int i=pozx;i<ys.size();i+=(i&(-i)))
ft[i].upd(ind(i,pozy),newv);
}
};
set<pair<pair<int,int>,int>> s;
bool tip[300005];
int cit[300005][2];
signed main()
{
int n,q;
cin>>n>>q;
FT2 old(n),active_sum(n),active_cnt(n);
vector<bool> init(n+2,0);
char ch;
for(int i=1;i<=n;i++)
{
cin>>ch;
if(ch=='1') init[i]=1;
else init[i]=0;
}
vector<bool> stare=init;
int inc=-1;
for(int i=1;i<=n;i++)
{
if(stare[i]==1)
{
if(inc==-1) inc=i;
if(i==n || stare[i+1]==0)
{
s.insert({{inc,i},0});
active_cnt.fake_upd(inc,i);
active_sum.fake_upd(inc,i);
//cout<<inc<<" "<<i<<" in set\n";
inc=-1;
}
}
}
string str;
int a,b;
for(int i=1;i<=q;i++)
{
cin>>str;
if(str=="toggle")
tip[i]=0;
else
tip[i]=1;
if(tip[i]==0)
{
cin>>cit[i][0];
a=cit[i][0];
if(stare[a]==0)
{
int le=a,ri=a;
if(stare[a-1]==1)
{
auto it = prev(s.lower_bound({{a,-1},-1}));
le = (*it).first.first;
old.fake_upd((*it).first.first,(*it).first.second);
active_cnt.fake_upd((*it).first.first,(*it).first.second);
active_sum.fake_upd((*it).first.first,(*it).first.second);
s.erase(it);
}
if(stare[a+1]==1)
{
auto it = s.lower_bound({{a,-1},-1});
ri = (*it).first.second;
old.fake_upd((*it).first.first,(*it).first.second);
active_cnt.fake_upd((*it).first.first,(*it).first.second);
active_sum.fake_upd((*it).first.first,(*it).first.second);
s.erase(it);
}
s.insert({{le,ri},i});
active_cnt.fake_upd(le,ri);
active_sum.fake_upd(le,ri);
}
else
{
auto it = prev(s.lower_bound({{a,n+1},n+1}));
int le = (*it).first.first, ri = (*it).first.second;
old.fake_upd(le,ri);
active_cnt.fake_upd(le,ri);
active_sum.fake_upd(le,ri);
s.erase(it);
if(stare[a-1]==1)
{
s.insert({{le,a-1},i});
active_cnt.fake_upd(le,a-1);
active_sum.fake_upd(le,a-1);
}
if(stare[a+1]==1)
{
s.insert({{a+1,ri},i});
active_cnt.fake_upd(a+1,ri);
active_sum.fake_upd(a+1,ri);
}
}
stare[a] = 1-stare[a];
}
else
cin>>cit[i][0]>>cit[i][1];
}
stare=init;
s.clear();
old.init();
active_cnt.init();
active_sum.init();
inc=-1;
for(int i=1;i<=n;i++)
{
if(stare[i]==1)
{
if(inc==-1) inc=i;
if(i==n || stare[i+1]==0)
{
s.insert({{inc,i},0});
active_cnt.upd(inc,i,+1);
active_sum.upd(inc,i,0);
inc=-1;
}
}
}
for(int i=1;i<=q;i++)
{
if(tip[i]==0)
{
a=cit[i][0];
if(stare[a]==0)
{
int le=a,ri=a;
if(stare[a-1]==1)
{
auto it = prev(s.lower_bound({{a,-1},-1}));
le = (*it).first.first;
old.upd((*it).first.first,(*it).first.second,i-(*it).second);
active_cnt.upd((*it).first.first,(*it).first.second,-1);
active_sum.upd((*it).first.first,(*it).first.second,-(*it).second);
s.erase(it);
}
if(stare[a+1]==1)
{
auto it = s.lower_bound({{a,-1},-1});
ri = (*it).first.second;
old.upd((*it).first.first,(*it).first.second,i-(*it).second);
active_cnt.upd((*it).first.first,(*it).first.second,-1);
active_sum.upd((*it).first.first,(*it).first.second,-(*it).second);
s.erase(it);
}
s.insert({{le,ri},i});
active_cnt.upd(le,ri,+1);
active_sum.upd(le,ri,i);
}
else
{
auto it = prev(s.lower_bound({{a,n+1},n+1}));
int le = (*it).first.first, ri = (*it).first.second;
old.upd(le,ri,i-(*it).second);
active_cnt.upd(le,ri,-1);
active_sum.upd(le,ri,-(*it).second);
s.erase(it);
if(stare[a-1]==1)
{
s.insert({{le,a-1},i});
active_cnt.upd(le,a-1,+1);
active_sum.upd(le,a-1,i);
}
if(stare[a+1]==1)
{
s.insert({{a+1,ri},i});
active_cnt.upd(a+1,ri,+1);
active_sum.upd(a+1,ri,i);
}
}
stare[a] = 1-stare[a];
}
else
{
a=cit[i][0];
b=cit[i][1];
b--;
int rez=0;
rez += old.qry(a,b);
rez += active_cnt.qry(a,b)*i - active_sum.qry(a,b);
cout<<rez<<"\n";
}
}
return 0;
}
Compilation message
street_lamps.cpp: In member function 'int Fenwick::qry(int)':
street_lamps.cpp:10:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | for(int i=poz;i<s.size();i+=(i&(-i)))
| ~^~~~~~~~~
street_lamps.cpp: In member function 'void FT2::fake_upd(int, int)':
street_lamps.cpp:27:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(int i=pozx;i<ys.size();i+=(i&(-i)))
| ~^~~~~~~~~~
street_lamps.cpp: In member function 'void FT2::upd(int, int, int)':
street_lamps.cpp:51:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(int i=pozx;i<ys.size();i+=(i&(-i)))
| ~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
352 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
623 ms |
31104 KB |
Output is correct |
2 |
Correct |
893 ms |
36904 KB |
Output is correct |
3 |
Correct |
1327 ms |
51792 KB |
Output is correct |
4 |
Correct |
2433 ms |
175892 KB |
Output is correct |
5 |
Correct |
2513 ms |
174864 KB |
Output is correct |
6 |
Correct |
2786 ms |
185224 KB |
Output is correct |
7 |
Correct |
323 ms |
91140 KB |
Output is correct |
8 |
Correct |
341 ms |
89412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
856 KB |
Output is correct |
2 |
Correct |
4 ms |
860 KB |
Output is correct |
3 |
Correct |
3 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
3818 ms |
219492 KB |
Output is correct |
6 |
Correct |
3184 ms |
202176 KB |
Output is correct |
7 |
Correct |
2203 ms |
176404 KB |
Output is correct |
8 |
Correct |
273 ms |
90904 KB |
Output is correct |
9 |
Correct |
130 ms |
6484 KB |
Output is correct |
10 |
Correct |
121 ms |
6732 KB |
Output is correct |
11 |
Correct |
117 ms |
6992 KB |
Output is correct |
12 |
Correct |
282 ms |
86516 KB |
Output is correct |
13 |
Correct |
307 ms |
92400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
3 ms |
996 KB |
Output is correct |
5 |
Correct |
663 ms |
109472 KB |
Output is correct |
6 |
Correct |
1557 ms |
145400 KB |
Output is correct |
7 |
Correct |
2674 ms |
184664 KB |
Output is correct |
8 |
Correct |
4066 ms |
242308 KB |
Output is correct |
9 |
Correct |
1114 ms |
55720 KB |
Output is correct |
10 |
Correct |
960 ms |
53520 KB |
Output is correct |
11 |
Correct |
1066 ms |
56244 KB |
Output is correct |
12 |
Correct |
935 ms |
53524 KB |
Output is correct |
13 |
Correct |
1100 ms |
55572 KB |
Output is correct |
14 |
Correct |
952 ms |
53532 KB |
Output is correct |
15 |
Correct |
294 ms |
85236 KB |
Output is correct |
16 |
Correct |
295 ms |
86768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
352 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
623 ms |
31104 KB |
Output is correct |
9 |
Correct |
893 ms |
36904 KB |
Output is correct |
10 |
Correct |
1327 ms |
51792 KB |
Output is correct |
11 |
Correct |
2433 ms |
175892 KB |
Output is correct |
12 |
Correct |
2513 ms |
174864 KB |
Output is correct |
13 |
Correct |
2786 ms |
185224 KB |
Output is correct |
14 |
Correct |
323 ms |
91140 KB |
Output is correct |
15 |
Correct |
341 ms |
89412 KB |
Output is correct |
16 |
Correct |
4 ms |
856 KB |
Output is correct |
17 |
Correct |
4 ms |
860 KB |
Output is correct |
18 |
Correct |
3 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
3818 ms |
219492 KB |
Output is correct |
21 |
Correct |
3184 ms |
202176 KB |
Output is correct |
22 |
Correct |
2203 ms |
176404 KB |
Output is correct |
23 |
Correct |
273 ms |
90904 KB |
Output is correct |
24 |
Correct |
130 ms |
6484 KB |
Output is correct |
25 |
Correct |
121 ms |
6732 KB |
Output is correct |
26 |
Correct |
117 ms |
6992 KB |
Output is correct |
27 |
Correct |
282 ms |
86516 KB |
Output is correct |
28 |
Correct |
307 ms |
92400 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
31 |
Correct |
2 ms |
860 KB |
Output is correct |
32 |
Correct |
3 ms |
996 KB |
Output is correct |
33 |
Correct |
663 ms |
109472 KB |
Output is correct |
34 |
Correct |
1557 ms |
145400 KB |
Output is correct |
35 |
Correct |
2674 ms |
184664 KB |
Output is correct |
36 |
Correct |
4066 ms |
242308 KB |
Output is correct |
37 |
Correct |
1114 ms |
55720 KB |
Output is correct |
38 |
Correct |
960 ms |
53520 KB |
Output is correct |
39 |
Correct |
1066 ms |
56244 KB |
Output is correct |
40 |
Correct |
935 ms |
53524 KB |
Output is correct |
41 |
Correct |
1100 ms |
55572 KB |
Output is correct |
42 |
Correct |
952 ms |
53532 KB |
Output is correct |
43 |
Correct |
294 ms |
85236 KB |
Output is correct |
44 |
Correct |
295 ms |
86768 KB |
Output is correct |
45 |
Correct |
162 ms |
15152 KB |
Output is correct |
46 |
Correct |
362 ms |
21060 KB |
Output is correct |
47 |
Correct |
1208 ms |
77916 KB |
Output is correct |
48 |
Correct |
2297 ms |
173932 KB |
Output is correct |
49 |
Correct |
132 ms |
7252 KB |
Output is correct |
50 |
Correct |
128 ms |
7168 KB |
Output is correct |
51 |
Correct |
131 ms |
7360 KB |
Output is correct |
52 |
Correct |
134 ms |
7760 KB |
Output is correct |
53 |
Correct |
135 ms |
7760 KB |
Output is correct |
54 |
Correct |
136 ms |
7632 KB |
Output is correct |