C++#include <bits/stdc++.h>
using namespace std;
//#define int long long
const int N = 2e5+10;
int n,m,Ans,lst;
bool skip;
int a[N],cnt1[N],cnt2[N];
int belong[N],nl[N],nr[N],ans[N];
struct Q {
int l,r,id;
bool operator < (const Q &x) const {
if (belong[l] == belong[x.l]) {
if (belong[l] & 1) return r < x.r;
return r > x.r;
}
return belong[l] < belong[x.l];
}
}q[N];
void build() {
int block = (int)sqrt(n),tot = (int)ceil(n*1.0/block);
for (int i = 1;i <= n;i ++) belong[i] = (i-1) / block + 1;
for (int i = 1;i <= tot;i ++) {
nl[i] = (i-1)*block + 1;
nr[i] = i*block;
}
}
void add(int x) {
if (a[x] == 1) {
if (lst == 1) skip = 0;
else {
if (skip) skip = 1,lst = 1;
else Ans ++,lst = 2,skip = 0;
}
}else {
if (lst == 1) lst = 2,skip = 0;
else {
if (skip) skip = 0;
else skip = 1;
}
}
}
void del(int x) {
}
signed main() {
// freopen("xtw.in","r",stdin);
// freopen("xtw.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin >> n >> m;
build();
for (int i = 1;i <= n;i ++) cin >> a[i];
for (int i = 1;i <= n;i ++) {
cnt1[i] = cnt1[i-1] + (a[i] == 1);
cnt2[i] = cnt2[i-1] + (a[i] == 2);
}
for (int i = 1;i <= m;i ++) {
cin >> q[i].l >> q[i].r;
q[i].id = i;
}
sort(q+1,q+m+1);
int ll = 1,rr = 0;
for (int i = 1;i <= m;i ++) {
if (ll > q[i].l) add(--ll);
if (rr < q[i].r) add(++rr);
if (ll < q[i].l) del(ll++);
if (rr > q[i].r) del(rr--);
ans[q[i].id] = Ans;
}
return 0;
}
C++#include <bits/stdc++.h>
using namespace std;
//#define int long long
int n;
string s;
signed main() {
freopen("ppip.in","r",stdin);
freopen("ppip.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin >> s;
n = (int)s.size();
int cnti = 0,cntp = 0;
for (int i = 0;i < n;i ++) {
if (s[i] == 'I') cnti ++;
else cntp ++;
}
cout << n-2*min(cnti,cntp);
return 0;
}