#pragma GCC optimize "Ofast"
#pragma GCC optimize "unroll-loops"
#pragma GCC target "sse,sse2,sse3,sse4,abm,avx,mmx,popcnt,tune=native"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// Defines and one-liners
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
template <typename T> inline T& back(vector<T> &vec) { return *(vec.end() - 1); }
template <typename T> inline void pop(vector<T> &vec) { return vec.erase(vec.end() - 1); }
// Scan, Debug, and other nonsense
template <typename T> ostream& operator<<(ostream& out,vector<T> iter){out<<"[";for(auto t:iter){out<<t<<", ";}out<<"]";return out;}
template <typename T> void printArray(ostream& out,T* arr,int sz){out<<"[";for(int i=0;i<sz;i++){out<<arr[i]<<", "; } out<<"]";}
#define OUT_OPERATOR(type, propa, propb) ostream& operator<<(ostream& out,type obj){out<<"("<<#propa<<"="<<obj. propa<<", "<<#propb<<"="<<obj. propb<<")";return out;}
void scan(){}
template<typename F, typename... R> void scan(F &f,R&... r){cin>>f;scan(r...);}
int di_; string dnms_, co_ = ",";
void debug_(){cout<<endl;}
template<typename F, typename... R> void debug_(F f,R... r){while(dnms_[di_] != ',')cout<<dnms_[di_++];di_++;cout<<": "<<f<<",";debug_(r...);}
#define debug(...) dnms_=#__VA_ARGS__+co_,di_=0,debug_(__VA_ARGS__)
// Structures for Problem
struct seg {
int l, r;
bool operator<(const seg &o) const {
return l < o.l;
}
bool operator==(const seg &o) const {
return l == o.l && r == o.r;
}
};
struct line {
int m, b;
void merge(line o) {
m += o.m;
b += o.b;
}
void inv() {
m *= -1;
b *= -1;
}
int gety(int x) {
return m * x + b;
}
};
OUT_OPERATOR(seg, l, r)
OUT_OPERATOR(line, m, b)
const line ZERO = {0, 0};
inline line mergel(line a, line b) {
a.merge(b);
return a;
}
inline line invl(line a) {
a.inv();
return a;
}
// Segment Tree
struct InnerSegNode {
int l, r; line v;
};
const int MSN = 20e6 + 1;
InnerSegNode nodes[MSN];
int make() {
static int ctr = 0;
// assert(ctr + 1 < MSN);
return ++ctr;
}
#define nrt nodes[rt] // Current node
int cql, cqr; line cv; // Current QL and QR
line upd(int &rt, int l, int r) {
if (!rt) rt = make();
if (r < cql || l > cqr) return nrt.v;
if (l == cql && r == cqr) {
nrt.v.merge(cv);
return nrt.v;
}
int mid = (l + r) >> 1;
return nrt.v = mergel(upd(nrt.l, l, mid), upd(nrt.r, mid + 1, r));
}
line query(int &rt, int l, int r) {
if (!rt || (r < cql || l > cqr)) return ZERO;
if (l >= cql && r <= cqr) return nrt.v;
int mid = (l + r) >> 1;
return mergel(query(nrt.l, l, mid), query(nrt.r, mid + 1, r));
}
#undef nrt
const int MN = 3e5 + 1;
int n, q, ba, bb;
string bt;
bool state[MN];
set<seg> segs;
// BIT Functions
int bit[MN]; // Bit of "SegTree"
inline line sum(int a, int b) {
cql = 1; cqr = b;
line ret = ZERO;
for (; a; a -= a & -a)
ret.merge(query(bit[a], 1, n));
return ret;
}
inline void add(int a, int b, const line x) {
cql = cqr = b; cv = x;
for(; a <= n; a += a & -a)
upd(bit[a], 1, n);
}
// Range Add/Sub functions and other utility functions used in main()
inline void updRect(int x1, int y1, int x2, int y2, line x) {
add(x1, y1, x);
add(x2 + 1, y2 + 1, x);
x.inv();
add(x1, y2 + 1, x);
add(x2 + 1, y1, x);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
scan(n, q, bt);
// Init hashmaps and set states
for (int i = 1; i <= n; i++)
state[i] = bt[i - 1] == '1';
// for (int i = 1; i <= n; i++) {
// bit[i].reserve(128);
// bit[i].max_load_factor(0.25);
// }
// Initial states
int lastBegin = -1;
for (int i = 1; i <= n; i++) {
if (state[i] && lastBegin == -1)
lastBegin = i;
else if (!state[i] && lastBegin != -1) {
segs.insert({lastBegin, i - 1});
lastBegin = -1;
}
}
if (lastBegin != -1)
segs.insert({lastBegin, n});
// Add states onto 2D BIT
for (auto seg : segs)
updRect(seg.l, seg.l, seg.r, seg.r, {1, 0});
for (int i = 0; i < q; i++) {
// CurTime = i + 1
scan(bt, ba);
// Debug
// vector<seg> vss(segs.begin(), segs.end());
// debug(i, vss);
// for (int j = 1; j <= n; j++) {
// vector<int> vc;
// for (int k = 1; k <= n; k++) {
// auto sumv = sum(j, k);
// // debug(j, k, sumv, sumv.gety(i + 1));
// vc.push_back(sumv.gety(i + 1));
// }
// debug(vc);
// }
if (bt == "query") {
scan(bb);
bb--;
cout << sum(ba, bb).gety(i + 1) << "\n";
}
else { // bt == "toggle"
if (state[ba]) {
// Find state to split
seg toFind = {ba, -1};
auto ptr = segs.upper_bound(toFind);
// assert(ptr != segs.begin()); // There should always be an L who is less than the current thing
ptr--;
segs.erase(ptr);
updRect(ptr->l, ba, ba, ptr->r, {-1, i + 1});
seg ins1 = {ptr->l, ba - 1}, ins2 = {ba + 1, ptr->r};
if (ins1.l <= ins1.r) // if ins1.l > ins1.r then the seg is size 0 and doesn't matter
segs.insert(ins1);
if (ins2.l <= ins2.r) // Same for ins2
segs.insert(ins2);
}
else { // !state[ba]
// Find states to combine
seg toFind = {ba, -1};
auto ptr2 = segs.upper_bound(toFind), ptr = ptr2; // ptr = left, ptr2 = right
// ptr is invalid -> ptr2 == segs.begin(), ptr2 s invalid, ptr2 == segs.end()
bool condPtr = false, condPtr2 = false;
if (segs.size()) { // If the segs set is empty, don't try to set the ptr
ptr--; //
condPtr = (ptr2 != segs.begin()) && (ptr->r + 1 == ba);
condPtr2 = (ptr2 != segs.end()) && (ptr2->l - 1 == ba);
}
// debug(i, ba, condPtr, condPtr2);
line toAdd = {1, -i - 1};
if (condPtr && condPtr2) { // Both
segs.erase(ptr);
segs.erase(ptr2);
segs.insert({ptr->l, ptr2->r});
updRect(ptr->l, ba, ba, ptr2->r, toAdd);
}
else if (condPtr) { // Left satisfied
segs.erase(ptr);
segs.insert({ptr->l, ba});
updRect(ptr->l, ba, ba, ba, toAdd);
}
else if (condPtr2) { // Right satisfied
segs.erase(ptr2);
segs.insert({ba, ptr2->r});
updRect(ba, ba, ba, ptr2->r, toAdd);
}
else {
segs.insert({ba, ba});
updRect(ba, ba, ba, ba, toAdd);
}
}
state[ba] ^= true;
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
400 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
368 ms |
3220 KB |
Output is correct |
2 |
Correct |
489 ms |
3360 KB |
Output is correct |
3 |
Correct |
979 ms |
7236 KB |
Output is correct |
4 |
Correct |
3736 ms |
258112 KB |
Output is correct |
5 |
Correct |
3498 ms |
261632 KB |
Output is correct |
6 |
Correct |
4009 ms |
266944 KB |
Output is correct |
7 |
Correct |
138 ms |
7600 KB |
Output is correct |
8 |
Correct |
152 ms |
9012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
924 KB |
Output is correct |
2 |
Correct |
6 ms |
888 KB |
Output is correct |
3 |
Correct |
5 ms |
928 KB |
Output is correct |
4 |
Correct |
2 ms |
404 KB |
Output is correct |
5 |
Correct |
4944 ms |
314908 KB |
Output is correct |
6 |
Correct |
4211 ms |
306840 KB |
Output is correct |
7 |
Correct |
2930 ms |
260952 KB |
Output is correct |
8 |
Correct |
149 ms |
9056 KB |
Output is correct |
9 |
Correct |
106 ms |
4048 KB |
Output is correct |
10 |
Correct |
112 ms |
4332 KB |
Output is correct |
11 |
Correct |
114 ms |
4636 KB |
Output is correct |
12 |
Correct |
136 ms |
7540 KB |
Output is correct |
13 |
Correct |
148 ms |
9036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
760 KB |
Output is correct |
2 |
Correct |
4 ms |
788 KB |
Output is correct |
3 |
Correct |
5 ms |
888 KB |
Output is correct |
4 |
Correct |
6 ms |
888 KB |
Output is correct |
5 |
Correct |
1187 ms |
208176 KB |
Output is correct |
6 |
Correct |
2394 ms |
242348 KB |
Output is correct |
7 |
Correct |
3620 ms |
266156 KB |
Output is correct |
8 |
Execution timed out |
5061 ms |
280156 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
400 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
368 ms |
3220 KB |
Output is correct |
9 |
Correct |
489 ms |
3360 KB |
Output is correct |
10 |
Correct |
979 ms |
7236 KB |
Output is correct |
11 |
Correct |
3736 ms |
258112 KB |
Output is correct |
12 |
Correct |
3498 ms |
261632 KB |
Output is correct |
13 |
Correct |
4009 ms |
266944 KB |
Output is correct |
14 |
Correct |
138 ms |
7600 KB |
Output is correct |
15 |
Correct |
152 ms |
9012 KB |
Output is correct |
16 |
Correct |
6 ms |
924 KB |
Output is correct |
17 |
Correct |
6 ms |
888 KB |
Output is correct |
18 |
Correct |
5 ms |
928 KB |
Output is correct |
19 |
Correct |
2 ms |
404 KB |
Output is correct |
20 |
Correct |
4944 ms |
314908 KB |
Output is correct |
21 |
Correct |
4211 ms |
306840 KB |
Output is correct |
22 |
Correct |
2930 ms |
260952 KB |
Output is correct |
23 |
Correct |
149 ms |
9056 KB |
Output is correct |
24 |
Correct |
106 ms |
4048 KB |
Output is correct |
25 |
Correct |
112 ms |
4332 KB |
Output is correct |
26 |
Correct |
114 ms |
4636 KB |
Output is correct |
27 |
Correct |
136 ms |
7540 KB |
Output is correct |
28 |
Correct |
148 ms |
9036 KB |
Output is correct |
29 |
Correct |
4 ms |
760 KB |
Output is correct |
30 |
Correct |
4 ms |
788 KB |
Output is correct |
31 |
Correct |
5 ms |
888 KB |
Output is correct |
32 |
Correct |
6 ms |
888 KB |
Output is correct |
33 |
Correct |
1187 ms |
208176 KB |
Output is correct |
34 |
Correct |
2394 ms |
242348 KB |
Output is correct |
35 |
Correct |
3620 ms |
266156 KB |
Output is correct |
36 |
Execution timed out |
5061 ms |
280156 KB |
Time limit exceeded |
37 |
Halted |
0 ms |
0 KB |
- |