Submission #158656

# Submission time Handle Problem Language Result Execution time Memory
158656 2019-10-18T09:39:42 Z PeppaPig Street Lamps (APIO19_street_lamps) C++14
100 / 100
1645 ms 76484 KB
#include <bits/stdc++.h>

#define iii tuple<int, int, int>

using namespace std;

const int N = 3e5+5;

void update(vector<int> &t, int x, int k) {
    for(int i = x; i < t.size(); i += i & -i)
        t[i] += k;
}

int query(vector<int> &t, int x) {
    int ret = 0;
    for(int i = x; i; i -= i & -i)
        ret += t[i];
    return ret;
}

int n, q, state[N];
char str[N];
set<iii> S;
vector<iii> ask;
vector<int> coord[N], t[N];

void update_2d(int x, int l, int r, int k) {
    for(int i = x; i <= n; i += i & -i) {
        auto it = lower_bound(coord[i].begin(), coord[i].end(), l + 1);
        if(it == coord[i].begin()) update(t[i], 1, k);
        else if(it != coord[i].end()) update(t[i], it - coord[i].begin(), k);
        it = upper_bound(coord[i].begin(), coord[i].end(), r);
        if(it != coord[i].end()) update(t[i], it - coord[i].begin() + 1, -k);
    }
}

int query_2d(int l, int r) {
    int ret = 0;
    for(int i = l; i; i -= i & -i) {
        int idx = lower_bound(coord[i].begin(), coord[i].end(), r) - coord[i].begin() + 1;
        ret += query(t[i], idx);
    }
    return ret;
}

void set_time(iii seg, int time) {
    int l, r, t; tie(l, r, t) = seg;
    update_2d(l, l, r, time - t);
    update_2d(r + 1, l, r, t - time);
}

int main() {
    scanf("%d %d %s", &n, &q, str + 1);
    for(int i = 1; i <= n; i++) state[i] = (str[i] == '1'); 
    for(int i = 1, j; i <= n + 1; i = j + 1) {
        for(j = i; state[j] && j <= n; j++);
        S.emplace(i, j, 0);
    }
    for(int i = 1; i <= q; i++) {
        char T[10];
        int a, b;
        scanf(" %s", T);
        if(T[0] == 't') {
            scanf("%d", &a);
            ask.emplace_back(0, a, 0);
        } else {
            scanf("%d %d", &a, &b);
            for(int j = a; j; j -= j & -j) coord[j].emplace_back(b);
            ask.emplace_back(1, a, b);
        }
    }
    for(int i = 1; i <= n; i++) {
        sort(coord[i].begin(), coord[i].end());
        coord[i].resize(unique(coord[i].begin(), coord[i].end()) - coord[i].begin());
        if(coord[i].size()) t[i].resize(coord[i].size() + 1);
    }
    int time = 0;
    for(iii p : ask) {
        ++time;
        int T, a, b; tie(T, a, b) = p;
        auto find = [&](int x) { return *prev(S.lower_bound({x + 1, 0, 0})); };
        if(T == 0) {
            if(state[a]) {
                iii seg = find(a);
                set_time(seg, time);
                S.erase(seg);
                S.emplace(get<0>(seg), a, time), S.emplace(a + 1, get<1>(seg), time);
            } else {
                iii seg_l = find(a), seg_r = find(a + 1);
                set_time(seg_l, time), set_time(seg_r, time);
                S.erase(seg_l), S.erase(seg_r);
                S.emplace(get<0>(seg_l), get<1>(seg_r), time);
            }
            state[a] ^= 1;
        } else {
            int ans = query_2d(a, b);
            iii seg = find(a);
            if(get<1>(seg) >= b) ans += time - get<2>(seg);
            printf("%d\n", ans);
        }
    }

    return 0;
}

Compilation message

street_lamps.cpp: In function 'void update(std::vector<int>&, int, int)':
street_lamps.cpp:10:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = x; i < t.size(); i += i & -i)
                    ~~^~~~~~~~~~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:53:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d %s", &n, &q, str + 1);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
street_lamps.cpp:62:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf(" %s", T);
         ~~~~~^~~~~~~~~~
street_lamps.cpp:64:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d", &a);
             ~~~~~^~~~~~~~~~
street_lamps.cpp:67:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d %d", &a, &b);
             ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 14 ms 14456 KB Output is correct
