Submission #1050542

# Submission time Handle Problem Language Result Execution time Memory
1050542 2024-08-09T10:41:44 Z otarius Election (BOI18_election) C++17
100 / 100
394 ms 45004 KB
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ull unsigned long long
#define all(x) (x).begin(),(x).end()

// #define int long long
// #define int unsigned long long

// #define ordered_set(T) tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>
// #define ordered_multiset(T) tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>

void open_file(string filename) {
    freopen((filename + ".in").c_str(), "r", stdin);
    freopen((filename + ".out").c_str(), "w", stdout);
}

// const ll mod = 1e9 + 7;
// const ll mod = 998244353;

const ll inf = 1e9;
const ll biginf = 1e18;
const int maxN = 5 * 1e5 + 25;

struct node {
    int sum, suff;
};
char c[maxN];
node t[4 * maxN];
vector<pii> qs[maxN];
node merge(node a, node b) {
    node ans;
    ans.sum = a.sum + b.sum;
    ans.suff = min(b.suff, a.suff + b.sum);
    return ans;
}
void update(int v, int tl, int tr, int pos, int val) {
    if (tl == tr) {
        t[v].sum = val;
        t[v].suff = min(0, val);
        return;
    } int tm = (tl + tr) / 2;
    if (pos <= tm) update(2 * v, tl, tm, pos, val);
    else update(2 * v + 1, tm + 1, tr, pos, val);
    t[v] = merge(t[2 * v], t[2 * v + 1]);
}
node getmin(int v, int tl, int tr, int l, int r) {
    if (l > r) {
        node tmp;
        tmp.sum = tmp.suff = 0;
        return tmp;
    } if (tl == l && tr == r)
        return t[v];
    int tm = (tl + tr) / 2;
    return merge(getmin(2 * v, tl, tm, l, min(r, tm)),
                 getmin(2 * v + 1, tm + 1, tr, max(l, tm + 1), r));
}
void solve() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> c[i];
        if (c[i] == 'C') update(1, 1, n, i, 1);
        else update(1, 1, n, i, -1);
    } int q; cin >> q; int ans[q + 1];
    for (int i = 1; i <= q; i++) {
        int l, r; cin >> l >> r;
        qs[l].pb({r, i});
    } vector<int> st;
    for (int i = n; i >= 1; i--) {
        if (c[i] == 'T') {
            st.pb(i);
            update(1, 1, n, i, 0);
        } else if (!st.empty()) {
            update(1, 1, n, st.back(), -1);
            st.pop_back();
        } for (auto v : qs[i]) {
            int mn = getmin(1, 1, n, i, v.ff).suff;
            if (mn < 0) mn = -mn;
            else mn = 0;
            ans[v.sc] = mn + upper_bound(st.rbegin(), st.rend(), v.ff) - st.rbegin();
        }
    } for (int i = 1; i <= q; i++)
        cout << ans[i] << '\n';
}
int32_t main() { 
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
        cout << '\n';
    }
    return 0;
}

Compilation message

election.cpp: In function 'void open_file(std::string)':
election.cpp:26:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |     freopen((filename + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
election.cpp:27:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |     freopen((filename + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12892 KB Output is correct
2 Correct 2 ms 12892 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Correct 3 ms 12892 KB Output is correct
5 Correct 2 ms 12892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12892 KB Output is correct
2 Correct 2 ms 12892 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Correct 3 ms 12892 KB Output is correct
5 Correct 2 ms 12892 KB Output is correct
6 Correct 42 ms 17780 KB Output is correct
7 Correct 39 ms 17288 KB Output is correct
8 Correct 38 ms 17492 KB Output is correct
9 Correct 38 ms 17720 KB Output is correct
10 Correct 39 ms 17752 KB Output is correct
11 Correct 44 ms 18004 KB Output is correct
12 Correct 41 ms 18052 KB Output is correct
13 Correct 46 ms 18260 KB Output is correct
14 Correct 42 ms 18008 KB Output is correct
15 Correct 38 ms 18044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 12892 KB Output is correct
2 Correct 2 ms 12892 KB Output is correct
3 Correct 2 ms 12892 KB Output is correct
4 Correct 3 ms 12892 KB Output is correct
5 Correct 2 ms 12892 KB Output is correct
6 Correct 42 ms 17780 KB Output is correct
7 Correct 39 ms 17288 KB Output is correct
8 Correct 38 ms 17492 KB Output is correct
9 Correct 38 ms 17720 KB Output is correct
10 Correct 39 ms 17752 KB Output is correct
11 Correct 44 ms 18004 KB Output is correct
12 Correct 41 ms 18052 KB Output is correct
13 Correct 46 ms 18260 KB Output is correct
14 Correct 42 ms 18008 KB Output is correct
15 Correct 38 ms 18044 KB Output is correct
16 Correct 371 ms 43132 KB Output is correct
17 Correct 308 ms 39392 KB Output is correct
18 Correct 345 ms 40528 KB Output is correct
19 Correct 326 ms 42064 KB Output is correct
20 Correct 340 ms 42148 KB Output is correct
21 Correct 394 ms 44496 KB Output is correct
22 Correct 349 ms 44196 KB Output is correct
23 Correct 374 ms 45004 KB Output is correct
24 Correct 347 ms 44244 KB Output is correct
25 Correct 344 ms 43348 KB Output is correct