Submission #422608

# Submission time Handle Problem Language Result Execution time Memory
422608 2021-06-10T09:04:12 Z egekabas Election (BOI18_election) C++14
100 / 100
1277 ms 46540 KB
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
int seg[4*500000];
int lazy[4*500000];
void push(int v){
    seg[2*v] += lazy[v];
    seg[2*v+1] += lazy[v];
    lazy[2*v] += lazy[v];
    lazy[2*v+1] += lazy[v];
    
    lazy[v] = 0;
}
void upd(int v, int tl, int tr, int l, int r, int val){
    if(tr < l || r < tl)
        return;
    if(l <= tl && tr <= r){
        seg[v] += val;
        lazy[v] += val;
        return;
    }
    push(v);
    upd(2*v, tl, (tl+tr)/2, l, r, val);
    upd(2*v+1, (tl+tr)/2+1, tr, l, r, val);
    seg[v] = min(seg[2*v], seg[2*v+1]);
}
int get(int v, int tl, int tr, int l, int r){
    if(tr < l || r < tl)
        return 1e9;
    if(l <= tl && tr <= r)
        return seg[v];
    push(v);
    return min(get(2*v, tl, (tl+tr)/2, l, r), get(2*v+1, (tl+tr)/2+1, tr, l, r));
}
int n, q;
int a[500009];
vector<pii> ask[500009];
int ans[500009];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);

    cin >> n;
    string s;
    cin >> s;
    for(int i = 0; i < n; ++i){
        if(s[i] == 'C')
            a[i] = 1;
        else
            a[i] = -1;
    }
    cin >> q;
    for(int i = 0; i < q; ++i){
        int l, r;
        cin >> l >> r;
        --l;
        --r;
        ask[l].pb({r, i});
    }
    deque<int> negs;
    for(int i = n-1; i >= 0; --i){
        if(a[i] == -1)
            negs.push_front(i);
        else{
            upd(1, 0, n, 0, i, 1);
            if(negs.size()){
                upd(1, 0, n, 0, negs[0], -1);
                negs.pop_front();
            }
        }
        for(auto u : ask[i]){
            ans[u.ss] = upper_bound(all(negs), u.ff)-negs.begin();
            ans[u.ss] += max(0, get(1, 0, n, u.ff+1, u.ff+1)-get(1, 0, n, i, u.ff));
        }
    }
    for(int i = 0; i < q; ++i)
        cout << ans[i] << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12216 KB Output is correct
2 Correct 11 ms 12216 KB Output is correct
3 Correct 11 ms 12108 KB Output is correct
4 Correct 11 ms 12108 KB Output is correct
5 Correct 10 ms 12108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12216 KB Output is correct
2 Correct 11 ms 12216 KB Output is correct
3 Correct 11 ms 12108 KB Output is correct
4 Correct 11 ms 12108 KB Output is correct
5 Correct 10 ms 12108 KB Output is correct
6 Correct 126 ms 17328 KB Output is correct
7 Correct 114 ms 16836 KB Output is correct
8 Correct 115 ms 16936 KB Output is correct
9 Correct 111 ms 17240 KB Output is correct
10 Correct 124 ms 17196 KB Output is correct
11 Correct 159 ms 17544 KB Output is correct
12 Correct 130 ms 17480 KB Output is correct
13 Correct 125 ms 17532 KB Output is correct
14 Correct 130 ms 17476 KB Output is correct
15 Correct 119 ms 17348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12216 KB Output is correct
2 Correct 11 ms 12216 KB Output is correct
3 Correct 11 ms 12108 KB Output is correct
4 Correct 11 ms 12108 KB Output is correct
5 Correct 10 ms 12108 KB Output is correct
6 Correct 126 ms 17328 KB Output is correct
7 Correct 114 ms 16836 KB Output is correct
8 Correct 115 ms 16936 KB Output is correct
9 Correct 111 ms 17240 KB Output is correct
10 Correct 124 ms 17196 KB Output is correct
11 Correct 159 ms 17544 KB Output is correct
12 Correct 130 ms 17480 KB Output is correct
13 Correct 125 ms 17532 KB Output is correct
14 Correct 130 ms 17476 KB Output is correct
15 Correct 119 ms 17348 KB Output is correct
16 Correct 1157 ms 45032 KB Output is correct
17 Correct 940 ms 41000 KB Output is correct
18 Correct 1097 ms 42072 KB Output is correct
19 Correct 1002 ms 43636 KB Output is correct
20 Correct 1202 ms 44036 KB Output is correct
21 Correct 1254 ms 46260 KB Output is correct
22 Correct 1153 ms 45988 KB Output is correct
23 Correct 1247 ms 46540 KB Output is correct
24 Correct 1255 ms 45832 KB Output is correct
25 Correct 1277 ms 45168 KB Output is correct