답안 #781172

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
781172 2023-07-12T20:27:31 Z PanosPask 가로등 (APIO19_street_lamps) C++14
100 / 100
1385 ms 50144 KB
#include <bits/stdc++.h>

using namespace std;

struct BITree {
    int size;
    vector<int> tree;

    void add(int i, int v) {
        for (int x = i; x < size; x = x | (x + 1))
            tree[x] += v;
    }
    int get(int i) {
        int res = 0;
        for (int x = i; x >= 0; x = (x & (x + 1)) - 1)
            res += tree[x];
        return res;
    }
    int get(int l, int r) {
        return get(r) - get(l - 1);
    }

    void init(int n) {
        size = n;
        tree.assign(size, 0);
    }
};

const int POINT = 0;
const int START_Q = -1;
const int END_Q = 1;
struct Event {
    int type;
    int time;
    int x;
    int y;

    // Variables for points only
    int value;

    // Variables for queries only
    int id;
};
bool x_sort(const Event& a, const Event& b)
{
    return a.x < b.x;
}

struct LitUp {
    int l, r;
    int time_on;

    LitUp(int a, int b, int c) : l(a), r(b), time_on(c) {};
};
bool operator <(const LitUp& a, const LitUp& b)
{
    return a.l < b.l;
}

int n, q;
vector<int> ans;
BITree segments;
set<LitUp> active;
vector<bool> bulbs;
vector<Event> events;

void update(const Event& a, int mul = 1)
{
    if (a.type != POINT)
        return;
    segments.add(a.y, a.value * mul);
}
void query(const Event& a)
{
    if (a.type == POINT)
        return;

    ans[a.id] += a.type * segments.get(a.y, n);
}

void divide_and_conquer(vector<Event>& events)
{
    if (events.size() == 1)
        return;

    int mid = events.size() / 2;
    vector<Event> ev1(mid), ev2(events.size() - mid);
    for (int i = 0; i < mid; i++)
        ev1[i] = events[i];
    for (int i = mid; i < events.size(); i++)
        ev2[i - mid] = events[i];

    divide_and_conquer(ev1);
    divide_and_conquer(ev2);

    sort(ev1.begin(), ev1.end(), x_sort);
    sort(ev2.begin(), ev2.end(), x_sort);

    int to_add = 0;
    for (auto& e : ev2) {
        while (to_add < mid && ev1[to_add].x <= e.x) {
            update(ev1[to_add]);
            to_add++;
        }

        query(e);
    }
    while (to_add) {
        to_add--;
        update(ev1[to_add], -1);
    }
}

void make_events(void)
{
    char cur[10];
    for (int t = 1; t <= q; t++) {
        scanf("%s", cur);
        if (cur[0] == 't') {
            int i;
            scanf("%d", &i);
            i--;

            if (bulbs[i]) {
                // Seperate point i from point i + 1
                LitUp prv = *prev(active.upper_bound({i, 0, 0}));
                active.erase(prv);
                int v = t - prv.time_on;

                events.push_back({POINT, t, prv.l, prv.r, v, t});
                active.insert({prv.l, i, t});
                active.insert({i + 1, prv.r, t});
            }
            else {
                // Unite point i and point i + 1
                LitUp p1 = *prev(active.upper_bound({i, 0, 0}));
                LitUp p2 = *prev(active.upper_bound({i + 1, 0, 0}));
                active.erase(p1);
                active.erase(p2);

                events.push_back({POINT, t, p1.l, p1.r, t - p1.time_on, t});
                events.push_back({POINT, t, p2.l, p2.r, t - p2.time_on, t});

                active.insert({p1.l, p2.r, t});
            }

            bulbs[i] = !bulbs[i];
        }
        else {
            int a, b;
            scanf("%d %d", &a, &b);
            a--; b--;
            ans[t]++;

            // Check if the segment is active
            LitUp p1 = *prev(active.upper_bound({a, 0, 0}));
            LitUp p2 = *prev(active.upper_bound({b, 0, 0}));

            if (p1.l == p2.l) {
                ans[t] += t - p1.time_on;
            }

            // Create the query
            events.push_back({END_Q, t, a, b, -1, t});
        }
    }
}

void read_process_input(void)
{
    scanf("%d %d", &n, &q);

    ans.assign(q + 1, -1);
    segments.init(n + 1);
    bulbs.resize(n);

    int latest_point = 0;
    for (int i = 0; i < n; i++) {
        int cond;
        scanf("%1d", &cond);
        bulbs[i] = cond;

        if (!cond) {
            active.insert({latest_point, i, 0});
            latest_point = i + 1;
        }
    }
    active.insert({latest_point, n, 0});

    make_events();

    active.clear();
}

// #define DEBUG
int main(void)
{
    #ifdef DEBUG
        freopen("streetlamps.in", "r", stdin);
    #endif

    read_process_input();
    divide_and_conquer(events);

    for (int i = 1; i <= q; i++) {
        if (ans[i] != -1)
            printf("%d\n", ans[i]);
    }

    return 0;
}