2 Correct 14 ms 14456 KB Output is correct
3 Correct 14 ms 14456 KB Output is correct
4 Correct 14 ms 14456 KB Output is correct
5 Correct 14 ms 14456 KB Output is correct
6 Correct 14 ms 14456 KB Output is correct
7 Correct 14 ms 14456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 279 ms 25892 KB Output is correct
2 Correct 335 ms 26428 KB Output is correct
3 Correct 559 ms 28372 KB Output is correct
4 Correct 1473 ms 54060 KB Output is correct
5 Correct 1498 ms 55760 KB Output is correct
6 Correct 1391 ms 50616 KB Output is correct
7 Correct 1441 ms 75760 KB Output is correct
8 Correct 1010 ms 58648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 14456 KB Output is correct
2 Correct 16 ms 14456 KB Output is correct
3 Correct 16 ms 14456 KB Output is correct
4 Correct 16 ms 14456 KB Output is correct
5 Correct 1205 ms 45044 KB Output is correct
6 Correct 1645 ms 50596 KB Output is correct
7 Correct 1447 ms 54492 KB Output is correct
8 Correct 984 ms 59056 KB Output is correct
9 Correct 168 ms 24736 KB Output is correct
10 Correct 185 ms 27656 KB Output is correct
11 Correct 194 ms 28068 KB Output is correct
12 Correct 1473 ms 76484 KB Output is correct
13 Correct 997 ms 58944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 14584 KB Output is correct
2 Correct 15 ms 14584 KB Output is correct
3 Correct 16 ms 14504 KB Output is correct
4 Correct 15 ms 14456 KB Output is correct
5 Correct 1342 ms 66660 KB Output is correct
6 Correct 1350 ms 59224 KB Output is correct
7 Correct 1348 ms 50288 KB Output is correct
8 Correct 1028 ms 35592 KB Output is correct
9 Correct 424 ms 24628 KB Output is correct
10 Correct 337 ms 23392 KB Output is correct
11 Correct 426 ms 24552 KB Output is correct
12 Correct 330 ms 23392 KB Output is correct
13 Correct 423 ms 24552 KB Output is correct
14 Correct 330 ms 23396 KB Output is correct
15 Correct 1382 ms 76444 KB Output is correct
16 Correct 1045 ms 58904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 14456 KB Output is correct
2 Correct 14 ms 14456 KB Output is correct
3 Correct 14 ms 14456 KB Output is correct
4 Correct 14 ms 14456 KB Output is correct
5 Correct 14 ms 14456 KB Output is correct
6 Correct 14 ms 14456 KB Output is correct
7 Correct 14 ms 14456 KB Output is correct
8 Correct 279 ms 25892 KB Output is correct
9 Correct 335 ms 26428 KB Output is correct
10 Correct 559 ms 28372 KB Output is correct
11 Correct 1473 ms 54060 KB Output is correct
12 Correct 1498 ms 55760 KB Output is correct
13 Correct 1391 ms 50616 KB Output is correct
14 Correct 1441 ms 75760 KB Output is correct
15 Correct 1010 ms 58648 KB Output is correct
16 Correct 15 ms 14456 KB Output is correct
17 Correct 16 ms 14456 KB Output is correct
18 Correct 16 ms 14456 KB Output is correct
19 Correct 16 ms 14456 KB Output is correct
20 Correct 1205 ms 45044 KB Output is correct
21 Correct 1645 ms 50596 KB Output is correct
22 Correct 1447 ms 54492 KB Output is correct
23 Correct 984 ms 59056 KB Output is correct
24 Correct 168 ms 24736 KB Output is correct
25 Correct 185 ms 27656 KB Output is correct
26 Correct 194 ms 28068 KB Output is correct
27 Correct 1473 ms 76484 KB Output is correct
28 Correct 997 ms 58944 KB Output is correct
29 Correct 15 ms 14584 KB Output is correct
30 Correct 15 ms 14584 KB Output is correct
31 Correct 16 ms 14504 KB Output is correct
32 Correct 15 ms 14456 KB Output is correct
33 Correct 1342 ms 66660 KB Output is correct
34 Correct 1350 ms 59224 KB Output is correct
35 Correct 1348 ms 50288 KB Output is correct
36 Correct 1028 ms 35592 KB Output is correct
37 Correct 424 ms 24628 KB Output is correct
38 Correct 337 ms 23392 KB Output is correct
39 Correct 426 ms 24552 KB Output is correct
40 Correct 330 ms 23392 KB Output is correct
41 Correct 423 ms 24552 KB Output is correct
42 Correct 330 ms 23396 KB Output is correct
43 Correct 1382 ms 76444 KB Output is correct
44 Correct 1045 ms 58904 KB Output is correct
45 Correct 135 ms 20080 KB Output is correct
46 Correct 210 ms 20784 KB Output is correct
47 Correct 709 ms 33704 KB Output is correct
48 Correct 1467 ms 53012 KB Output is correct
49 Correct 220 ms 27808 KB Output is correct
50 Correct 208 ms 27940 KB Output is correct
51 Correct 215 ms 28192 KB Output is correct
52 Correct 259 ms 29476 KB Output is correct
53 Correct 260 ms 29348 KB Output is correct
54 Correct 259 ms 29344 KB Output is correct