#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 3e5;
int N, Q, A[MAXN+10];
set<int> S, E;
pii range(int x) { return pii(*(--S.upper_bound(x)), *E.lower_bound(x)); }
pll operator + (const pll &p, const pll &q) { return pll(p.first+q.first, p.second+q.second); }
struct Node1
{
int pos; pll val;
Node1 *lc, *rc;
Node1() : pos(-1), lc(NULL), rc(NULL) {}
};
Node1 *tree[MAXN+10];
void update1(Node1 *node, int tl, int tr, int x, pll val)
{
node->val=node->val+val;
if(tl==tr) return;
int mid=tl+tr>>1;
if(node->pos!=-1)
{
if(node->pos<=mid)
{
node->lc=new Node1();
node->lc->val=node->val;
node->lc->pos=node->pos;
}
else
{
node->rc=new Node1();
node->rc->val=node->val;
node->rc->pos=node->pos;
}
node->pos=-1;
}
if(x<=mid)
{
if(node->lc==NULL)
{
node->lc=new Node1();
node->lc->val=val;
node->lc->pos=x;
}
else update1(node->lc, tl, mid, x, val);
}
else
{
if(node->rc==NULL)
{
node->rc=new Node1();
node->rc->val=val;
node->rc->pos=x;
}
else update1(node->rc, mid+1, tr, x, val);
}
}
pll query1(Node1 *node, int tl, int tr, int l, int r)
{
if(tr<l || r<tl) return pll(0, 0);
if(l<=tl && tr<=r) return node->val;
int mid=tl+tr>>1;
if(node->pos!=-1)
{
if(l<=node->pos && node->pos<=r) return node->val;
else return pll(0, 0);
}
pll ret(0, 0);
if(node->lc!=NULL) ret=ret+query1(node->lc, tl, mid, l, r);
if(node->rc!=NULL) ret=ret+query1(node->rc, mid+1, tr, l, r);
return ret;
}
void update2(int y, int x, pll val)
{
int i, j; y=N-y+1;
for(i=y; i<=N; i+=(i&-i)) update1(tree[i], 1, N, x, val);
}
pll query2(int y, int x)
{
int i, j;
pll ret(0, 0); y=N-y+1;
for(i=y; i>0; i-=(i&-i)) ret=ret+query1(tree[i], 1, N, 1, x);
return ret;
}
int main()
{
int i, j;
scanf("%d%d", &N, &Q);
for(i=1; i<=N; i++) scanf("%1d", &A[i]);
for(i=1; i<=N; i++) tree[i]=new Node1();
set<int>::iterator it, jt;
for(i=1; i<=N; i++) if(A[i-1]==0 && A[i]==1) S.insert(i);
for(i=1; i<=N; i++) if(A[i]==1 && A[i+1]==0) E.insert(i);
for(it=S.begin(), jt=E.begin(); it!=S.end() && jt!=E.end(); it++, jt++) update2(*jt, *it, pll(Q+1, 1));
for(i=1; i<=Q; i++)
{
char st[10];
int a, b;
scanf("%s", st);
if(st[0]=='t')
{
scanf("%d", &a);
if(A[a]==0)
{
if(S.find(a+1)!=S.end() && E.find(a-1)!=E.end())
{
update2(range(a-1).second, range(a-1).first, pll(-(Q-i+1), -1));
update2(range(a+1).second, range(a+1).first, pll(-(Q-i+1), -1));
S.erase(a+1);
E.erase(a-1);
}
else if(S.find(a+1)!=S.end())
{
update2(range(a+1).second, range(a+1).first, pll(-(Q-i+1), -1));
S.erase(a+1);
S.insert(a);
}
else if(E.find(a-1)!=E.end())
{
update2(range(a-1).second, range(a-1).first, pll(-(Q-i+1), -1));
E.erase(a-1);
E.insert(a);
}
else
{
S.insert(a);
E.insert(a);
}
update2(range(a).second, range(a).first, pll((Q-i+1), 1));
A[a]=1;
}
else
{
int s, e;
tie(s, e)=range(a);
update2(range(a).second, range(a).first, pll(-(Q-i+1), -1));
if(s==e)
{
S.erase(a);
E.erase(a);
}
else if(s==a)
{
S.erase(a);
S.insert(a+1);
update2(range(a+1).second, range(a+1).first, pll((Q-i+1), 1));
}
else if(e==a)
{
E.erase(a);
E.insert(a-1);
update2(range(a-1).second, range(a-1).first, pll((Q-i+1), 1));
}
else
{
S.insert(a+1);
E.insert(a-1);
update2(range(a-1).second, range(a-1).first, pll((Q-i+1), 1));
update2(range(a+1).second, range(a+1).first, pll((Q-i+1), 1));
}
A[a]=0;
}
}
else
{
scanf("%d%d", &a, &b); b--;
pll ret=query2(b, a);
printf("%lld\n", ret.first-ret.second*(Q-i+1));
}
}
}
Compilation message
street_lamps.cpp: In function 'void update1(Node1*, int, int, int, pll)':
street_lamps.cpp:30:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
street_lamps.cpp: In function 'pll query1(Node1*, int, int, int, int)':
street_lamps.cpp:74:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
street_lamps.cpp: In function 'void update2(int, int, pll)':
street_lamps.cpp:88:12: warning: unused variable 'j' [-Wunused-variable]
int i, j; y=N-y+1;
^
street_lamps.cpp: In function 'pll query2(int, int)':
street_lamps.cpp:94:12: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:102:12: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
street_lamps.cpp:104:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &Q);
~~~~~^~~~~~~~~~~~~~~~
street_lamps.cpp:105:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=1; i<=N; i++) scanf("%1d", &A[i]);
~~~~~^~~~~~~~~~~~~~
street_lamps.cpp:119:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", st);
~~~~~^~~~~~~~~~
street_lamps.cpp:122:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a);
~~~~~^~~~~~~~~~
street_lamps.cpp:188:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a, &b); b--;
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
297 ms |
4304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1144 KB |
Output is correct |
2 |
Incorrect |
5 ms |
1144 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Incorrect |
4 ms |
760 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |