# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
838280 | manizare | 가로등 (APIO19_street_lamps) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#GCC pragma optimize("O3,unroll-loops")
#define pb push_back
#define F first
#define S second
#define all(a) a.begin(),a.end()
#define pii pair<int,int>
using namespace std ;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 3e5 + 10 , inf = 1e9+10 ;
int ls[maxn] , fen[maxn] , pre[maxn] , n , q ;
vector <int> vec[maxn * 4] , fenw[4*maxn];
vector <pii> pors[maxn] ;
void upd(int x ,int val){
x++;
while(x <= (n+2)){
fen[x] += val ;
x += x&-x ;
}
}
int que(int x){
x++ ;
int ans= 0 ;
while(x){
ans += fen[x] ;
x -= x&-x;
}
return ans ;
}
void upd2(int ty , int x ,int val){
x++;
while(x < fenw[ty].size()){
fenw[ty][x] += val ;
x += x&-x ;
}
}
int que2(int ty , int x){
x++ ;
int ans= 0 ;
while(x){
ans += fenw[ty][x] ;
x -= x&-x;
}
return ans ;
}
void bui(int p = 1 , int l =0 , int r = n){
int pl = p << 1 , pr = p << 1 | 1 , mid = (l+r) >> 1 ;
if(l == r){
for(int i = 0 ; i < pors[l].size() ; i++){
vec[p].pb(pors[l][i].S) ;
}
sort(all(vec[p])) ;
vec[p].resize(unique(all(vec[p]))-vec[p].begin()) ;
reverse(all(vec[p]));
for(int i = 0 ; i <= vec[p].size() + 2 ; i++){
fenw[p].pb(0) ;
}
return ;
}
bui(pl , l , mid) ;
bui(pr, mid+1 ,r);
vec[p] = vec[pl] ;
for(int i =0 ; i < vec[pr].size() ; i++){
vec[p].pb(vec[pr][i]) ;
}
sort(all(vec[p])) ;
vec[p].resize(unique(all(vec[p]))-vec[p].begin()) ;
reverse(all(vec[p]));
for(int i = 0 ; i <= vec[p].size()+2 ; i++){
fenw[p].pb(0) ;
}
}
void az(int le , int ri , int val , int R , int p = 1 , int l = 0 , int r = n){
int pl = p << 1 , pr = p << 1 | 1, mid = (l+r) >> 1;
if(p == 1){
// cout << le << " " <<ri << " "<< val << " "<< R << "<--\n" ;
}
if(le > r || l > ri)return ;
if(l >= le && ri >= r){
int ll = -1 , rr = vec[p].size() ;
while(rr-ll > 1){
int mid = (ll+rr) >> 1 ;
if(vec[p][mid] <= R)rr = mid ;
else ll = mid ;
}
upd2(p , rr , val) ;
return ;
}
az(le , ri , val , R , pl , l , mid) ;
az(le , ri , val , R , pr , mid+1, r) ;
}
int javab (int x , int R , int p = 1 ,int l = 0 , int r = n){
int pl = p << 1 , pr = p << 1 | 1 , mid = (l + r) >> 1;
if(x > r || l > x)return 0 ;
int ll = -1 , rr = vec[p].size() ;
while(rr-ll > 1){
int mid = (ll+rr) >> 1 ;
if(vec[p][mid] <= R)rr = mid ;
else ll = mid ;
}
if(x==l && x == r)return que2(p , rr) ;
return javab(x , R , pl , l , mid) + javab(x , R , pr , mid+1 ,r) + que2(p , rr) ;
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >>n >> q;
string s;
cin >> s;
s= "0" + s;
pre[0] = 1;
for(int i =1 ; i <= n ; i++){
pre[i] = pre[i-1] + (s[i] == '0') ;
}
for(int i = 0 ;i <= n; i ++){
if(s[i] == '0'){
upd(i , 1) ;
}
}
vector <pair <int ,pii> > qu ;
for(int i =1 ; i <= q; i ++){
string a;
cin >> a;
if(a[0] == 'q'){
int l , r ;
cin >> l >> r ;
r--;l--;
pors[l].pb({i , r}) ;
qu.pb({0 , {l , r}}) ;
}else{
int x;
cin >> x;
qu.pb({1 , {x , x}}) ;
}
}
bui();
for(int i = 1 ; i <= q ; i++){
int ty = qu[i-1].F , l = qu[i-1].S.F , r = qu[i-1].S.S ;
if(ty == 1){
int a = que(l-1) , b= que(l) ;
if(a==b){
int le = -1 , ri = l ;
while(ri-le > 1){
int mid = (le + ri) / 2;
if(que(mid) == a){
ri = mid ;
}else{
le = mid ;
}
}
int rr = ri ;
le = l , ri = n+1 ;
while(ri-le > 1){
int mid = (le + ri) / 2;
if(que(mid) == a){
le = mid ;
}else{
ri = mid ;
}
}
az(rr , le , i - ls[le] , le) ;
ls[le] = ls[l-1] = i ;
}
if(s[l] == '0'){
s[l] = '1' ;
upd(l , -1) ;
}else{
s[l] = '0' ;
upd(l , 1) ;
}
if(a==b)continue ;
int aa = que(l-1) , bb = que(l) ;
if(a != b && aa == bb){
int le= -1 , ri = l-1 ;
while(ri-le > 1){
int mid = (le + ri)/2 ;
if(que(mid) == aa)ri = mid ;
else le = mid ;
}
az(ri , l-1 , i-ls[l-1] , l-1);
le= l , ri = n+1 ;
while(ri - le > 1){
int mid =(le + ri)/2 ;
if(que(mid) == bb){
le = mid ;
}else{
ri = mid ;
}
}
az(l , le, i-ls[le] , le);
ls[le] = i ;
}
}
else {
int le = l , ri = n+1;
int z =que(l) ;
while(ri - le > 1){
int mid = (le + ri) / 2;
if(que(mid )== z)le = mid ;
else ri = mid ;
}
int ans = javab(l, r) ;
if(r <= le){
ans += i-ls[le] ;
}
cout << ans << "\n" ;
}
}
}
/*
*/