Audition (Sin binary search)
#include <iostream>
#include <vector>
#include <stdio.h>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <bitset>
using namespace std;
#define ll long long
#define mp make_pair
int const MOD = 1000000007;
int main(){
int t;
cin>>t;
while(t>0){
long long n,k,actual,count,r;
count=0;
actual=0;
cin>>n>>k;
string a;
cin>>a;
if(k==0){
for(int i=0;i<n;i++){
if(a[i]=='0'){
actual++;
}
else{
count=count+actual*(actual+1)/2;
actual=0;
}
}
count=count+actual*(actual+1)/2;
cout<<count<<endl;
t--;
continue;
}
vector <int> v(n+1,0);
vector <int> v1(3000000,0);
for(int i=0;i<n;i++){
if(a[i]=='1'){
actual++;
v[i+1]=actual;
}
else{
v[i+1]=actual;
}
}
for(int i=1;i<=n;i++){
v1[v[i]]++;
}
for(int i=1;i<=n;i++){
r=k+v[i-1];
count=count+v1[r];
}
cout<<count<<endl;
t--;
}
return 0;
}
Ayuda
Por si alguien tiene WA yo tuve muchos por el caso k=0 y por no usar long long
Ada and Field
#include <iostream>
#include <vector>
#include <stdio.h>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <bitset>
using namespace std;
#define ll long long
#define mp make_pair
int const MOD = 1000000007;
int main(){
int t;
cin>>t;
while(t>0){
int n,m,q,p,r,total;
cin>>n>>m>>q;
set<ll>::iterator it;
set <ll> ver;
set <ll> hor;
ver.insert(0);
ver.insert(n);
hor.insert(0);
hor.insert(m);
multiset <ll> sver;
multiset <ll> shor;
sver.insert(n);
shor.insert(m);
int a,b;
for(int i=0;i<q;i++){
cin>>a>>b;
if(a==0){
if(ver.count(b)==1){
cout<<(*sver.rbegin())*(*shor.rbegin())<<endl;
continue;
}
ver.insert(b);
it=ver.find(b);
++it;
p=*it;
--it;
--it;
r=*it;
total=p-r;
sver.erase(sver.find(total));
sver.insert(p-b);
sver.insert(b-r);
cout<<(*sver.rbegin())*(*shor.rbegin())<<endl;
}
else{
if(hor.count(b)==1){
cout<<(*sver.rbegin())*(*shor.rbegin())<<endl;
continue;
}
hor.insert(b);
it=hor.find(b);
++it;
p=*it;
--it;
--it;
r=*it;
total=p-r;
shor.erase(shor.find(total));
shor.insert(p-b);
shor.insert(b-r);
cout<<(*sver.rbegin())*(*shor.rbegin())<<endl;
}
}
t--;
}
return 0;
}
Comentario
Me parece que este problema es mas data structures que binary search