Submission #737040

#TimeUsernameProblemLanguageResultExecution timeMemory
737040josanneo22Election (BOI18_election)C++17
100 / 100
506 ms44488 KiB
#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second 


void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename A>
void __print(const A &x);
template <typename A, typename B>
void __print(const pair<A, B> &p);
template <typename... A>
void __print(const tuple<A...> &t);
template <typename T>
void __print(stack<T> s);
template <typename T>
void __print(queue<T> q);
template <typename T, typename... U>
void __print(priority_queue<T, U...> q);
template <typename A>
void __print(const A &x) {
    bool first = true;
    cerr << '{';
    for (const auto &i : x) {
        cerr << (first ? "" : ","), __print(i);
        first = false;
    }
    cerr << '}';
}
template <typename A, typename B>
void __print(const pair<A, B> &p) {
    cerr << '(';
    __print(p.first);
    cerr << ',';
    __print(p.second);
    cerr << ')';
}
template <typename... A>
void __print(const tuple<A...> &t) {
    bool first = true;
    cerr << '(';
    apply([&first](const auto &...args) { ((cerr << (first ? "" : ","), __print(args), first = false), ...); }, t);
    cerr << ')';
}
template <typename T>
void __print(stack<T> s) {
    vector<T> debugVector;
    while (!s.empty()) {
        T t = s.top();
        debugVector.push_back(t);
        s.pop();
    }
    reverse(debugVector.begin(), debugVector.end());
    __print(debugVector);
}
template <typename T>
void __print(queue<T> q) {
    vector<T> debugVector;
    while (!q.empty()) {
        T t = q.front();
        debugVector.push_back(t);
        q.pop();
    }
    __print(debugVector);
}
template <typename T, typename... U>
void __print(priority_queue<T, U...> q) {
    vector<T> debugVector;
    while (!q.empty()) {
        T t = q.top();
        debugVector.push_back(t);
        q.pop();
    }
    __print(debugVector);
}
void _print() { cerr << "]\n"; }
template <typename Head, typename... Tail>
void _print(const Head &H, const Tail &...T) {
    __print(H);
    if (sizeof...(T))
        cerr << ", ";
    _print(T...);
}
#ifndef ONLINE_JUDGE
#define debug(...) cerr << "Line:" << __LINE__ << " [" << #__VA_ARGS__ << "] = ["; _print(__VA_ARGS__)
#else
#define debug(...)
#endif


#define int long long
char buf[1<<23],*p1=buf,*p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline int rd()
{
	int s=0;
	char ch=getchar(),last;
	while(ch<'0'||ch>'9') last=ch,ch=getchar();
	while(ch>='0'&&ch<='9') s=(s<<1)+(s<<3)+(ch^48),ch=getchar();
	return last=='-'?-s:s;
}
int num[100];
inline void rt(int x)
{
	if(x<0) putchar('-'),x=-x;;
	int len=0;
	do num[len++]=x%10;while(x/=10);
	while(len--) putchar(num[len]+'0');
}

int n, q;
string s;
 
struct node {
    int L, R;
    int sum;
    int best;
    node operator + (const node& other) const {
        node ret;
        ret.sum = sum + other.sum;
        ret.L = max(L, sum + other.L);
        ret.R = max(other.R, other.sum + R);
        ret.best = max({L + other.R, best + other.sum, sum + other.best});
        return ret;
    }
} st[2000010];
 
void build(int id, int L, int R) {
    if (L == R) {
        if (s[L - 1] == 'T') st[id] = {1, 1, 1, 1};
        else st[id] = {0, 0, -1, 0};
        return;
    }
    int mid = L + R >> 1;
    build(id * 2, L, mid);
    build(id * 2 + 1, mid + 1, R);
    st[id] = st[id * 2] + st[id * 2 + 1];
}
 
node get(int id, int L, int R, int u, int v) {
    if (u <= L && R <= v) return st[id];
    int mid = L + R >> 1;
    if (v <= mid) return get(id * 2, L, mid, u, v);
    if (u > mid) return get(id * 2 + 1, mid + 1, R, u, v);
    return get(id * 2, L, mid, u, v) + get(id * 2 + 1, mid + 1, R, u, v);
}

void solve(){
    cin >> n;
    cin >> s;
    build(1, 1, n);
    cin >> q;
    while (q--) {
        int l, r;
        cin >> l >> r;
        cout << get(1, 1, n, l, r).best << '\n';
    }
}
signed main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	solve();
}

Compilation message (stderr)

election.cpp: In function 'void build(long long int, long long int, long long int)':
election.cpp:149:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  149 |     int mid = L + R >> 1;
      |               ~~^~~
election.cpp: In function 'node get(long long int, long long int, long long int, long long int, long long int)':
election.cpp:157:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  157 |     int mid = L + R >> 1;
      |               ~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...