#include "bits/stdc++.h"
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef long long ll;
typedef string str;
typedef double db;
typedef long double ld;
typedef pair<int, int> pi;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define endl "\n"
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
const ll MOD = 1e9 + 7; //998244353
const ll INF = 1e18;
const ll MX = 1e9 + 10;
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up
#ifndef LOCAL
#define cerr if(false) cerr
#endif
#define debug(x) cerr << #x << " : " << x << endl;
#define debugs(x,y) cerr << #x << " : " << x << " / " << #y << " : " << y << endl;
#define debugv(v) cerr << #v << " : " << "[ "; for(auto it : v) cerr << it << ' '; cerr << ']' << endl;
#define here() cerr << "here" << endl;
void IO() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
struct segtree
{
int size;
vector<ll> sums;
void init(int n)
{
size = 1;
while(size<n) size*=2;
sums.assign(2*size, 1e9);
}
void set(int i, int v, int x, int lx, int rx)
{
if(rx-lx==1)
{
sums[x] = v;
return;
}
int mid = (lx+rx)/2;
if(i<mid)
set(i, v, 2*x+1, lx, mid);
else
set(i, v, 2*x+2, mid, rx);
sums[x] = min(sums[2*x+1], sums[2*x+2]);
}
void set(int i, int v)
{
set(i, v, 0, 0, size);
}
ll sum(int l, int r, int x, int lx, int rx)
{
if(lx>=r||l>=rx) return 1e9;
if(lx>=l&&rx<=r) return sums[x];
int mid = (lx+rx)/2;
ll s1 = sum(l, r, 2*x+1, lx, mid);
ll s2 = sum(l, r, 2*x+2, mid, rx);
return min(s1, s2);
}
ll sum(int l, int r)
{
return sum(l, r, 0, 0, size);
}
};
int main()
{
boost;
IO();
ll n;
cin >> n;
string s;
cin >> s;
vector<ll> pre(n);
if(s[0]=='C')
pre[0] = 1;
else
pre[0] = -1;
FOR(i, 1, n)
{
pre[i] = pre[i-1];
if(s[i]=='C')
pre[i]+=1;
else
pre[i]-=1;
}
vector<ll> suff(n);
if(s[n-1]=='C')
suff[n-1] = 1;
else
suff[n-1] = -1;
for(int i = n-2; i>=0; i--)
{
suff[i]+=suff[i+1];
if(s[i]=='C')
suff[i] += 1;
else
suff[i]-=1;
}
segtree treepre, treesuff;
treepre.init(n);
FOR(i, 0, n)
treepre.set(i, pre[i]);
treesuff.init(n);
FOR(i, 0, n)
treesuff.set(i, suff[i]);
ll q;
cin >> q;
while(q--)
{
ll l, r;
cin >> l >> r;
ll removesuff = 0, removepre = 0;
if(r<=n-1)
removesuff = suff[r];
if(l-2>=0)
removepre = pre[l-2];
for(int i = l-1; i<r; i++)
treepre.set(i, treepre.sums[i] - removepre);
for(int i = l-1; i<r; i++)
treesuff.set(i, treesuff.sums[i] -removesuff);
ll min1 = treepre.sum(l-1, r-1);
ll min2 = treesuff.sum(l-1, r-1);
ll res = min(min1, min2);
if(res<0)
cout << -res << endl;
else
cout << 0 << endl;
for(int i = l-1; i<r; i++)
treepre.set(i, treepre.sums[i] +2*removepre);
for(int i = l-1; i<r; i++)
treesuff.set(i, treesuff.sums[i] +2*removesuff);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
248 ms |
400 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
248 ms |
400 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
248 ms |
400 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |