Submission #1119631

#TimeUsernameProblemLanguageResultExecution timeMemory
1119631lmToT27Election (BOI18_election)C++17
100 / 100
553 ms43064 KiB
#include <bits/stdc++.h> #define all(dataStructure) dataStructure.begin(),dataStructure.end() using namespace std; typedef long long ll; const int MAX = 5e5 + 3; const ll MOD[] = {1000000007, 998244353}; int n; string st; struct node_t { int maxPre, maxSuf, sum, ans; node_t(int a = 0, int b = 0, int c = 0, int d = 0) { maxPre = a; maxSuf = b; sum = c; ans = d; } } node[MAX << 2]; node_t combine(const node_t &L, const node_t &R) { node_t res; res.maxPre = max(L.maxPre, L.sum + R.maxPre); res.maxSuf = max(R.maxSuf, R.sum + L.maxSuf); res.sum = L.sum + R.sum; res.ans = L.maxPre + R.maxSuf; res.ans = max(res.ans, L.ans + R.sum); res.ans = max(res.ans, R.ans + L.sum); return res; } void build(int id = 1, int l = 1, int r = n) { if (l == r) { if (st[l - 1] == 'C') node[id] = node_t(0, 0, -1, 0); else node[id] = node_t(1, 1, 1, 1); } else { int mid = (l + r) / 2; build(id << 1, l, mid); build(id << 1 | 1, mid + 1, r); node[id] = combine(node[id << 1], node[id << 1 | 1]); } } node_t query(int u, int v, int id = 1, int l = 1, int r = n) { if (v < l || r < u) return node_t(); if (u <= l && r <= v) return node[id]; int mid = (l + r) / 2; return combine(query(u, v, id << 1, l, mid), query(u, v, id << 1 | 1, mid + 1, r)); } void Solve() { cin >> n >> st; build(); int q; cin >> q; while (q--) { int l, r; cin >> l >> r; cout << query(l, r).ans << '\n'; } } int32_t main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0); #define TASK "" if (fopen(TASK".INP", "r")) { freopen(TASK".INP", "r", stdin); freopen(TASK".OUT", "w", stdout); } if (fopen("TASK.INP", "r")) { freopen("TASK.INP", "r", stdin); freopen("TASK.OUT", "w", stdout); } /* int TEST = 1; cin >> TEST; while (TEST--) */ Solve(); cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n"; return 0; }

Compilation message (stderr)

election.cpp: In function 'int32_t main()':
election.cpp:75:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |                 freopen(TASK".INP", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
election.cpp:76:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |                 freopen(TASK".OUT", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
election.cpp:80:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |                 freopen("TASK.INP", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
election.cpp:81:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |                 freopen("TASK.OUT", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...