#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pl;
typedef vector<ll> vl;
#define FD(i, r, l) for(ll i = r; i > (l); --i)
#define K first
#define V second
#define G(x) ll x; cin >> x;
#define GD(x) ld x; cin >> x;
#define GS(s) string s; cin >> s;
#define EX(x) { cout << x << '\n'; exit(0); }
#define A(a) (a).begin(), (a).end()
#define F(i, l, r) for (ll i = l; i < (r); ++i)
#define NN 300010
#define M 1000000007 // 998244353
struct query {
enum {
INSERT,
QUERY
} type;
ll l, r; // [l, r]
ll time;
ll qidx;
query(ll l, ll r, ll time): l(l), r(r), time(time), type(INSERT) {}
query(ll l, ll r, ll time, ll qidx): l(l), r(r), time(time), qidx(qidx), type(QUERY) {}
};
ll ans[NN];
const ll INFTIME = NN;
namespace seg {
typedef pl T;
T id={0, 0};
T f(T a, T b) {return {a.K + b.K, a.V + b.V};}
T t[2 * NN];
ll n=NN; // array size
vl record;
void modify(ll p, T value) { // set value at position p
record.push_back(p);
for (p+=n, t[p] = f(t[p], value); p /= 2;) t[p] = f(t[2*p], t[2*p+1]);
}
void reset() {
for (auto p: record) {
for (p+=n, t[p] = id; p /= 2;) t[p] = f(t[2*p], t[2*p+1]);
}
record.clear();
}
T query(ll l, ll r) { // fold f on interval [l, r)
T resl=id, resr=id;
for (l += n, r += n; l < r; l /= 2, r /= 2) {
if (l&1) resl = f(resl, t[l++]);
if (r&1) resr = f(t[--r], resr);
}
return f(resl, resr);
}
}
void solve(vector<query>::iterator l, vector<query>::iterator r) {
if (r-l <= 1) return;
auto mid = l + (r-l)/2;
solve(l, mid); solve(mid, r);
auto cmp = [](const auto &a, const auto &b) {return a.l < b.l; };
sort(l, mid, cmp); sort(mid, r, cmp);
auto lit = l, rit = mid;
// cdq on {number of points (A <= (l <= r) <= B), sum of weights of ponits }
#define sgn(x) (x < 0 ? -1:1)
while (rit != r) {
if (lit != mid and lit->l <= rit->l) { // insert
auto [type, l, r, time, _] = *lit++;
if (type == query::INSERT) {
seg::modify(r, {(INFTIME - abs(time)) * sgn(time), sgn(time)});
}
} else {
auto [type, l, r, time, qidx] = *rit++;
if (type == query::QUERY) {
auto [contribution, number] = seg::query(r, NN);
contribution -= number * (INFTIME - time);
ans[qidx] += contribution;
}
}
}
seg::reset();
}
int main(){
// freopen("a.in", "r", stdin);
// freopen("a.out", "w", stdout);
ios_base::sync_with_stdio(false); cin.tie(0);
cout << fixed << setprecision(20);
vector<query> queries;
G(n) G(q)
GS(s) set<pl> intervals;
// optimizzation: intstead of calling insert n times, just push back immidiately
auto insert = [&](ll i, ll time) {
pl to_add{i, i};
auto it = intervals.lower_bound({i, i});
if (it != intervals.end() and it->K == i+1) {
to_add.V = it->V;
queries.push_back(query(it->K, it->V, -time));
intervals.erase(it);
}
it = intervals.lower_bound({i, i});
if (it != intervals.begin() and (*--it).V == i-1) {
to_add.K = it->K;
queries.push_back(query(it->K, it->V, -time));
intervals.erase(it);
}
intervals.insert(to_add);
queries.push_back(query(to_add.K, to_add.V, time));
};
auto remove = [&](ll i, ll time) {
auto [l, r] = *--intervals.upper_bound({i, NN});
intervals.erase(--intervals.upper_bound({i, NN}));
queries.push_back(query(l, r, -time));
if (l <= i-1) {
queries.push_back(query(l, i-1, time));
intervals.insert({l, i-1});
}
if (i + 1 <= r) {
queries.push_back(query(i+1, r, time));
intervals.insert({i+1, r});
}
};
{
ll l = -1, r = -1;
F(i, 0, n) {
if (s[i] == '1') {
if (l == -1) l = i;
r = i;
} else {
if (l != -1) {
intervals.insert({l, r});
queries.push_back(query(l, r, 1));
}
l = r = -1;
}
}
if (l != -1) {
intervals.insert({l, r});
queries.push_back(query(l, r, 1));
}
}
ll nq = 0; // qidx indexing
F(time, 2, q+2) {
GS(input)
if (input == "toggle") {
G(i) --i;
if (s[i] == '1') remove(i, time);
else insert(i, time);
s[i] ^=1;
} else {
G(a) G(b)
queries.push_back(query(a-1, b-2, time, nq++));
}
}
// for (auto [type, l, r, time, qidx]: queries) {
// cout << (type == query::INSERT ? "INS":"QUE") << " " << l << " " << r << " " << time << " : " << (type == query::INSERT ? -1: qidx) << endl;
// }
solve(A(queries));
F(i, 0, nq) cout << ans[i] << '\n';
}
Compilation message
street_lamps.cpp: In constructor 'query::query(ll, ll, ll)':
street_lamps.cpp:33:8: warning: 'query::time' will be initialized after [-Wreorder]
33 | ll time;
| ^~~~
street_lamps.cpp:31:7: warning: 'query::<unnamed enum> query::type' [-Wreorder]
31 | } type;
| ^~~~
street_lamps.cpp:36:5: warning: when initialized here [-Wreorder]
36 | query(ll l, ll r, ll time): l(l), r(r), time(time), type(INSERT) {}
| ^~~~~
street_lamps.cpp: In constructor 'query::query(ll, ll, ll, ll)':
street_lamps.cpp:34:8: warning: 'query::qidx' will be initialized after [-Wreorder]
34 | ll qidx;
| ^~~~
street_lamps.cpp:31:7: warning: 'query::<unnamed enum> query::type' [-Wreorder]
31 | } type;
| ^~~~
street_lamps.cpp:37:5: warning: when initialized here [-Wreorder]
37 | query(ll l, ll r, ll time, ll qidx): l(l), r(r), time(time), qidx(qidx), type(QUERY) {}
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4596 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
550 ms |
29856 KB |
Output is correct |
2 |
Correct |
598 ms |
31356 KB |
Output is correct |
3 |
Correct |
748 ms |
33760 KB |
Output is correct |
4 |
Correct |
1014 ms |
52284 KB |
Output is correct |
5 |
Correct |
1037 ms |
52224 KB |
Output is correct |
6 |
Correct |
1062 ms |
52732 KB |
Output is correct |
7 |
Correct |
541 ms |
28872 KB |
Output is correct |
8 |
Correct |
547 ms |
30852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4700 KB |
Output is correct |
2 |
Correct |
2 ms |
4816 KB |
Output is correct |
3 |
Correct |
2 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1176 ms |
50868 KB |
Output is correct |
6 |
Correct |
1178 ms |
51064 KB |
Output is correct |
7 |
Correct |
1020 ms |
52104 KB |
Output is correct |
8 |
Correct |
565 ms |
29976 KB |
Output is correct |
9 |
Correct |
212 ms |
19636 KB |
Output is correct |
10 |
Correct |
235 ms |
24732 KB |
Output is correct |
11 |
Correct |
236 ms |
25524 KB |
Output is correct |
12 |
Correct |
556 ms |
27228 KB |
Output is correct |
13 |
Correct |
565 ms |
30284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4696 KB |
Output is correct |
2 |
Correct |
2 ms |
4700 KB |
Output is correct |
3 |
Correct |
2 ms |
4700 KB |
Output is correct |
4 |
Correct |
3 ms |
4688 KB |
Output is correct |
5 |
Correct |
697 ms |
40724 KB |
Output is correct |
6 |
Correct |
881 ms |
45220 KB |
Output is correct |
7 |
Correct |
1042 ms |
51960 KB |
Output is correct |
8 |
Correct |
1262 ms |
52984 KB |
Output is correct |
9 |
Correct |
718 ms |
33196 KB |
Output is correct |
10 |
Correct |
756 ms |
45228 KB |
Output is correct |
11 |
Correct |
716 ms |
33268 KB |
Output is correct |
12 |
Correct |
755 ms |
45928 KB |
Output is correct |
13 |
Correct |
720 ms |
32984 KB |
Output is correct |
14 |
Correct |
765 ms |
45220 KB |
Output is correct |
15 |
Correct |
554 ms |
27148 KB |
Output is correct |
16 |
Correct |
556 ms |
30744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4596 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
550 ms |
29856 KB |
Output is correct |
9 |
Correct |
598 ms |
31356 KB |
Output is correct |
10 |
Correct |
748 ms |
33760 KB |
Output is correct |
11 |
Correct |
1014 ms |
52284 KB |
Output is correct |
12 |
Correct |
1037 ms |
52224 KB |
Output is correct |
13 |
Correct |
1062 ms |
52732 KB |
Output is correct |
14 |
Correct |
541 ms |
28872 KB |
Output is correct |
15 |
Correct |
547 ms |
30852 KB |
Output is correct |
16 |
Correct |
2 ms |
4700 KB |
Output is correct |
17 |
Correct |
2 ms |
4816 KB |
Output is correct |
18 |
Correct |
2 ms |
4700 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1176 ms |
50868 KB |
Output is correct |
21 |
Correct |
1178 ms |
51064 KB |
Output is correct |
22 |
Correct |
1020 ms |
52104 KB |
Output is correct |
23 |
Correct |
565 ms |
29976 KB |
Output is correct |
24 |
Correct |
212 ms |
19636 KB |
Output is correct |
25 |
Correct |
235 ms |
24732 KB |
Output is correct |
26 |
Correct |
236 ms |
25524 KB |
Output is correct |
27 |
Correct |
556 ms |
27228 KB |
Output is correct |
28 |
Correct |
565 ms |
30284 KB |
Output is correct |
29 |
Correct |
3 ms |
4696 KB |
Output is correct |
30 |
Correct |
2 ms |
4700 KB |
Output is correct |
31 |
Correct |
2 ms |
4700 KB |
Output is correct |
32 |
Correct |
3 ms |
4688 KB |
Output is correct |
33 |
Correct |
697 ms |
40724 KB |
Output is correct |
34 |
Correct |
881 ms |
45220 KB |
Output is correct |
35 |
Correct |
1042 ms |
51960 KB |
Output is correct |
36 |
Correct |
1262 ms |
52984 KB |
Output is correct |
37 |
Correct |
718 ms |
33196 KB |
Output is correct |
38 |
Correct |
756 ms |
45228 KB |
Output is correct |
39 |
Correct |
716 ms |
33268 KB |
Output is correct |
40 |
Correct |
755 ms |
45928 KB |
Output is correct |
41 |
Correct |
720 ms |
32984 KB |
Output is correct |
42 |
Correct |
765 ms |
45220 KB |
Output is correct |
43 |
Correct |
554 ms |
27148 KB |
Output is correct |
44 |
Correct |
556 ms |
30744 KB |
Output is correct |
45 |
Correct |
315 ms |
23728 KB |
Output is correct |
46 |
Correct |
367 ms |
24504 KB |
Output is correct |
47 |
Correct |
574 ms |
27676 KB |
Output is correct |
48 |
Correct |
981 ms |
53244 KB |
Output is correct |
49 |
Correct |
274 ms |
25740 KB |
Output is correct |
50 |
Correct |
268 ms |
24476 KB |
Output is correct |
51 |
Correct |
271 ms |
24344 KB |
Output is correct |
52 |
Correct |
282 ms |
26260 KB |
Output is correct |
53 |
Correct |
284 ms |
26252 KB |
Output is correct |
54 |
Correct |
283 ms |
26552 KB |
Output is correct |