///***LTT***///
/// ->NHAT QUOC GIA<- ///
#include<bits/stdc++.h>
//#pragma GCC optimize ("O3")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("popcnt")
//#define int long long
#define endl "\n"
#define F first
#define S second
#define pb push_back
using namespace std;
vector <int> lg2;
//void MAKE_LOG_ARR(int n){
// lg2.resize(n + 3);
// for (int i = 1;i <= n;++i){
// lg2[i] = __lg(i);
// }
//}
const long long oo = 1e9+7;
const int N = 5 * 1e5 + 10;
int n, q;
string s;
int a[N];
int tree[N * 4], lazy[N * 4];
vector <pair <int, int>> query[N];
vector <int> del;
int ans[N];
void down(int id, int l, int r){
if (l != r){
tree[id * 2] += lazy[id];
tree[id * 2 + 1] += lazy[id];
lazy[id * 2] += lazy[id];
lazy[id * 2 + 1] += lazy[id];
}
lazy[id] = 0;
return;
}
void update(int id, int l, int r, int u, int v, int val){
if (lazy[id] != 0){
down(id, l, r);
}
if (l > v or r < u) return;
if (l >= u and r <= v){
tree[id] += val;
lazy[id] += val;
return;
}
int mid = (l + r)/2;
update(id * 2, l, mid, u, v, val);
update(id * 2 + 1, mid + 1, r, u, v, val);
tree[id] = min(tree[id * 2], tree[id * 2 + 1]);
return;
}
int get(int id, int l, int r, int u, int v){
if (u > n) return 0;
if (lazy[id] != 0){
down(id, l, r);
}
if (l > v or r < u) return oo;
if (l >= u and r <= v){
return tree[id];
}
int mid = (l + r)/2;
return min(get(id * 2, l, mid, u, v), get(id * 2 + 1, mid + 1, r, u, v));
}
void solve(){
cin >> n;
cin >> s;
for (int i = 0;i < s.size();++i){
if (s[i] == 'C'){
a[i + 1] = 1;
} else a[i + 1] = -1;
}
for (int i = 1;i <= n;++i){
update(1,1,n,1,i,a[i]);
}
cin >> q;
for (int i = 1;i <= q;++i){
int l, r;
cin >> l >> r;
query[l].pb({r, i});
}
// for (int i = 1;i <= n;++i){
// cout << get(1,1,n,i,i) <<" ";
// }
// cout <<"\n";
// for (int i = 1;i <= n;++i){
// cout << a[i] <<" ";
// }
// cout <<"\n";
for (int i = n;i >= 1;--i){
if (a[i] == 1){
if (!del.empty()){
int zz = del.back();
update(1,1,n,1,zz,-1);
del.pop_back();
}
} else {
update(1,1,n,1,i,1);
del.pb(i);
}
for (int j = 0;j < query[i].size();++j){
int id = query[i][j].S;
int r = query[i][j].F;
int res = del.size();
int ll = 0, rr = del.size() - 1;
while (ll <= rr){
int mid = (ll + rr)/2;
if (del[mid] <= r){
rr = mid - 1;
res = mid;
} else ll = mid + 1;
}
// cout << id <<" : ";
// for (int z : del){
// cout << z <<" ";
// }
// cout <<"\n";
// cout << res <<"\n";
int sz = del.size();
ans[id] = (sz - res) + abs(get(1,1,n,i,r) - get(1,1,n,r + 1,r + 1));
}
}
for (int i = 1;i <= q;++i){
cout << ans[i] <<"\n";
}
return;
}
int main(){
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL);
if (fopen("ltt.inp", "r")){
freopen("ltt.inp", "r", stdin);
freopen("ltt.out", "w", stdout);
}
// int t;
// cin >> t;
// while(t--){
solve();
// }
return 0;
}
Compilation message
election.cpp: In function 'void solve()':
election.cpp:70:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for (int i = 0;i < s.size();++i){
| ~~^~~~~~~~~~
election.cpp:103:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | for (int j = 0;j < query[i].size();++j){
| ~~^~~~~~~~~~~~~~~~~
election.cpp: In function 'int main()':
election.cpp:135:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
135 | freopen("ltt.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
election.cpp:136:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
136 | freopen("ltt.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
18768 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
18768 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
18768 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |