#include <iostream>
#include <chrono>
#include <vector>
#include <algorithm>
#include <set>
#define maxn 200005
#define maxlog 20
#define INF 1000000010
#define LINF 1000000000000000005
#define endl '\n'
#define pb(x) push_back(x)
#define X first
#define Y second
#define control cout<<"passed"<<endl;
#pragma GCC optimize("O3" , "Ofast" , "unroll-loops" , "fast-math")
#pragma GCC target("avx2")
using namespace std;
std::chrono::high_resolution_clock::time_point startT, currT;
constexpr double TIME_MULT = 1;
double timePassed()
{
using namespace std::chrono;
currT = high_resolution_clock::now();
double time = duration_cast<duration<double>>(currT - startT).count();
return time * TIME_MULT;
}
struct que
{
int type , l , r;
que(){};
que(int _type , int _l , int _r)
{
type = _type;
l = _l;
r = _r;
}
};
vector <que> queries;
vector <int> v[maxn], fenwick[maxn];
int n, q;
void update(int line, int to, int val)
{
///cout << line << " " << to << " " << val << endl;
for(int i = line; i <= n; i += (i & -i))
{
//if(v[i].size() == 0)
// continue;
auto it = upper_bound(v[i].begin(), v[i].end(), to);
for(int j = it - v[i].begin(); j <= v[i].size(); j += (j & -j))
fenwick[i][j] += val;
}
}
int query(int line, int to)
{
//cout << "_--_ " << line << " " << to << endl;
int ans = 0;
for(int i = line; i > 0; i -= (i & -i))
{
if(v[i].size() == 0)
continue;
auto it = upper_bound(v[i].begin(), v[i].end(), to);
for(int j = it - v[i].begin(); j > 0; j -= (j & -j))
ans += fenwick[i][j];
}
return ans;
}
void add(int line, int to)
{
//cout << line << " " << to << endl;
for(int i = line; i <= n; i += (i & -i))
v[i].pb(to);
}
string s;
set <int> zeros;
int xo[maxn], ans[maxn];
void read()
{
cin >> n >> q;
cin >> s;
queries.resize(q + 1);
zeros.insert(0);
zeros.insert(n + 1);
for(int i = 1; i <= n; i++)
{
xo[i] = s[i - 1] - '0';
if(!xo[i])
zeros.insert(i);
}
}
pair <int , int> timer[maxn];
void pre_solve()
{
string type;
int a , b;
for(int i = 1; i <= q; i++)
{
cin >> type;
if(type == "toggle")
{
cin >> a;
if(xo[a] == 0)
zeros.erase(zeros.find(a));
else
zeros.insert(a);
xo[a] ^= 1;
auto it = zeros.upper_bound(a);
int pom = *it - 1;
it--;
if(*it == a)
it--;
int pom2 = *it + 1;
add(pom2 , a);
add(pom2 , pom + 1);
add(a + 1 , a);
add(a + 1 , pom + 1);
queries[i] = {0 , pom2 , pom};
timer[i] = {a , (xo[a]? -i : i)};
continue;
}
cin >> a >> b;
b--;
queries[i] = {1 , a , b};
if(*zeros.lower_bound(a) > b)
ans[i] += i;
}
for(int i = 1; i <= n; i++)
{
sort(v[i].begin() , v[i].end());
v[i].resize(unique(v[i].begin() , v[i].end()) - v[i].begin());
fenwick[i].resize(v[i].size() + 1);
}
}
void solve()
{
int t , l , r;
for(int i = 1; i <= q; i++)
{
t = queries[i].type;
l = queries[i].l;
r = queries[i].r;
if(t == 0)
{
update(l , timer[i].X , timer[i].Y);
update(l , r + 1 , -timer[i].Y);
update(timer[i].X + 1 , timer[i].X , -timer[i].Y);
update(timer[i].X + 1 , r + 1 , timer[i].Y);
continue;
}
cout << query(l , r) + ans[i] << endl;
}
}
int main()
{
/**#ifdef ONLINE_JUDGE
freopen("taxi.in", "r", stdin);
freopen("taxi.out", "w", stdout);
#endif*/
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//startT = std::chrono::high_resolution_clock::now();
read();
pre_solve();
solve();
return 0;
}
Compilation message
street_lamps.cpp: In function 'void update(int, int, int)':
street_lamps.cpp:62:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for(int j = it - v[i].begin(); j <= v[i].size(); j += (j & -j))
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
12632 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
3 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
10584 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
186 ms |
28172 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
10844 KB |
Output is correct |
2 |
Correct |
4 ms |
12892 KB |
Output is correct |
3 |
Correct |
3 ms |
12892 KB |
Output is correct |
4 |
Correct |
3 ms |
12868 KB |
Output is correct |
5 |
Runtime error |
49 ms |
40916 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
13048 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Correct |
4 ms |
12892 KB |
Output is correct |
4 |
Correct |
5 ms |
10840 KB |
Output is correct |
5 |
Runtime error |
30 ms |
31464 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
12632 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
3 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
10584 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Incorrect |
186 ms |
28172 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |