Submission #510801

#TimeUsernameProblemLanguageResultExecution timeMemory
510801amukkalirStreet Lamps (APIO19_street_lamps)C++17
0 / 100
474 ms712 KiB
#include <bits/stdc++.h>
using namespace std; 
typedef long long ll; 
#define pii pair<int,int> 
#define fi first 
#define se second 
#define pb push_back 
#define mp make_pair

const int nax = 3e5; 
int n, q; 
int tree[4*nax+5]; 
int s[nax+5]; 
int mn[nax+5]; //mulai nyala kapan 

int merge(int a, int b) {
    return max(a,b); 
}

void build(int idx, int l, int r) {
    if(r==l) {
        tree[idx] = mn[l]; 
        //cerr << l << " " << tree[idx] << endl ; 
    }
    else {
        int m = l+r >> 1; 
        build(idx<<1,l,m); 
        build(idx<<1|1, m+1, r); 
        tree[idx] = merge(tree[idx<<1], tree[idx<<1|1]); 
    }
}

void upd(int idx, int l, int r, int where, int val) {
    if(r==l) tree[idx] = val; 
    else if (l <= where && where <= r) {
        int m = l+r >> 1; 
        upd(idx<<1, l, m, where, val); 
        upd(idx<<1|1, m+1, r, where, val); 
        tree[idx] = merge(tree[idx<<1], tree[idx<<1|1]);
    }
}

int rmq(int idx, int l, int r, int from, int to) {
    if(to < l || from > r) return -1; 
    else if(from <= l && r <= to) {
        //cerr << l << " " << r << " returns " << tree[idx] << endl; 
        return tree[idx]; 
    }
    else {
        int m = l+r >> 1; 
        int ret = merge(rmq(idx<<1,l,m,from,to), rmq(idx<<1|1,m+1,r,from,to)); 
        //cerr << l << " " << r << " returns " << ret << endl; 
        return ret; 
    }
}

signed main () {
    scanf("%d %d", &n,&q); 
    for(int i=1; i<=n; i++) {
        int x; 
        scanf("%1d", &x); 
        s[i] = x; 

        if(s[i]) mn[i] = 0; 
        else mn[i] = q+1000; 
    }

    build(1, 1, n); 

    for(int t=1; t<=q; t++) {
        string ts; cin >> ts; 
        if(ts == "query") {
            int a, b; scanf("%d %d", &a, &b); 
            int ans = rmq(1, 1, n, a, b-1); 
            //cerr << ans << endl; 
            if(ans > q) ans = 0; 
            else ans = t-ans; 

            printf("%d\n", ans); 

        } else {
            int x; scanf("%d", &x);            
            upd(1, 1, n, x, t); 
        }
    }
}

Compilation message (stderr)

street_lamps.cpp: In function 'void build(int, int, int)':
street_lamps.cpp:26:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   26 |         int m = l+r >> 1;
      |                 ~^~
street_lamps.cpp: In function 'void upd(int, int, int, int, int)':
street_lamps.cpp:36:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   36 |         int m = l+r >> 1;
      |                 ~^~
street_lamps.cpp: In function 'int rmq(int, int, int, int, int)':
street_lamps.cpp:50:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   50 |         int m = l+r >> 1;
      |                 ~^~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:58:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |     scanf("%d %d", &n,&q);
      |     ~~~~~^~~~~~~~~~~~~~~~
street_lamps.cpp:61:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |         scanf("%1d", &x);
      |         ~~~~~^~~~~~~~~~~
street_lamps.cpp:73:28: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |             int a, b; scanf("%d %d", &a, &b);
      |                       ~~~~~^~~~~~~~~~~~~~~~~
street_lamps.cpp:82:25: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |             int x; scanf("%d", &x);
      |                    ~~~~~^~~~~~~~~~
#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...