이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// Null
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
const ll Mod = 1000000007LL;
const int N = 5e5 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;
int n, q, ps[N];
str s;
int lz[N << 2], seg[N << 2];
void Apply(int id, int x){
seg[id] += x;
lz[id] += x;
}
void Shift(int id){
Apply(id << 1, lz[id]);
Apply(id << 1 | 1, lz[id]);
lz[id] = 0;
}
void Add(int id, int l, int r, int x, int L, int R){
if(r <= L || R <= l) return ;
if(l <= L && R <= r){
Apply(id, x);
return ;
}
Shift(id);
int mid = (L + R) >> 1;
Add(id << 1, l, r, x, L, mid);
Add(id << 1 | 1, l, r, x, mid, R);
seg[id] = min(seg[id << 1], seg[id << 1 | 1]);
}
int Get(int id, int l, int r, int L, int R){
if(r <= L || R <= l) return 1e9;
if(l <= L && R <= r) return seg[id];
Shift(id);
int mid = (L + R) >> 1;
return min( Get(id << 1, l, r, L, mid), Get(id << 1 | 1, l, r, mid, R) );
}
int ans[N];
vector< pair<int, int> > Q[N], M;
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> s >> q;
int l, r;
for(int i = 0; i < q; i++){
cin >> l >> r;
Q[l - 1].pb({r, i});
}
for(int i = 1; i <= n; i++) ps[i] = ps[i - 1] + (s[i - 1] == 'T' ? 1 : -1);
//for(int i = 0; i <= n; i++) cerr << ps[i] << ' ';
//cerr << "\n\n";
int Sm, X, Y;
for(int i = n; i >= 0; i--){
//Add(1, i, i + 1, ps[i], 0, n + 1);
int la = i;
while(!M.empty()){
if(M.back().F > ps[i]) break;
Add(1, la + 1, M.back().S + 1, M.back().F - ps[i], 0, n + 1);
la = M.back().S;
M.pop_back();
}
M.pb({ps[i], la});
for(auto [r, qn] : Q[i]){
Sm = ps[r] - ps[i];
//debug(Get(1, r, r + 1, 0, n + 1));
X = ps[r] - Get(1, r, r + 1, 0, n + 1) - ps[i];
Y = (Sm - X) - Get(1, i, r + 1, 0, n + 1);
ans[qn] = X + Y;
//cerr << i + 1 << ' ' << r << " -> "<< Sm << ' ' << X << '\n';
}
}
for(int i = 0; i < q; i++) cout << ans[i] << '\n';
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
election.cpp: In function 'int main()':
election.cpp:83:12: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
for(auto [r, qn] : Q[i]){
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |