# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1030991 | hasan2006 | Election (BOI18_election) | C++17 | 2 ms | 600 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define TL ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define rall(s) s.rbegin(),s.rend()
#define all(s) s.begin(),s.end()
#define pb push_back
#define se second
#define fi first
#define ll long long
#define ld long double
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define yes cout<<"yes\n"
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define no cout<<"no\n"
const int N = 1e6 + 9 , mod = 1e9 + 7;
ll a[N] , b[N] , dp[N] , c[N] , d[N] , t[N * 3] , lz[N * 3];
void pushup(int i){
t[i] = min(t[2 * i] , t[2 * i + 1]);
}
void pushlazy(int i){
t[2 * i] += lz[i] , t[2 * i + 1] += lz[i];
lz[2 * i + 1] += lz[i] , lz[2 * i] += lz[i];
lz[i] = 0;
}
void add(int i , int l , int r , int tl , int tr , int x){
int m = (l + r) / 2;
if(l > tr || r < tl) return;
if(l >= tl && r <= tr){
t[i] += x , lz[i] += x;
}else {
pushlazy(i);
add(2 * i , l , m , tl , tr , x);
add(2 * i + 1 , m + 1 , r , tl , tr , x);
pushup(i);
}
}
ll get(int i , int l , int r , int tl , int tr){
int m = (l + r) / 2;
if(l > tr || r < tl) return 1e18;
if(l >= tl && r <= tr){
return t[i];
}else {
pushlazy(i);
return min(get(2 * i , l , m , tl , tr) , get(2 * i + 1 , m + 1 , r , tl , tr));
}
}
void solve()
{
ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
cin>>n;
for(i = 1; i <= n; i++){
char ch;
cin>>ch;
a[i] = (ch == 'T' ? -1 : 1);
b[i] = b[i - 1] + a[i];
add(1 , 1 , n, i , i, b[i]);
}
cin>>q;
vector<pair<int,pair<int,int>>>v;
for(i = 1; i <= q; i++)
cin>>l>>r , v.pb({r , {l , i}});
sort(all(v));
deque<int>d;
k = 1;
for(auto to : v){
l = to.se.fi , r = to.fi;
while(k <= r){
if(a[k] == -1)
add(1 , 1 , n , k , n , 1) , d.push_back(k);
else if(d.size())
add(1 , 1 , n , d.back() , n , -1) , d.pop_back();
k++;
}
c[to.se.se] = d.size() + max(b[l - 1] - get(1 , 1 , n , l , r) , 0ll);
}
for(i =1; i <= q;i ++)
cout<<c[i]<<"\n";
}
int main(){
TL;
/*#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif*/
int t = 1;
//cin>>t;
while(t--)
{
solve();
}
}
// Author : حسن
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |