本人双非铜牌选手,入坑ACM6个月,目前板刷CF来提升自己的算法能力和代码实现能力,欢迎关注我一起刷题QwQ。本人CF账号
tag:思维,前缀和
problem types:求最值
观察输出,发现只有0,1,2三种情况,按区间长度是奇是偶进行分类即可。
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, n) for(int i = a; i <= n; i++)
typedef pair<int, int> PII;
const int N = 3e5 + 10;
int sum[N];
void solve(){
int n, q; cin >> n >> q;
string s; cin >> s; s = ' ' + s;
rep(i, 1, n){
if(i % 2){
if(s[i] == '+') s[i] = '-';
else s[i] = '+';
}
}
rep(i, 1, n){
if(s[i] == '+') sum[i] = sum[i - 1] + 1;
else sum[i] = sum[i - 1] - 1;
}
while(q--){
int l, r; cin >> l >> r;
if((r - l + 1) % 2 == 0){
if(sum[r] - sum[l - 1] == 0){
cout << 0 << '\n';
}else{
cout << 2 << '\n';
}
}else{
cout << 1 << '\n';
}
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
cin >> t;
while(t--)
solve();
}