Submission #251880

# Submission time Handle Problem Language Result Execution time Memory
251880 2020-07-22T15:11:48 Z egekabas Street Lamps (APIO19_street_lamps) C++14
100 / 100
2819 ms 144880 KB
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
struct fen{
    vector<int> v;
    vector<int> bit;
    void add(int x){
        v.pb(x);
    }
    void init(){
        sort(v.begin(), v.end());
        v.resize(unique(v.begin(), v.end())-v.begin());
        bit.resize(v.size()+1);
    }
    int getid(int x){
        return lower_bound(v.begin(), v.end(), x)-v.begin()+1;
    }
    void upd(int x, int y){
        x = getid(x);
        for(; x > 0; x -= x&(-x))
            bit[x] += y;
    }
    int get(int x){
        x = getid(x);
        int ret = 0;
        for(; x < bit.size(); x += x&(-x))
            ret += bit[x];
        //cout << ret << " HEY\n";
        return ret;
    }
};
fen seg[1200000];
void add(int v, int tl, int tr, int idx, int val){
    seg[v].add(val);
    if(idx == tl && idx == tr) return;
    if(idx <= (tl+tr)/2)
        add(2*v, tl, (tl+tr)/2, idx, val);
    else
        add(2*v+1, (tl+tr)/2+1, tr, idx, val);
}
void init(int v, int tl, int tr){
    seg[v].init();
    if(tl == tr) return;
    init(2*v, tl, (tl+tr)/2);
    init(2*v+1, (tl+tr)/2+1, tr); 
}
void upd(int v, int tl, int tr, int idx, int place, int val){
    seg[v].upd(place, val);
    if(idx == tl && idx == tr) return;
    if(idx <= (tl+tr)/2)
        upd(2*v, tl, (tl+tr)/2, idx, place, val);
    else
        upd(2*v+1, (tl+tr)/2+1, tr, idx, place, val);
}
int get(int v, int tl, int tr, int l, int r, int place){
    if(r < tl || tr < l) return 0;
    if(l <= tl && tr <= r){
        return seg[v].get(place);
    }
    else{
        return get(2*v, tl, (tl+tr)/2, l, r, place)+
        get(2*v+1, (tl+tr)/2+1, tr, l, r, place);
    }
}
bitset<300009> lbeg;
set<pii> beg;
int n, q;
pair<int, pii> qu[300009];
vector<pair<pii, int>> erased;
void addlight(int idx, set<pair<pii, int>> &s, int time){
    int l = idx;
    int r = idx;
    auto it = s.upper_bound({{idx,1e9}, 1e9});
    if(it != s.end() && (*it).ff.ff == idx+1){
        erased.pb(*it);
        r = (*it).ff.ss;
        s.erase(it);
    }
    it = s.upper_bound({{idx,1e9}, 1e9});
    if(it != s.begin()){
        --it;
        if((*it).ff.ss == idx-1){
            erased.pb(*it);
            l = (*it).ff.ff;
            s.erase(it);
        }
    }
    s.insert({{l, r}, time});
}
void eraselight(int idx, set<pair<pii, int>> &s, int time){
    auto it = s.upper_bound({{idx, 1e9}, 1e9});
    --it;
    
    if(idx != (*it).ff.ff)
        s.insert({{(*it).ff.ff, idx-1}, time});
    if(idx != (*it).ff.ss)
        s.insert({{idx+1, (*it).ff.ss}, time});
    erased.pb(*it);
    s.erase(it);
}
bitset<300009> st;
set<pair<pii, int>> s;
void initall(){
    s.clear();
    for(auto u : beg)
        s.insert({u, 0});
    st = lbeg;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);

    cin >> n >> q;
    string str;
    cin >> str;
    for(int i = 0; i < n; ++i){
        lbeg[i] = (str[i]=='1');
    }
    int last = n;
    for(int i = n-1; i >= 0; --i){
        if(lbeg[i] == 0)
            last = i;
        else if(i == 0 || lbeg[i-1] == 0)
            beg.insert({i, last-1});
    }

    initall();

    for(int i = 1; i <= q; ++i){
        string str;
        cin >> str;
        if(str == "toggle"){
            int x;
            cin >> x;
            --x;
            qu[i].ff = 0;
            qu[i].ss.ff = x;
            if(st[x])
                eraselight(x, s, i);
            else
                addlight(x, s, i);
            st[x] = (!st[x]);

        }
        else{
            int x, y;
            cin >> x >> y;
            --x;
            y -= 2;
            qu[i] = {1, {x, y}};
        }
    }
    for(auto u : erased){
        add(1, 0, n-1, u.ff.ff, u.ff.ss);
    }
    erased.clear();
    init(1, 0, n-1);
    initall();
    for(ll i = 1; i <= q; ++i){
        if(qu[i].ff == 0){
            int x = qu[i].ss.ff;
            if(st[x])
                eraselight(x, s, i);
            else
                addlight(x, s, i);
            for(auto u : erased)
                upd(1, 0, n-1, u.ff.ff, u.ff.ss, i-u.ss);
            erased.clear();
            st[x] = (!st[x]);
        }
        else{
            int x = qu[i].ss.ff;
            int y = qu[i].ss.ss;
            int ans = get(1, 0, n-1, 0, x, y);
            auto it = s.upper_bound({{x, 1e9}, 1e9});
            if(it != s.begin()){
                --it;
                if((*it).ff.ss >= y)
                    ans += i-(*it).ss;
            }
            cout << ans << '\n';
        }
    }
}

