//#ifndef khos
//#pragma GCC optimize ("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#endif
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#ifdef khos
#include "t_debug.cpp"
#else
#define debug(...) 42
#endif
using namespace std;
using namespace __gnu_pbds;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using indexed_multiset = tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update>;
#define int int64_t
#define vi vector
#define ss second
#define ff first
#define TESTCASES
#define all(x) (x).begin(), (x).end()
const int mod = 1e9+7;
const int MAXN=2000000+5;
const int inf=1e18;
void solution() {
int n;
cin >> n;
vi<int> a(n);
for(auto &x:a)cin >> x;
vi<int> vec;
vec.push_back(0);
for(int i=0; i<n; i++){
if(a[i]==1)vec.push_back(i);
}
int q, cnt=0;
auto sum = [&](int x){
int l=0, r=vec.size()-1;
while(l<r){
int mid=(l+r+1)/2;
if(vec[mid]-x<=cnt && mid-1<=x)l=mid;
else r=mid-1;
}
return l;
};
cin >> q;
while(q--){
int op;
cin >> op;
if(op==1){
cnt++;
}
else {
int l, r;
cin >> l >> r;
-- l, -- r;
cout << (r - l + 1) * 2 - sum(r) + sum(l - 1) << '\n';
}
}
}
int32_t main(){
clock_t tStart = clock();
#ifdef khos
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int q = 1;
#ifdef TESTCASES
// cin >> q;
#endif
while(q--) {
solution();
cout << '\n';
}
cerr<<fixed<<setprecision(3)<<"\nTime Taken: "<<(double)(clock()- tStart)/CLOCKS_PER_SEC<<endl;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |