[AGC059A] My Last ABC Problem
Genius_Star · · 题解
或许更好的阅读体验。
好好好,喜欢对脑电波。
思路:
首先可以将相同字符缩成一段,那么每次操作
而显然的,一次最多删两个字符,至少可以删一个,因为可能
当
否则,最后剩下
但是这还是把整个段相同字符缩在一起的长度,对于区间
于是时间复杂度为
完整代码:
#include<bits/stdc++.h>
#define fi first
#define se second
#define lowbit(x) (x) & (-(x))
#define popcnt(x) __builtin_popcount(x)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int N = 1e5 + 10;
inline ll read(){
ll x = 0, f = 1;
char c = getchar();
while(c < '0' || c > '9'){
if(c == '-')
f = -1;
c = getchar();
}
while(c >= '0' && c <= '9'){
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return x * f;
}
inline void write(ll x){
if(x < 0){
putchar('-');
x = -x;
}
if(x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
int n, q, l, r;
int S[N];
char s[N];
int main(){
n = read(), q = read();
scanf("%s", s + 1);
for(int i = 1; i <= n; ++i)
S[i] = S[i - 1] + (s[i] != s[i - 1]);
while(q--){
l = read(), r = read();
int len = S[r] - S[l] + 1;
if(len == 1){
puts("0");
continue;
}
if(len & 1)
write(((len - 1) >> 1) + (s[l] != s[r]));
else
write(len >> 1);
putchar('\n');
}
return 0;
}