Submission #131261

# Submission time Handle Problem Language Result Execution time Memory
131261 2019-07-16T21:48:47 Z osaaateiasavtnl Street Lamps (APIO19_street_lamps) C++14
100 / 100
2433 ms 116768 KB
#include <bits/stdc++.h>
using namespace std;
#define app push_back
const int N = 3e5 + 7;
struct Quer {
    int t, i, l, r;
};
struct Fen {
    int w, l, cnt;
    Fen() {
        w = 0; l = 0; cnt = 0;
    }   
};  
struct Event {
    int t, l, r, tm;
};
int n, q;
bool a[N]; 
Quer d[N];
vector <int> f[N];
vector <Event> mem;
void add(int l, int r, int t) {
    r = n - r;
    mem.app({1, l, r, t});
    for (int i = l; i < N; i |= i + 1) {
        f[i].app(r);
    }   
}
int cl[N], cr[N];
int sum[N << 2];
void build(int v, int tl, int tr) {
    if (tl == tr) {
        sum[v] = a[tl];
        return;
    }   
    int tm = (tl + tr) >> 1;
    build(v * 2 + 1, tl, tm); build(v * 2 + 2, tm + 1, tr);
    sum[v] = sum[v * 2 + 1] + sum[v * 2 + 2];
}   
void upd(int v, int tl, int tr, int p, int x) {
    if (tr == tl) {
        sum[v] = x;
        return;
    }   
    int tm = (tl + tr) >> 1;
    if (p <= tm) upd(v * 2 + 1, tl, tm, p, x);
    else upd(v * 2 + 2, tm + 1, tr, p, x);
    sum[v] = sum[v * 2 + 1] + sum[v * 2 + 2];
}
int getl(int v, int tl, int tr, int l, int r) {
    if (tr < l || r < tl || sum[v] == tr - tl + 1) return n;
    if (tl == tr) return tl;
    int tm = (tl + tr) >> 1;
    int t = getl(v * 2 + 1, tl, tm, l, r);
    if (t != n) return t;
    else return getl(v * 2 + 2, tm + 1, tr, l, r);    
}     
int getr(int v, int tl, int tr, int l, int r) {
    if (tr < l || r < tl || sum[v] == tr - tl + 1) return -1;
    if (tl == tr) return tl;
    int tm = (tl + tr) >> 1;
    int t = getr(v * 2 + 2, tm + 1, tr, l, r);
    if (t != -1) return t;
    else return getr(v * 2 + 1, tl, tm, l, r);
}   
vector <Fen> fn[N];
void add1(int x, int y, int t) {
    for (int i = x; i < N; i |= i + 1) {
        int j = lower_bound(f[i].begin(), f[i].end(), y) - f[i].begin();
        for (; j < fn[i].size(); j |= j + 1) {
            fn[i][j].l += t;
            fn[i][j].cnt++;
        }   
    }   
}   
void del1(int x, int y, int t) {
    for (int i = x; i < N; i |= i + 1) {
        int j = lower_bound(f[i].begin(), f[i].end(), y) - f[i].begin();
        for (; j < fn[i].size(); j |= j + 1) {
            fn[i][j].l -= t;
            fn[i][j].cnt--;
        }   
    }   
}   
void add2(int x, int y, int t) {
    for (int i = x; i < N; i |= i + 1) {
        int j = lower_bound(f[i].begin(), f[i].end(), y) - f[i].begin();
        for (; j < fn[i].size(); j |= j + 1) {
            fn[i][j].w += t;
        }   
    }       
}   
int get(int x, int y, int t) {
    int ans = 0;
    for (int i = x; i >= 0; i &= i + 1, --i) {
        int j = upper_bound(f[i].begin(), f[i].end(), y) - f[i].begin() - 1;
        for (; j >= 0; j &= j + 1, --j) {
            ans += fn[i][j].w;
            ans -= fn[i][j].l;
            ans += fn[i][j].cnt * t;
        }   
    }
    return ans;   
}   

map <int, int> dc[N];

signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    cin >> n >> q;
    for (int i = 0; i < n; ++i) {
        char c;
        cin >> c;
        a[i] = c == '1';
    }   
    for (int i = 0; i < q; ++i) {
        string t;
        cin >> t;
        if (t == "toggle") {
            d[i].t = 1;
            cin >> d[i].i;
            --d[i].i;
        }
        else {
            d[i].t = 2;
            cin >> d[i].l >> d[i].r;
            --d[i].r;
            --d[i].l; --d[i].r;
        }
    }
    int l = -1;
    for (int i = 0; i < n; ++i) {
        if (!a[i]) {
            if (l != -1) {
                add(l, i - 1, 0);
                l = -1;
            }
        }   
        else {
            if (l == -1) {
                l = i;
            }   
        }   
    }   
    if (a[n - 1]) {
        add(l, n - 1, 0);
    }   
    build(0, 0, n - 1);
    for (int i = 0; i < q; ++i) {
        if (d[i].t == 1) {
            int p = d[i].i;
            if (a[p]) {
                int l = getr(0, 0, n - 1, 0, p - 1) + 1;
                int r = getl(0, 0, n - 1, p + 1, n - 1) - 1;
                mem.app({2, l, n - r, i + 1});
                if (l != p) {
                    add(l, p - 1, i + 1);
                }   
                if (r != p) {
                    add(p + 1, r, i + 1);
                }   
            }
            else {
                int l = getr(0, 0, n - 1, 0, p - 1) + 1;
                int r = getl(0, 0, n - 1, p + 1, n - 1) - 1;
                if (l != p) {
                    mem.app({2, l, n - (p - 1), i + 1});
                }   
                if (r != p) {
                    mem.app({2, p + 1, n - r, i + 1});
                }
                add(l, r, i + 1);
            }
            a[p] ^= 1;
            upd(0, 0, n - 1, p, a[p]);
        }
        else {
            mem.app({3, d[i].l, n - d[i].r, i + 1});                
        }   
    }   
    for (int i = 0; i < N; ++i) {
        sort(f[i].begin(), f[i].end());
        f[i].resize(unique(f[i].begin(), f[i].end()) - f[i].begin());
    }   
    for (int i = 0; i < N; ++i) {
        fn[i].resize(f[i].size());
    }   

    #ifdef HOME
    for (auto e : mem) {
        cout << e.t << ' ' << e.l << ' ' << -e.r + n << ' ' << e.tm << '\n';
    }   
    #endif

    for (auto e : mem) {
        if (e.t == 1) {
            add1(e.l, e.r, e.tm);  
            dc[e.l][e.r] = e.tm;
        }
        else if (e.t == 2) {
            int pr = dc[e.l][e.r];
            del1(e.l, e.r, pr);
            add2(e.l, e.r, e.tm - pr);
        }
        else {
            cout << get(e.l, e.r, e.tm) << '\n';
        }   
    }   
}   

Compilation message