Compilation message

street_lamps.cpp: In member function 'int fen::get(int)':
street_lamps.cpp:36:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(; x < bit.size(); x += x&(-x))
               ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 33 ms 56696 KB Output is correct
2 Correct 31 ms 56704 KB Output is correct
3 Correct 32 ms 56704 KB Output is correct
4 Correct 32 ms 56704 KB Output is correct
5 Correct 33 ms 56700 KB Output is correct
6 Correct 31 ms 56704 KB Output is correct
7 Correct 32 ms 56704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 307 ms 69480 KB Output is correct
2 Correct 393 ms 74212 KB Output is correct
3 Correct 613 ms 79460 KB Output is correct
4 Correct 1833 ms 123548 KB Output is correct
5 Correct 1965 ms 127168 KB Output is correct
6 Correct 1965 ms 126908 KB Output is correct
7 Correct 330 ms 85900 KB Output is correct
8 Correct 320 ms 87312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 56952 KB Output is correct
2 Correct 45 ms 56952 KB Output is correct
3 Correct 36 ms 56952 KB Output is correct
4 Correct 33 ms 56832 KB Output is correct
5 Correct 2457 ms 143088 KB Output is correct
6 Correct 2411 ms 142836 KB Output is correct
7 Correct 1920 ms 126112 KB Output is correct
8 Correct 308 ms 87308 KB Output is correct
9 Correct 128 ms 63308 KB Output is correct
10 Correct 145 ms 63864 KB Output is correct
11 Correct 153 ms 64120 KB Output is correct
12 Correct 304 ms 85916 KB Output is correct
13 Correct 318 ms 87308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 56824 KB Output is correct
2 Correct 42 ms 56824 KB Output is correct
3 Correct 47 ms 56952 KB Output is correct
4 Correct 34 ms 57012 KB Output is correct
5 Correct 552 ms 95112 KB Output is correct
6 Correct 1236 ms 112772 KB Output is correct
7 Correct 1876 ms 126332 KB Output is correct
8 Correct 2819 ms 144880 KB Output is correct
9 Correct 441 ms 77276 KB Output is correct
10 Correct 385 ms 79320 KB Output is correct
11 Correct 438 ms 77152 KB Output is correct
12 Correct 405 ms 79448 KB Output is correct
13 Correct 444 ms 77152 KB Output is correct
14 Correct 389 ms 79704 KB Output is correct
15 Correct 337 ms 85900 KB Output is correct
16 Correct 318 ms 87308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 56696 KB Output is correct
2 Correct 31 ms 56704 KB Output is correct
3 Correct 32 ms 56704 KB Output is correct
4 Correct 32 ms 56704 KB Output is correct
5 Correct 33 ms 56700 KB Output is correct
6 Correct 31 ms 56704 KB Output is correct
7 Correct 32 ms 56704 KB Output is correct
8 Correct 307 ms 69480 KB Output is correct
9 Correct 393 ms 74212 KB Output is correct
10 Correct 613 ms 79460 KB Output is correct
11 Correct 1833 ms 123548 KB Output is correct
12 Correct 1965 ms 127168 KB Output is correct
13 Correct 1965 ms 126908 KB Output is correct
14 Correct 330 ms 85900 KB Output is correct
15 Correct 320 ms 87312 KB Output is correct
16 Correct 35 ms 56952 KB Output is correct
17 Correct 45 ms 56952 KB Output is correct
18 Correct 36 ms 56952 KB Output is correct
19 Correct 33 ms 56832 KB Output is correct
20 Correct 2457 ms 143088 KB Output is correct
21 Correct 2411 ms 142836 KB Output is correct
22 Correct 1920 ms 126112 KB Output is correct
23 Correct 308 ms 87308 KB Output is correct
24 Correct 128 ms 63308 KB Output is correct
25 Correct 145 ms 63864 KB Output is correct
26 Correct 153 ms 64120 KB Output is correct
27 Correct 304 ms 85916 KB Output is correct
28 Correct 318 ms 87308 KB Output is correct
29 Correct 34 ms 56824 KB Output is correct
30 Correct 42 ms 56824 KB Output is correct
31 Correct 47 ms 56952 KB Output is correct
32 Correct 34 ms 57012 KB Output is correct
33 Correct 552 ms 95112 KB Output is correct
34 Correct 1236 ms 112772 KB Output is correct
35 Correct 1876 ms 126332 KB Output is correct
36 Correct 2819 ms 144880 KB Output is correct
37 Correct 441 ms 77276 KB Output is correct
38 Correct 385 ms 79320 KB Output is correct
39 Correct 438 ms 77152 KB Output is correct
40 Correct 405 ms 79448 KB Output is correct
41 Correct 444 ms 77152 KB Output is correct
42 Correct 389 ms 79704 KB Output is correct
43 Correct 337 ms 85900 KB Output is correct
44 Correct 318 ms 87308 KB Output is correct
45 Correct 161 ms 65000 KB Output is correct
46 Correct 213 ms 66540 KB Output is correct
47 Correct 930 ms 87044 KB Output is correct
48 Correct 1921 ms 123236 KB Output is correct
49 Correct 146 ms 64376 KB Output is correct
50 Correct 147 ms 64412 KB Output is correct
51 Correct 150 ms 64504 KB Output is correct
52 Correct 155 ms 64888 KB Output is correct
53 Correct 159 ms 64888 KB Output is correct
54 Correct 148 ms 64888 KB Output is correct