#include<bits/stdc++.h>
using namespace std;
const int MAXN=3e5+6;
struct BIT
{
int a[MAXN];
void update(int idx,int val)
{
for(;idx<MAXN;idx+=idx&(-idx))
{
a[idx]+=val;
}
}
int query(int idx)
{
int ret=0;
for(;idx>0;idx-=idx&(-idx))
{
ret+=a[idx];
}
return ret;
}
}t1,t2;
int ans[MAXN];
struct point
{
int x,y,z,ind,val;
bool operator<(point const& other)const
{
if(x!=other.x)return x<other.x;
if(y!=other.y)return y<other.y;
return z<other.z;
}
};
bool cmp(point p1,point p2)
{
if(p1.y!=p2.y)return p1.y<p2.y;
if(p1.z!=p2.z)return p1.z>p2.z;
return p1.ind==0;
}
vector<point>v;
void dq(int l,int r)
{
if(l+1==r)return;
int mid=(l+r)/2;
dq(l,mid);
dq(mid,r);
vector<point>tmp;
vector<pair<int,int> >changes;
int i1=l,i2=mid;
while(i1<mid&&i2<r)
{
if(cmp(v[i1],v[i2]))
{
tmp.push_back(v[i1]);
t1.update(v[i1].z,v[i1].val);
if(v[i1].val>0)t2.update(v[i1].z,-1);
if(v[i1].val<0)t2.update(v[i1].z,+1);
changes.push_back({v[i1].z,v[i1].val});
i1++;
}
else
{
tmp.push_back(v[i2]);
//cout<<v[i2].ind<<" "<<t.query(MAXN-1)<<" "<<t.query(v[i2].z-1)<<endl;
ans[v[i2].ind]+=(t1.query(MAXN-1)-t1.query(v[i2].z-1))+v[i2].x*(t2.query(MAXN-1)-t2.query(v[i2].z-1));
i2++;
}
}
while(i1<mid)
{
tmp.push_back(v[i1]);
i1++;
}
while(i2<r)
{
tmp.push_back(v[i2]);
ans[v[i2].ind]+=(t1.query(MAXN-1)-t1.query(v[i2].z-1))+v[i2].x*(t2.query(MAXN-1)-t2.query(v[i2].z-1));
i2++;
}
for(int i=0;i<tmp.size();i++)v[l+i]=tmp[i];
for(auto xd:changes)
{
t1.update(xd.first,-xd.second);
if(xd.second>0)t2.update(xd.first,+1);
if(xd.second<0)t2.update(xd.first,-1);
}
}
set<pair<int,int> >segments;
bool lit[MAXN];
#define time aver
int n,q,time;
void pog_insert(int l,int r)
{
v.push_back({time,l,r,0,-time});
segments.insert({l,r});
}
void pog_erase(int l,int r)
{
v.push_back({time,l,r,0,+time});
segments.erase(make_pair(l,r));
}
void toggle(int i)
{
if(lit[i]==1)
{
auto tt=segments.lower_bound({i+1,-69});
tt--;
int l=(*tt).first,r=(*tt).second;
if(l<=i-1)pog_insert(l,i-1);
if(i+1<=r)pog_insert(i+1,r);
pog_erase(l,r);
lit[i]=0;
}
else
{
if(!lit[i-1]&&!lit[i+1])
{
pog_insert(i,i);
}
else if(!lit[i-1]&&lit[i+1])
{
auto tt=segments.lower_bound({i,-69});
int l=(*tt).first,r=(*tt).second;
pog_erase(l,r);
pog_insert(i,r);
}
else if(lit[i-1]&&!lit[i+1])
{
auto tt=segments.lower_bound({i,-69});
tt--;
int l=(*tt).first,r=(*tt).second;
pog_erase(l,r);
pog_insert(l,i);
}
else
{
auto tt=segments.lower_bound({i,-69});
int l1=(*tt).first,r1=(*tt).second;
tt--;
int l2=(*tt).first,r2=(*tt).second;
pog_erase(l1,r1);
pog_erase(l2,r2);
pog_insert(l2,r1);
}
lit[i]=!lit[i];
}
}
int main()
{
cin>>n>>q;
time++;
for(int i=1;i<=n;i++)
{
char c;cin>>c;
if(c=='1')toggle(i);
}
int cnt=0;
for(int i=1;i<=q;i++)
{
time++;
string op;
cin>>op;
if(op=="toggle")
{
int x;
cin>>x;
toggle(x);
}
else
{
cnt++;
int a,b;
cin>>a>>b;b--;
v.push_back({time,a,b,cnt,0});
}
}
sort(v.begin(),v.end());
/*for(int i=0;i<v.size();i++)
{
cout<<v[i].x<<" "<<v[i].y<<" "<<v[i].z<<" "<<v[i].ind<<" "<<v[i].val<<endl;
}*/
dq(0,v.size());
/*cout<<endl<<endl;
for(int i=0;i<v.size();i++)
{
cout<<v[i].x<<" "<<v[i].y<<" "<<v[i].z<<" "<<v[i].ind<<" "<<v[i].val<<endl;
}*/
for(int i=1;i<=cnt;i++)
{
cout<<ans[i]<<"\n";
}
//cout<<endl;
return 0;
}
Compilation message
street_lamps.cpp: In function 'void dq(int, int)':
street_lamps.cpp:81:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for(int i=0;i<tmp.size();i++)v[l+i]=tmp[i];
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1039 ms |
32284 KB |
Output is correct |
2 |
Correct |
1061 ms |
32476 KB |
Output is correct |
3 |
Correct |
1076 ms |
33140 KB |
Output is correct |
4 |
Correct |
1686 ms |
59308 KB |
Output is correct |
5 |
Correct |
1796 ms |
62928 KB |
Output is correct |
6 |
Correct |
1699 ms |
60372 KB |
Output is correct |
7 |
Correct |
769 ms |
30664 KB |
Output is correct |
8 |
Correct |
1352 ms |
65964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
616 KB |
Output is correct |
2 |
Correct |
5 ms |
620 KB |
Output is correct |
3 |
Correct |
5 ms |
620 KB |
Output is correct |
4 |
Correct |
4 ms |
620 KB |
Output is correct |
5 |
Correct |
1546 ms |
54220 KB |
Output is correct |
6 |
Correct |
1673 ms |
58060 KB |
Output is correct |
7 |
Correct |
1812 ms |
63100 KB |
Output is correct |
8 |
Correct |
1384 ms |
66032 KB |
Output is correct |
9 |
Correct |
465 ms |
18884 KB |
Output is correct |
10 |
Correct |
520 ms |
25220 KB |
Output is correct |
11 |
Correct |
516 ms |
25348 KB |
Output is correct |
12 |
Correct |
770 ms |
30840 KB |
Output is correct |
13 |
Correct |
1375 ms |
66092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
768 KB |
Output is correct |
2 |
Correct |
4 ms |
620 KB |
Output is correct |
3 |
Correct |
5 ms |
620 KB |
Output is correct |
4 |
Correct |
5 ms |
620 KB |
Output is correct |
5 |
Correct |
1236 ms |
55060 KB |
Output is correct |
6 |
Correct |
1495 ms |
57612 KB |
Output is correct |
7 |
Correct |
1680 ms |
60144 KB |
Output is correct |
8 |
Correct |
1951 ms |
63872 KB |
Output is correct |
9 |
Correct |
1240 ms |
35136 KB |
Output is correct |
10 |
Correct |
1381 ms |
47176 KB |
Output is correct |
11 |
Correct |
1199 ms |
34912 KB |
Output is correct |
12 |
Correct |
1389 ms |
47184 KB |
Output is correct |
13 |
Correct |
1207 ms |
34960 KB |
Output is correct |
14 |
Correct |
1377 ms |
47476 KB |
Output is correct |
15 |
Correct |
775 ms |
30808 KB |
Output is correct |
16 |
Correct |
1360 ms |
65860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1039 ms |
32284 KB |
Output is correct |
9 |
Correct |
1061 ms |
32476 KB |
Output is correct |
10 |
Correct |
1076 ms |
33140 KB |
Output is correct |
11 |
Correct |
1686 ms |
59308 KB |
Output is correct |
12 |
Correct |
1796 ms |
62928 KB |
Output is correct |
13 |
Correct |
1699 ms |
60372 KB |
Output is correct |
14 |
Correct |
769 ms |
30664 KB |
Output is correct |
15 |
Correct |
1352 ms |
65964 KB |
Output is correct |
16 |
Correct |
4 ms |
616 KB |
Output is correct |
17 |
Correct |
5 ms |
620 KB |
Output is correct |
18 |
Correct |
5 ms |
620 KB |
Output is correct |
19 |
Correct |
4 ms |
620 KB |
Output is correct |
20 |
Correct |
1546 ms |
54220 KB |
Output is correct |
21 |
Correct |
1673 ms |
58060 KB |
Output is correct |
22 |
Correct |
1812 ms |
63100 KB |
Output is correct |
23 |
Correct |
1384 ms |
66032 KB |
Output is correct |
24 |
Correct |
465 ms |
18884 KB |
Output is correct |
25 |
Correct |
520 ms |
25220 KB |
Output is correct |
26 |
Correct |
516 ms |
25348 KB |
Output is correct |
27 |
Correct |
770 ms |
30840 KB |
Output is correct |
28 |
Correct |
1375 ms |
66092 KB |
Output is correct |
29 |
Correct |
4 ms |
768 KB |
Output is correct |
30 |
Correct |
4 ms |
620 KB |
Output is correct |
31 |
Correct |
5 ms |
620 KB |
Output is correct |
32 |
Correct |
5 ms |
620 KB |
Output is correct |
33 |
Correct |
1236 ms |
55060 KB |
Output is correct |
34 |
Correct |
1495 ms |
57612 KB |
Output is correct |
35 |
Correct |
1680 ms |
60144 KB |
Output is correct |
36 |
Correct |
1951 ms |
63872 KB |
Output is correct |
37 |
Correct |
1240 ms |
35136 KB |
Output is correct |
38 |
Correct |
1381 ms |
47176 KB |
Output is correct |
39 |
Correct |
1199 ms |
34912 KB |
Output is correct |
40 |
Correct |
1389 ms |
47184 KB |
Output is correct |
41 |
Correct |
1207 ms |
34960 KB |
Output is correct |
42 |
Correct |
1377 ms |
47476 KB |
Output is correct |
43 |
Correct |
775 ms |
30808 KB |
Output is correct |
44 |
Correct |
1360 ms |
65860 KB |
Output is correct |
45 |
Correct |
675 ms |
24336 KB |
Output is correct |
46 |
Correct |
694 ms |
25248 KB |
Output is correct |
47 |
Correct |
890 ms |
31084 KB |
Output is correct |
48 |
Correct |
1658 ms |
59476 KB |
Output is correct |
49 |
Correct |
590 ms |
27076 KB |
Output is correct |
50 |
Correct |
586 ms |
27196 KB |
Output is correct |
51 |
Correct |
596 ms |
27332 KB |
Output is correct |
52 |
Correct |
588 ms |
27692 KB |
Output is correct |
53 |
Correct |
590 ms |
27728 KB |
Output is correct |
54 |
Correct |
592 ms |
27824 KB |
Output is correct |