street_lamps.cpp: In function 'void add1(int, int, int)':
street_lamps.cpp:70:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (; j < fn[i].size(); j |= j + 1) {
                ~~^~~~~~~~~~~~~~
street_lamps.cpp: In function 'void del1(int, int, int)':
street_lamps.cpp:79:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (; j < fn[i].size(); j |= j + 1) {
                ~~^~~~~~~~~~~~~~
street_lamps.cpp: In function 'void add2(int, int, int)':
street_lamps.cpp:88:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (; j < fn[i].size(); j |= j + 1) {
                ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 30 ms 28536 KB Output is correct
2 Correct 29 ms 28536 KB Output is correct
3 Correct 30 ms 28536 KB Output is correct
4 Correct 30 ms 28536 KB Output is correct
5 Correct 30 ms 28792 KB Output is correct
6 Correct 29 ms 28536 KB Output is correct
7 Correct 30 ms 28536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 602 ms 55480 KB Output is correct
2 Correct 707 ms 56784 KB Output is correct
3 Correct 961 ms 57500 KB Output is correct
4 Correct 1453 ms 95804 KB Output is correct
5 Correct 1451 ms 91068 KB Output is correct
6 Correct 1612 ms 99400 KB Output is correct
7 Correct 204 ms 51940 KB Output is correct
8 Correct 205 ms 51936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 28792 KB Output is correct
2 Correct 34 ms 28864 KB Output is correct
3 Correct 33 ms 28792 KB Output is correct
4 Correct 30 ms 28536 KB Output is correct
5 Correct 2358 ms 107652 KB Output is correct
6 Correct 1984 ms 103184 KB Output is correct
7 Correct 1390 ms 90228 KB Output is correct
8 Correct 209 ms 51956 KB Output is correct
9 Correct 126 ms 39896 KB Output is correct
10 Correct 139 ms 44128 KB Output is correct
11 Correct 138 ms 44240 KB Output is correct
12 Correct 201 ms 51936 KB Output is correct
13 Correct 209 ms 51908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 28664 KB Output is correct
2 Correct 32 ms 28728 KB Output is correct
3 Correct 33 ms 28792 KB Output is correct
4 Correct 34 ms 28900 KB Output is correct
5 Correct 431 ms 72752 KB Output is correct
6 Correct 1000 ms 87656 KB Output is correct
7 Correct 1631 ms 98976 KB Output is correct
8 Correct 2433 ms 116768 KB Output is correct
9 Correct 933 ms 60888 KB Output is correct
10 Correct 986 ms 79044 KB Output is correct
11 Correct 927 ms 60880 KB Output is correct
12 Correct 984 ms 79132 KB Output is correct
13 Correct 933 ms 60968 KB Output is correct
14 Correct 989 ms 79060 KB Output is correct
15 Correct 192 ms 51936 KB Output is correct
16 Correct 211 ms 51952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 28536 KB Output is correct
2 Correct 29 ms 28536 KB Output is correct
3 Correct 30 ms 28536 KB Output is correct
4 Correct 30 ms 28536 KB Output is correct
5 Correct 30 ms 28792 KB Output is correct
6 Correct 29 ms 28536 KB Output is correct
7 Correct 30 ms 28536 KB Output is correct
8 Correct 602 ms 55480 KB Output is correct
9 Correct 707 ms 56784 KB Output is correct
10 Correct 961 ms 57500 KB Output is correct
11 Correct 1453 ms 95804 KB Output is correct
12 Correct 1451 ms 91068 KB Output is correct
13 Correct 1612 ms 99400 KB Output is correct
14 Correct 204 ms 51940 KB Output is correct
15 Correct 205 ms 51936 KB Output is correct
16 Correct 33 ms 28792 KB Output is correct
17 Correct 34 ms 28864 KB Output is correct
18 Correct 33 ms 28792 KB Output is correct
19 Correct 30 ms 28536 KB Output is correct
20 Correct 2358 ms 107652 KB Output is correct
21 Correct 1984 ms 103184 KB Output is correct
22 Correct 1390 ms 90228 KB Output is correct
23 Correct 209 ms 51956 KB Output is correct
24 Correct 126 ms 39896 KB Output is correct
25 Correct 139 ms 44128 KB Output is correct
26 Correct 138 ms 44240 KB Output is correct
27 Correct 201 ms 51936 KB Output is correct
28 Correct 209 ms 51908 KB Output is correct
29 Correct 31 ms 28664 KB Output is correct
30 Correct 32 ms 28728 KB Output is correct
31 Correct 33 ms 28792 KB Output is correct
32 Correct 34 ms 28900 KB Output is correct
33 Correct 431 ms 72752 KB Output is correct
34 Correct 1000 ms 87656 KB Output is correct
35 Correct 1631 ms 98976 KB Output is correct
36 Correct 2433 ms 116768 KB Output is correct
37 Correct 933 ms 60888 KB Output is correct
38 Correct 986 ms 79044 KB Output is correct
39 Correct 927 ms 60880 KB Output is correct
40 Correct 984 ms 79132 KB Output is correct
41 Correct 933 ms 60968 KB Output is correct
42 Correct 989 ms 79060 KB Output is correct
43 Correct 192 ms 51936 KB Output is correct
44 Correct 211 ms 51952 KB Output is correct
45 Correct 274 ms 49732 KB Output is correct
46 Correct 404 ms 49412 KB Output is correct
47 Correct 898 ms 63760 KB Output is correct
48 Correct 1459 ms 95468 KB Output is correct
49 Correct 145 ms 45024 KB Output is correct
50 Correct 143 ms 45092 KB Output is correct
51 Correct 145 ms 45284 KB Output is correct
52 Correct 152 ms 45576 KB Output is correct
53 Correct 151 ms 45520 KB Output is correct
54 Correct 150 ms 45488 KB Output is correct