Compilation message

street_lamps.cpp: In function 'void divide_and_conquer(std::vector<Event>&)':
street_lamps.cpp:90:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Event>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |     for (int i = mid; i < events.size(); i++)
      |                       ~~^~~~~~~~~~~~~~~
street_lamps.cpp: In function 'void make_events()':
street_lamps.cpp:118:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  118 |         scanf("%s", cur);
      |         ~~~~~^~~~~~~~~~~
street_lamps.cpp:121:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |             scanf("%d", &i);
      |             ~~~~~^~~~~~~~~~
street_lamps.cpp:151:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  151 |             scanf("%d %d", &a, &b);
      |             ~~~~~^~~~~~~~~~~~~~~~~
street_lamps.cpp: In function 'void read_process_input()':
street_lamps.cpp:171:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  171 |     scanf("%d %d", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~
street_lamps.cpp:180:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  180 |         scanf("%1d", &cond);
      |         ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 296 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 556 ms 31996 KB Output is correct
2 Correct 573 ms 31488 KB Output is correct
3 Correct 664 ms 31944 KB Output is correct
4 Correct 843 ms 37668 KB Output is correct
5 Correct 908 ms 40128 KB Output is correct
6 Correct 878 ms 36616 KB Output is correct
7 Correct 798 ms 39220 KB Output is correct
8 Correct 529 ms 31696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 468 KB Output is correct
2 Correct 2 ms 436 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 1385 ms 50144 KB Output is correct
6 Correct 1258 ms 44708 KB Output is correct
7 Correct 1016 ms 38288 KB Output is correct
8 Correct 573 ms 31692 KB Output is correct
9 Correct 209 ms 21920 KB Output is correct
10 Correct 239 ms 23960 KB Output is correct
11 Correct 229 ms 24080 KB Output is correct
12 Correct 969 ms 39216 KB Output is correct
13 Correct 529 ms 31696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 412 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 750 ms 30352 KB Output is correct
6 Correct 852 ms 34112 KB Output is correct
7 Correct 920 ms 37756 KB Output is correct
8 Correct 958 ms 41412 KB Output is correct
9 Correct 587 ms 33720 KB Output is correct
10 Correct 553 ms 35924 KB Output is correct
11 Correct 586 ms 33724 KB Output is correct
12 Correct 556 ms 35920 KB Output is correct
13 Correct 577 ms 33700 KB Output is correct
14 Correct 554 ms 35888 KB Output is correct
15 Correct 934 ms 39260 KB Output is correct
16 Correct 512 ms 31740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 296 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 296 KB Output is correct
8 Correct 556 ms 31996 KB Output is correct
9 Correct 573 ms 31488 KB Output is correct
10 Correct 664 ms 31944 KB Output is correct
11 Correct 843 ms 37668 KB Output is correct
12 Correct 908 ms 40128 KB Output is correct
13 Correct 878 ms 36616 KB Output is correct
14 Correct 798 ms 39220 KB Output is correct
15 Correct 529 ms 31696 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 2 ms 436 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 1385 ms 50144 KB Output is correct
21 Correct 1258 ms 44708 KB Output is correct
22 Correct 1016 ms 38288 KB Output is correct
23 Correct 573 ms 31692 KB Output is correct
24 Correct 209 ms 21920 KB Output is correct
25 Correct 239 ms 23960 KB Output is correct
26 Correct 229 ms 24080 KB Output is correct
27 Correct 969 ms 39216 KB Output is correct
28 Correct 529 ms 31696 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 2 ms 412 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 750 ms 30352 KB Output is correct
34 Correct 852 ms 34112 KB Output is correct
35 Correct 920 ms 37756 KB Output is correct
36 Correct 958 ms 41412 KB Output is correct
37 Correct 587 ms 33720 KB Output is correct
38 Correct 553 ms 35924 KB Output is correct
39 Correct 586 ms 33724 KB Output is correct
40 Correct 556 ms 35920 KB Output is correct
41 Correct 577 ms 33700 KB Output is correct
42 Correct 554 ms 35888 KB Output is correct
43 Correct 934 ms 39260 KB Output is correct
44 Correct 512 ms 31740 KB Output is correct
45 Correct 267 ms 21124 KB Output is correct
46 Correct 342 ms 21044 KB Output is correct
47 Correct 490 ms 22624 KB Output is correct
48 Correct 872 ms 36156 KB Output is correct
49 Correct 261 ms 26620 KB Output is correct
50 Correct 255 ms 26616 KB Output is correct
51 Correct 251 ms 26860 KB Output is correct
52 Correct 263 ms 27076 KB Output is correct
53 Correct 279 ms 27184 KB Output is correct
54 Correct 258 ms 27200 KB Output is correct