#include<bits/stdc++.h>
#define ll int
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
#define sz(a) ((ll)(a).size())
using namespace std;
const ll maxn=300005;
vector <ll> upd[maxn];
vector <pll> Q[maxn];
ll n, q, a[maxn], answer[maxn];
vector <pair<pll, pll>> U[maxn];
ll cnt[maxn/100][maxn/100];
namespace intervals
{
set <pair<pll, ll>> s;
void init() {s.insert({{1, q}, 1}), U[1].pb({{1, q}, {1, 1}});}
void add(ll l, ll r, ll t)
{
vector <pair<pll, ll>> sn;
auto ptr=s.lower_bound(mp(mp(l, 0), 0));
if (ptr!=s.begin()) ptr--;
while (ptr!=s.end() && (*ptr).fi.fi<=r)
sn.pb(*ptr), ptr=s.erase(ptr);
s.insert({{l, r}, t+1}), U[t].pb({{l, r}, {t+1, 1}});
for (ll i=0; i<sz(sn); i++)
{
ll L=sn[i].fi.fi, R=sn[i].fi.se, V=sn[i].se;
if (R<l || L>r) s.insert({{L, R}, V});
else if (l<=L && R<=r) U[t].pb({{L, R}, {V, -1}});
else if (L<l && r<R) U[t].pb({{l, r}, {V, -1}}), s.insert({{L, l-1}, V}), s.insert({{r+1, R}, V});
else if (L<l) U[t].pb({{l, R}, {V, -1}}), s.insert({{L, l-1}, V});
else U[t].pb({{L, r}, {V, -1}}), s.insert({{r+1, R}, V});
}
}
}
struct BIT2D
{
ll n;
vector <vector <ll>> pos, bit;
BIT2D(ll n): n(n) {bit.resize(n+1);}
void fupdate(ll x, ll y)
{
for (; x<=n; x+=x&(-x))
pos[x].pb(y);
}
void build()
{
for (ll i=1; i<=n; i++)
{
pos[i].pb(0), sort(pos[i].begin(), pos[i].end());
pos[i].resize(unique(pos[i].begin(), pos[i].end())-pos[i].begin());
bit[i].assign(sz(pos[i]), 0);
}
}
void update(ll x, ll y, ll val)
{
for (; x<=n; x+=x&(-x))
for (ll j=lower_bound(pos[x].begin(), pos[x].end(), y)-pos[x].begin(); j<sz(pos[x]); j+=j&(-j))
bit[x][j]+=val;
}
ll query(ll x, ll y)
{
ll ans=0;
for (; x; x-=x&(-x))
for (ll j=upper_bound(pos[x].begin(), pos[x].end(), y)-pos[x].begin()-1; j; j-=j&(-j))
ans+=bit[x][j];
return ans;
}
};
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> q;
for (ll i=1; i<=n; i++) {char c; cin >> c; a[i]=c=='1';}
for (ll i=1; i<=q; i++) answer[i]=-1;
for (ll i=1; i<=q; i++)
{
string t; cin >> t;
if (t=="query") {ll l, r; cin >> l >> r, Q[r-1].pb({l, i}), answer[i]=0;}
else {ll x; cin >> x, upd[x].pb(i);}
}
intervals::init();
for (ll i=1; i<=n; i++)
{
upd[i].pb(q);
for (ll j=0, s=a[i]; j<sz(upd[i]); j++, s^=1)
if (!s) intervals::add(j?upd[i][j-1]+1:1, upd[i][j], i);
}
for (ll i=1; i<=n; i++)
{
for (ll j=0; j<sz(U[i]); j++)
for (ll k=U[i][j].fi.fi; k<=U[i][j].fi.se; k++)
cnt[U[i][j].se.fi][k]+=U[i][j].se.se;
for (auto [l, id]:Q[i])
for (ll j=1; j<=l; j++)
for (ll k=1; k<=id; k++)
answer[id]+=cnt[j][k];
}
for (ll i=1; i<=q; i++)
if (answer[i]>=0) cout << answer[i] << " ";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
23900 KB |
Output is correct |
2 |
Correct |
6 ms |
24152 KB |
Output is correct |
3 |
Correct |
7 ms |
26200 KB |
Output is correct |
4 |
Correct |
6 ms |
26204 KB |
Output is correct |
5 |
Correct |
6 ms |
26200 KB |
Output is correct |
6 |
Correct |
6 ms |
26200 KB |
Output is correct |
7 |
Correct |
6 ms |
23900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5014 ms |
35760 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
36444 KB |
Output is correct |
2 |
Correct |
33 ms |
36444 KB |
Output is correct |
3 |
Correct |
56 ms |
36544 KB |
Output is correct |
4 |
Correct |
87 ms |
24192 KB |
Output is correct |
5 |
Runtime error |
977 ms |
184040 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
36436 KB |
Output is correct |
2 |
Correct |
80 ms |
36432 KB |
Output is correct |
3 |
Correct |
63 ms |
36704 KB |
Output is correct |
4 |
Correct |
11 ms |
36444 KB |
Output is correct |
5 |
Runtime error |
2144 ms |
168472 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
23900 KB |
Output is correct |
2 |
Correct |
6 ms |
24152 KB |
Output is correct |
3 |
Correct |
7 ms |
26200 KB |
Output is correct |
4 |
Correct |
6 ms |
26204 KB |
Output is correct |
5 |
Correct |
6 ms |
26200 KB |
Output is correct |
6 |
Correct |
6 ms |
26200 KB |
Output is correct |
7 |
Correct |
6 ms |
23900 KB |
Output is correct |
8 |
Execution timed out |
5014 ms |
35760 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |