Submission #1298984

#TimeUsernameProblemLanguageResultExecution timeMemory
1298984khoile08Election (BOI18_election)C++20
100 / 100
507 ms69844 KiB
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for(int i = a; i <= b; i++) #define FOD(i, a, b) for(int i = a; i >= b; i--) #define int long long #define fi first #define se second #define ll long long #define db double #define ii pair<int,int> #define pb push_back #define MASK(i) (1LL << i) #define sq(i) (1LL * (i) * (i)) #define task "task" const ll INF = 1e18; const int inf = 1e9; const int N = 5e5 + 4; int n, q; int a[N]; struct Node { int L, R, S, A; Node(int _a = 0, int _b = 0, int _s = 0, int _k = 0) { L = _a; R = _b; S = _s; A = _k; } }; Node emp = {-inf, -inf, 0}; struct Smt { Node st[4 * N]; Node combine(Node a, Node b) { Node ret; ret.S = a.S + b.S; ret.L = max(a.L, a.S + b.L); ret.R = max(b.R, b.S + a.R); ret.A = max({a.A + b.S, a.S + b.A, a.L + b.R}); return ret; } void build(int id, int l, int r) { if(l == r) { if(a[l] == 1) st[id] = {1, 1, 1, 1}; else st[id] = {0, 0, -1, 0}; return; } int mid = l + r >> 1; build(id << 1, l, mid); build(id << 1 | 1, mid + 1, r); st[id] = combine(st[id << 1], st[id << 1 | 1]); } Node get(int id, int l, int r, int u, int v) { if(l > v || r < u) return emp; if(l >= u && r <= v) return st[id]; int mid = l + r >> 1; return combine(get(id << 1, l, mid, u, v), get(id << 1 | 1, mid + 1, r, u, v)); } } smt; signed main() { if(fopen(task".inp", "r")) { freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); } ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; FOR(i, 1, n) { char c; cin >> c; a[i] = (c == 'C' ? -1 : 1); } smt.build(1, 1, n); cin >> q; FOR(i, 1, q) { int l, r; cin >> l >> r; cout << smt.get(1, 1, n, l, r).A << '\n'; } }

Compilation message (stderr)

election.cpp: In function 'int main()':
election.cpp:69:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
election.cpp:70:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...