답안 #1031127

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1031127 2024-07-22T15:15:21 Z hasan2006 Election (BOI18_election) C++17
100 / 100
1234 ms 60688 KB
#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 , 0 , 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 , 0 , n , k , n , 1) , d.push_back(k);
            else if(d.size())
                add(1 , 0 , n , d.back() , n , -1) , d.pop_back();
            k++;
        }
        ll L = 0 , R = d.size() , M;
        if(d.size()){
            while(L != R){
                M = (L + R) / 2;
                if(d[M] >= l)
                    R = M;
                else
                    L = M + 1;
            }
        }
        c[to.se.se] = (d.size() - L) + max(get(1 , 0 , n , l - 1 , l - 1) - get(1 , 0 , n , l , r) , 0ll);
    }
    for(i = 0; i <= 2e6; i++)
        t[i] = 0 , lz[i] = 0;
    reverse(a + 1 , a + n + 1);
    for(i = 1; i <= n; i++)
        b[i] = b[i - 1] + a[i] , add(1 , 0 , n , i , i , b[i]);
    for(i = 0; i < v.size(); i++){
        v[i].fi = n - v[i].fi + 1;
        v[i].se.fi = n - v[i].se.fi + 1;
        swap(v[i].fi , v[i].se.fi);
    }
    sort(all(v));
    d.clear();
    k = 1;
    for(auto to : v){
        l = to.se.fi , r = to.fi;
        while(k <= r){
            if(a[k] == -1)
                add(1 , 0 , n , k , n , 1) , d.push_back(k);
            else if(d.size())
                add(1 , 0 , n , d.back() , n , -1) , d.pop_back();
            k++;
        }
        ll L = 0 , R = d.size() , M;
        if(d.size()){
            while(L != R){
                M = (L + R) / 2;
                if(d[M] >= l)
                    R = M;
                else
                    L = M + 1;
            }
        }
        f = d.size()- L;
        c[to.se.se] =  min(c[to.se.se] , f + max(get(1 , 0 , n , l - 1 , l - 1) - get(1 , 0 , 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

election.cpp: In function 'void solve()':
election.cpp:102:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |     for(i = 0; i < v.size(); i++){
      |                ~~^~~~~~~~~~
election.cpp:60:20: warning: unused variable 'j' [-Wunused-variable]
   60 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                    ^
election.cpp:60:30: warning: unused variable 'x' [-Wunused-variable]
   60 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                              ^
election.cpp:60:34: warning: unused variable 'y' [-Wunused-variable]
   60 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                  ^
election.cpp:60:38: warning: unused variable 's' [-Wunused-variable]
   60 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                      ^
election.cpp:60:54: warning: unused variable 'm' [-Wunused-variable]
   60 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                                      ^
election.cpp:60:58: warning: unused variable 'mn' [-Wunused-variable]
   60 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                                          ^~
election.cpp:60:69: warning: unused variable 'mx' [-Wunused-variable]
   60 |     ll n , q , i , j ,l ,r , x , y , s = 0 , f , k , m , mn = 1e18, mx = 0 ;
      |                                                                     ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 33880 KB Output is correct
2 Correct 14 ms 33916 KB Output is correct
3 Correct 16 ms 33884 KB Output is correct
4 Correct 15 ms 33884 KB Output is correct
5 Correct 15 ms 33884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 33880 KB Output is correct
2 Correct 14 ms 33916 KB Output is correct
3 Correct 16 ms 33884 KB Output is correct
4 Correct 15 ms 33884 KB Output is correct
5 Correct 15 ms 33884 KB Output is correct
6 Correct 140 ms 37056 KB Output is correct
7 Correct 136 ms 36848 KB Output is correct
8 Correct 125 ms 37256 KB Output is correct
9 Correct 126 ms 36804 KB Output is correct
10 Correct 131 ms 36780 KB Output is correct
11 Correct 143 ms 37060 KB Output is correct
12 Correct 137 ms 37000 KB Output is correct
13 Correct 154 ms 37060 KB Output is correct
14 Correct 133 ms 37056 KB Output is correct
15 Correct 134 ms 37064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 33880 KB Output is correct
2 Correct 14 ms 33916 KB Output is correct
3 Correct 16 ms 33884 KB Output is correct
4 Correct 15 ms 33884 KB Output is correct
5 Correct 15 ms 33884 KB Output is correct
6 Correct 140 ms 37056 KB Output is correct
7 Correct 136 ms 36848 KB Output is correct
8 Correct 125 ms 37256 KB Output is correct
9 Correct 126 ms 36804 KB Output is correct
10 Correct 131 ms 36780 KB Output is correct
11 Correct 143 ms 37060 KB Output is correct
12 Correct 137 ms 37000 KB Output is correct
13 Correct 154 ms 37060 KB Output is correct
14 Correct 133 ms 37056 KB Output is correct
15 Correct 134 ms 37064 KB Output is correct
16 Correct 1029 ms 59240 KB Output is correct
17 Correct 916 ms 58692 KB Output is correct
18 Correct 951 ms 59012 KB Output is correct
19 Correct 957 ms 58416 KB Output is correct
20 Correct 961 ms 58292 KB Output is correct
21 Correct 1138 ms 60336 KB Output is correct
22 Correct 1010 ms 59952 KB Output is correct
23 Correct 1234 ms 60688 KB Output is correct
24 Correct 1061 ms 60064 KB Output is correct
25 Correct 1109 ms 59372 KB Output is correct