/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 300000;
const int Q_MAX = 300000;
int n, q;
bool light[N_MAX + 2];
struct Query
{
string type;
int pos;
int l, r;
};
Query queries[Q_MAX + 2];
int lLenPre[(N_MAX + 1) + 2];
int lLen[Q_MAX + 2];
int rLen[Q_MAX + 2];
bool full[Q_MAX + 2];
struct SGTNode
{
int len;
int pref;
int suff;
};
SGTNode join (const SGTNode &u, const SGTNode &v)
{
return SGTNode
{
u.len + v.len,
(u.pref == u.len ? u.len + v.pref : u.pref),
(v.suff == v.len ? v.len + u.suff : v.suff)
};
}
SGTNode SGT[N_MAX * 4 + 2];
void build (int node, int l, int r)
{
if(l == r)
{
SGT[node] = SGTNode{1, light[l], light[l]};
return;
}
int mid = (l + r) / 2;
int lSon = node * 2, rSon = node * 2 + 1;
build(lSon, l, mid);
build(rSon, mid + 1, r);
SGT[node] = join(SGT[lSon], SGT[rSon]);
}
void build ()
{
build(1, 1, n);
}
void update (int node, int l, int r, int upos)
{
if(l == r)
{
SGT[node].pref = 1 - SGT[node].pref;
SGT[node].suff = 1 - SGT[node].suff;
return;
}
int mid = (l + r) / 2;
int lSon = node * 2, rSon = node * 2 + 1;
if(upos <= mid)
update(lSon, l, mid, upos);
else
update(rSon, mid + 1, r, upos);
SGT[node] = join(SGT[lSon], SGT[rSon]);
}
void update (int upos)
{
update(1, 1, n, upos);
}
SGTNode query (int node, int l, int r, int ql, int qr)
{
if(ql <= l && r <= qr)
return SGT[node];
int mid = (l + r) / 2;
int lSon = node * 2, rSon = node * 2 + 1;
if(ql <= mid && mid + 1 <= qr)
return join(query(lSon, l, mid, ql, qr), query(rSon, mid + 1, r, ql, qr));
else if(ql <= mid)
return query(lSon, l, mid, ql, qr);
else
return query(rSon, mid + 1, r, ql, qr);
}
SGTNode query (int ql, int qr)
{
if(ql > qr)
return SGTNode{0, 0, 0};
return query(1, 1, n, ql, qr);
}
vector <int> BITpos[(N_MAX + 1) + 2];
vector <int> BIT[(N_MAX + 1) + 2];
void BITupdateSim (int x, int y)
{
for(int i = x; i <= n + 1; i += i & -i)
for(int j = y; j <= n + 1; j += j & -j)
BITpos[i].push_back(j);
}
void BITupdateSim (int lx, int rx, int ly, int ry)
{
if(lx > rx || ly > ry)
return;
BITupdateSim(lx, ly);
BITupdateSim(rx + 1, ly);
BITupdateSim(lx, ry + 1);
BITupdateSim(rx + 1, ry + 1);
}
void BITquerySim (int x, int y)
{
for(int i = x; i >= 1; i -= i & -i)
for(int j = y; j >= 1; j -= j & -j)
BITpos[i].push_back(j);
}
void compress (int i)
{
sort(BITpos[i].begin(), BITpos[i].end());
BITpos[i].resize(unique(BITpos[i].begin(), BITpos[i].end()) - BITpos[i].begin());
BIT[i].resize((int) BITpos[i].size() + 1);
}
int getPos (int i, int y)
{
int l = 0, r = (int) BITpos[i].size();
while(l < r)
{
int mid = (l + r) / 2;
if(BITpos[i][mid] < y)
l = mid + 1;
else
r = mid;
}
assert(l < (int) BITpos[i].size());
return l + 1;
}
void BITupdate (int x, int y, int uval)
{
if(x > n + 1 || y > n + 1)
return;
for(int i = x; i <= n + 1; i += i & -i)
for(int j = getPos(i, y); j <= (int) BITpos[i].size(); j += j & -j)
BIT[i][j] += uval;
}
void BITupdate (int lx, int rx, int ly, int ry, int uval)
{
if(lx > rx || ly > ry)
return;
BITupdate(lx, ly, uval);
BITupdate(rx + 1, ly, -uval);
BITupdate(lx, ry + 1, -uval);
BITupdate(rx + 1, ry + 1, uval);
}
int BITquery (int x, int y)
{
if(x < 1 || y < 1)
return 0;
int answer = 0;
for(int i = x; i >= 1; i -= i & -i)
for(int j = getPos(i, y); j >= 1; j -= j & -j)
answer += BIT[i][j];
return answer;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
string s;
cin >> s;
for(int i = 1; i <= n; i++)
light[i] = (s[i - 1] == '1');
for(int qi = 1; qi <= q; qi++)
{
cin >> queries[qi].type;
if(queries[qi].type == "toggle")
cin >> queries[qi].pos;
else
cin >> queries[qi].l >> queries[qi].r;
}
build();
for(int i = 2; i <= n + 1; i++)
if(light[i - 1] == true)
{
lLenPre[i] = query(1, i - 1).suff;
BITupdateSim(i - lLenPre[i], i - 1, i, i);
}
for(int qi = 1; qi <= q; qi++)
{
if(queries[qi].type == "toggle")
{
int upos = queries[qi].pos;
lLen[qi] = query(1, upos - 1).suff;
rLen[qi] = query(upos + 1, n).pref;
if(light[upos] == true)
BITupdateSim(upos - lLen[qi], upos, upos + 1, upos + 1 + rLen[qi]);
light[upos] = !light[upos];
update(upos);
if(light[upos] == true)
BITupdateSim(upos - lLen[qi], upos, upos + 1, upos + 1 + rLen[qi]);
}
else
{
int l = queries[qi].l;
int r = queries[qi].r;
BITquerySim(l, r);
full[qi] = (query(l, r - 1).pref == r - l);
}
}
for(int i = 1; i <= n + 1; i++)
compress(i);
for(int i = 1; i <= n; i++)
light[i] = (s[i - 1] == '1');
for(int i = 2; i <= n + 1; i++)
if(light[i - 1] == true)
BITupdate(i - lLenPre[i], i - 1, i, i, + q);
for(int qi = 1; qi <= q; qi++)
{
if(queries[qi].type == "toggle")
{
int upos = queries[qi].pos;
if(light[upos] == true)
BITupdate(upos - lLen[qi], upos, upos + 1, upos + 1 + rLen[qi], - (q - qi));
light[upos] = !light[upos];
if(light[upos] == true)
BITupdate(upos - lLen[qi], upos, upos + 1, upos + 1 + rLen[qi], + (q - qi));
}
else
{
int l = queries[qi].l;
int r = queries[qi].r;
int answer = BITquery(l, r);
if(full[qi] == true)
answer -= (q - qi);
cout << answer << "\n";
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
28460 KB |
Output is correct |
2 |
Correct |
17 ms |
28520 KB |
Output is correct |
3 |
Correct |
16 ms |
28492 KB |
Output is correct |
4 |
Correct |
16 ms |
28564 KB |
Output is correct |
5 |
Correct |
16 ms |
28588 KB |
Output is correct |
6 |
Correct |
16 ms |
28492 KB |
Output is correct |
7 |
Correct |
17 ms |
28564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
662 ms |
81372 KB |
Output is correct |
2 |
Correct |
1022 ms |
108464 KB |
Output is correct |
3 |
Correct |
2073 ms |
203840 KB |
Output is correct |
4 |
Runtime error |
1189 ms |
524292 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
29132 KB |
Output is correct |
2 |
Correct |
22 ms |
29272 KB |
Output is correct |
3 |
Correct |
23 ms |
29320 KB |
Output is correct |
4 |
Correct |
27 ms |
29548 KB |
Output is correct |
5 |
Runtime error |
1431 ms |
524292 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
29004 KB |
Output is correct |
2 |
Correct |
22 ms |
29208 KB |
Output is correct |
3 |
Correct |
23 ms |
29360 KB |
Output is correct |
4 |
Correct |
25 ms |
29504 KB |
Output is correct |
5 |
Runtime error |
4505 ms |
524292 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
28460 KB |
Output is correct |
2 |
Correct |
17 ms |
28520 KB |
Output is correct |
3 |
Correct |
16 ms |
28492 KB |
Output is correct |
4 |
Correct |
16 ms |
28564 KB |
Output is correct |
5 |
Correct |
16 ms |
28588 KB |
Output is correct |
6 |
Correct |
16 ms |
28492 KB |
Output is correct |
7 |
Correct |
17 ms |
28564 KB |
Output is correct |
8 |
Correct |
662 ms |
81372 KB |
Output is correct |
9 |
Correct |
1022 ms |
108464 KB |
Output is correct |
10 |
Correct |
2073 ms |
203840 KB |
Output is correct |
11 |
Runtime error |
1189 ms |
524292 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |