답안 #403224

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
403224 2021-05-12T23:48:54 Z AmineTrabelsi 가로등 (APIO19_street_lamps) C++14
40 / 100
4912 ms 524292 KB
// subtasks will not match always 
#include <bits/stdc++.h>
using namespace std;
// Hi
int n,q;
string s;
vector<pair<int,pair<int,int>>> que; // 0 toggle 1 query
void sub_one(){
    vector<vector<int>> cnt(n+5,vector<int>(n+5,0));
    vector<int> pref(n+5,0);
    for(int i=0;i<n;i++){
        pref[i+1] = pref[i]+(s[i]=='1');
    }
    for(int i=0;i<=n;i++){
        for(int j=0;j<i;j++){
            cnt[i][j] = cnt[j][i] = (pref[i]-pref[j] == i-j);
        }
    }
    for(int tt=0;tt<q;tt++){
        if(que[tt].first == 0){
            int ind = que[tt].second.first;
            if(s[ind] == '0')s[ind] = '1';
            else s[ind] = '0';
            for(int i=0;i<n;i++){
                pref[i+1] = pref[i]+(s[i]=='1');
            }
        }else{
            int a = que[tt].second.first,b = que[tt].second.second;
            cout << cnt[a][b] << '\n';
        }
        for(int i=0;i<=n;i++){
            for(int j=0;j<i;j++){
                cnt[i][j] = cnt[j][i] += (pref[i]-pref[j] == i-j);
            }
        }
    }
}
void sub_two(){
    vector<int> cnt,last_on(n+1,0);
    for(auto i:s)cnt.push_back(i-'0');
    vector<bool> on(cnt.begin(),cnt.end());
    for(int i=0;i<n;i++){
        if(on[i]){
            last_on[i] = -1;
        }else last_on[i] = 1e9+7;
    }
    for(int time=0;time<q;time++){
        if(que[time].first == 0){
            int ind = que[time].second.first;
            if(!on[ind]){
                on[ind] = 1;
                last_on[ind] = time;
                cnt[ind]++;
            }else{
                int pre = time-last_on[ind]-1;
                cnt[ind] += (pre >= 0 ? pre : 0);
                last_on[ind] = 1e9+7;
                on[ind] = 0;
            }
        }else{
            int a = que[time].second.first;
            int pre = time-last_on[a]-1;
            cout << cnt[a]+(pre >= 0 ? pre : 0) << '\n';
        }
    }
}
const int M = 300005;
int tree[M*4+50];
void build(int v,int tl,int tr){
    if(tl == tr){
        tree[v] = (s[tr] == '1' ? 0 : 1e9+7);
        return;
    }
    int mid = (tl+tr)/2;
    build(v*2,tl,mid);
    build(v*2+1,mid+1,tr);
    tree[v] = max(tree[v*2],tree[v*2+1]);
}
void upd(int v,int tl,int tr,int ind,int val){
    if(tl == tr && tl == ind){
        tree[v] = val;
        return;
    }if(tl == tr)return;
    int mid = (tl+tr)/2;
    if(ind <= mid){
        upd(v*2,tl,mid,ind,val);
    }else upd(v*2+1,mid+1,tr,ind,val);
    tree[v] = max(tree[v*2],tree[v*2+1]);
}
int mx(int v,int tl,int tr,int ql,int qr){
    if(ql > qr || tl > qr || tr < ql)return -(1e9+7);
    if(ql == tl && tr == qr){
        return tree[v];
    }if(tl == tr)return -(1e9+7);
    int mid = (tl+tr)/2;
    return max(mx(v*2,tl,mid,ql,min(mid,qr)),mx(v*2+1,mid+1,tr,max(mid+1,ql),qr));
}
void sub_three(){
    vector<bool> on;
    for(auto i:s)on.push_back((i=='1' ? 1 : 0));
    build(1,0,n-1);
    for(int time=0;time<q;time++){
        if(que[time].first == 0){
            int ind = que[time].second.first;
            upd(1,0,n-1,ind,time+1);
        }else{
            int a = que[time].second.first,b = que[time].second.second;
            int ans =  time-mx(1,0,n-1,a,b-1)+1;
            //cout << time-ans+1<<' ';
            cout << (ans >= 0 ? ans : 0) << '\n';
        }
    }
}
int main(){
    ios::sync_with_stdio(0);cin.tie(0);
    cin>>n>>q>>s;
    vector<bool> on;
    for(auto i:s)on.push_back((i=='1' ? 1 : 0));
    bool one = (n <= 200),two = 1,three = 0,four = 1;
    bool ex = 0;
    for(int i=0;i<q;i++){
        string t;
        cin>>t;
        if(t == "toggle"){
            if(ex)four = 0;
            int ind;
            cin>>ind;
            if(on[ind]){
                three = 0;
                on[ind] = 0;
            }else on[ind] = 1;
            que.push_back({0,{ind-1,0}});
        }else{
            ex = 1;
            int a,b;
            cin>>a>>b;
            if(b-a != 1)two = 0;
            que.push_back({1,{a-1,b-1}});
        }
    }/*
    sub_three();
    */
    if(one){
        sub_one();
    }else if(two){
        sub_two();
    }else if(three)sub_three();
    else sub_one();
    
    return 0;
}
/*
5 8
11011
toggle 3
toggle 3
query 1 2
query 1 2
query 1 6
query 3 4
query 3 4
query 1 6

*/

Compilation message

street_lamps.cpp: In function 'int main()':
street_lamps.cpp:119:45: warning: variable 'four' set but not used [-Wunused-but-set-variable]
  119 |     bool one = (n <= 200),two = 1,three = 0,four = 1;
      |                                             ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 3 ms 332 KB Output is correct
6 Correct 3 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4912 ms 6508 KB Output is correct
2 Correct 96 ms 6604 KB Output is correct
3 Correct 98 ms 6584 KB Output is correct
4 Correct 112 ms 9336 KB Output is correct
5 Correct 129 ms 9700 KB Output is correct
6 Correct 110 ms 9240 KB Output is correct
7 Correct 138 ms 9232 KB Output is correct
8 Correct 140 ms 10544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1852 ms 4312 KB Output is correct
2 Correct 1878 ms 4312 KB Output is correct
3 Correct 1824 ms 4308 KB Output is correct
4 Correct 1802 ms 4420 KB Output is correct
5 Runtime error 312 ms 524292 KB Execution killed with signal 9
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1828 ms 4304 KB Output is correct
2 Correct 1781 ms 4308 KB Output is correct
3 Correct 1794 ms 4308 KB Output is correct
4 Correct 1777 ms 4312 KB Output is correct
5 Runtime error 344 ms 524292 KB Execution killed with signal 9
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 3 ms 332 KB Output is correct
6 Correct 3 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 4912 ms 6508 KB Output is correct
9 Correct 96 ms 6604 KB Output is correct
10 Correct 98 ms 6584 KB Output is correct
11 Correct 112 ms 9336 KB Output is correct
12 Correct 129 ms 9700 KB Output is correct
13 Correct 110 ms 9240 KB Output is correct
14 Correct 138 ms 9232 KB Output is correct
15 Correct 140 ms 10544 KB Output is correct
16 Correct 1852 ms 4312 KB Output is correct
17 Correct 1878 ms 4312 KB Output is correct
18 Correct 1824 ms 4308 KB Output is correct
19 Correct 1802 ms 4420 KB Output is correct
20 Runtime error 312 ms 524292 KB Execution killed with signal 9
21 Halted 0 ms 0 KB -