답안 #683011

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
683011 2023-01-17T14:17:18 Z Duy_e Election (BOI18_election) C++14
0 / 100
112 ms 262144 KB
#include <bits/stdc++.h>
#define ll long long
#define pii pair<ll, ll>
#define st first
#define nd second
#define file "test"
#define rep(i, begin, end) for (ll i = (begin); i <= (end); i ++)
#define rrep(i, begin, end) for (ll i = (begin); i >= (end); i --)
using namespace std;
const long long INF = 1e18;
const long long N = 5e5 + 5;

struct node {
    int l_max, r_max, ans, sum;

    node operator +(node other) {
        node ret;
        ret.l_max = max(l_max, sum + other.l_max);
        ret.r_max = max(other.r_max, other.sum + r_max);
        ret.sum = other.sum + sum;
        ret.ans = max(max(ans + other.sum, sum + other.ans), l_max + other.r_max);
        return ret;
    }
} st[4 * N];

int a[N], n;

void build(int id, int l, int r) {
    if (l == r) {
        ll val = max(0, a[l]);
        st[id] = {val, val, val, a[l]};
        return;
    }

    int mid = l + r >> 1;
    build(id << 1, l , mid); build(id << 1 | 1, mid + 1, r);
    st[id] = st[id << 1] + st [id << 1 | 1];
}

node query(int id, int l, int r, int lef, int rig) {
    if (l > rig || r < lef) return {0, 0, 0, 0};
    if (lef <= l && r <= rig) return st[id];
    int mid = l + r >> 1;
    return query(id << 1, l, mid, lef, rig) + query(id << 1 | 1, mid + 1, r, lef, rig); 
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
        freopen(file".inp", "r", stdin); freopen(file".out", "w", stdout);
    #endif
    
    cin >> n;
    rep(i, 1, n) {
        char ch;
        cin >> ch;
        if (ch == 'T') a[i] = 1; else a[i] = -1;
    }

    build(1, 1, n);

    ll q, l, r;
    cin >> q;
    while (q --) {
        cin >> l >> r;
        cout << query(1, 1, n, l, r).ans << '\n';
    }

    return 0;
}  

Compilation message

election.cpp: In function 'void build(int, int, int)':
election.cpp:31:19: warning: narrowing conversion of 'val' from 'long long int' to 'int' [-Wnarrowing]
   31 |         st[id] = {val, val, val, a[l]};
      |                   ^~~
election.cpp:31:24: warning: narrowing conversion of 'val' from 'long long int' to 'int' [-Wnarrowing]
   31 |         st[id] = {val, val, val, a[l]};
      |                        ^~~
election.cpp:31:29: warning: narrowing conversion of 'val' from 'long long int' to 'int' [-Wnarrowing]
   31 |         st[id] = {val, val, val, a[l]};
      |                             ^~~
election.cpp:35:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   35 |     int mid = l + r >> 1;
      |               ~~^~~
election.cpp: In function 'node query(int, int, int, int, int)':
election.cpp:43:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   43 |     int mid = l + r >> 1;
      |               ~~^~~
election.cpp: In function 'int main()':
election.cpp:51:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |         freopen(file".inp", "r", stdin); freopen(file".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
election.cpp:51:49: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |         freopen(file".inp", "r", stdin); freopen(file".out", "w", stdout);
      |                                          ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 112 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 112 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 112 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -