Submission #1337127

#TimeUsernameProblemLanguageResultExecution timeMemory
1337127JuanJLTornjevi (COCI25_tornjevi)C++20
25 / 110
244 ms79512 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define fst first
#define snd second
#define pb push_back
#define forn(i,a,b) for(int i = a; i<b; i++)
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define mset(a,v) memset(a,v,sizeof(a))
#define FIN ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;

template <typename T>
using iset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

#ifdef D
	#define debug(x) cout<<x
#else
	#define debug(x) //nothing
#endif

const int MAXN = 100000+5;
const int MAXP = 25;
string color;
ll n,q;

struct State{
	ll aa=0;
	ll rr=0;
	ll ra = 0;
	ll ar = 0;
	State(ll aa=0, ll rr=0, ll ra = 0, ll ar = 0):aa(aa),rr(rr), ra(ra), ar(ar){}	
};

State blift[MAXN][MAXP];

State merge(State a, State b){
	State neww = State();

	ll aux;

	aux=min(a.ar,b.ar);
	b.ar-=aux;

	aux=min(a.aa,b.ar);
	b.ar-=aux;

	//
	//////////////////////////////

	aux=min(a.ra,b.ra);
	b.ra-=aux;

	aux=min(a.rr,b.ra);
	b.ra-=aux;

	//
	//////////////////////////////

	aux=min(a.rr,b.aa);
	b.aa-=aux;
	a.rr-=aux;
	neww.ar+=aux;

	aux=min(a.ra,b.aa);
	a.ra-=aux;
	b.aa-=aux;
	neww.aa+=aux;

	//////////////////////////////

	aux=min(a.aa,b.rr);
	a.aa-=aux;
	b.rr-=aux;
	neww.ra+=aux;

	aux=min(a.ar,b.rr);
	a.ar-=aux;
	b.rr-=aux;
	neww.rr+=aux;



	neww.ar+=a.ar+b.ar;
	neww.ra+=a.ra+b.ra;
	neww.rr+=a.rr+b.rr;
	neww.aa+=a.aa+b.aa;
	return neww;
}

void precalc(){
	forn(i,0,n) blift[i][0]=(color[i]=='P'?State(1,0,0,0):State(0,1,0,0));

	forn(k,1,MAXP){
		forn(i,0,n){
			blift[i][k]=merge((i+(1<<(k-1))<n?blift[i+(1<<(k-1))][k-1]:State()), blift[i][k-1]);
			debug(" State "<<i<<" "<<(1<<k)<<" aa= "<<blift[i][k].aa<<" rr= "<<blift[i][k].rr<<" ra= "<<blift[i][k].ra<<" ar= "<<blift[i][k].ar<<'\n');
		}
	}
}

int main(){	cin>>n>>q;
	cin>>color;

	precalc();
	
	forn(i,0,q){
		ll l,r; cin>>l>>r;
		l--; r--;
		State res = State();
		for(int k = MAXP-1; k>=0; k--){
			if(l+(1<<k)-1<=r){
				debug("haciendo "<<l<<" "<<(1<<k)<<'\n');
				res=merge(blift[l][k],res);
				l+=(1<<k);
			}
			if(l-1==r) break;
		}
		cout<<res.ar+res.ra+res.rr+res.aa<<'\n';
	}
	
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...