Submission #154971

# Submission time Handle Problem Language Result Execution time Memory
154971 2019-09-25T17:23:33 Z Akashi Street Lamps (APIO19_street_lamps) C++14
100 / 100
2698 ms 287868 KB
#include <bits/stdc++.h>
using namespace std;

int n, q;
char t[25], light[300005];

struct interv{
    int x, y, tmp;

    bool operator < (const interv &aux)const{
        if(x != aux.x) return x < aux.x;
        return y < aux.y;
    }
};
set <interv> s;

struct node{
    int val;
    node *left, *right;

    node(){
        val = 0;
        left = right = NULL;
    }
};

struct data_structure{
    node *bit[300005];

    void update_seg(node *nod, int x, int val, int st = 1, int dr = n){
        nod->val += val;
        if(st == dr) return ;

        int mij = (st + dr) / 2;
        if(x <= mij){
            if(nod->left == NULL) nod->left = new node;
            update_seg(nod->left, x, val, st, mij);
        }
        else{
            if(nod->right == NULL) nod->right = new node;
            update_seg(nod->right, x, val, mij + 1, dr);
        }
    }

    void update(int x, int y, int val){
        for(int i = x; i <= n ; i = i + (i & (-i)))
            update_seg(bit[i], y, val);
    }

    int query_seg(node *nod, int x, int y, int st = 1, int dr = n){
        if(x <= st && dr <= y) return nod->val;

        int mij = (st + dr) / 2;
        int a1 = 0, a2 = 0;
        if(x <= mij){
            if(nod->left != NULL) a1 = query_seg(nod->left, x, y, st, mij);
        }
        if(mij + 1 <= y){
            if(nod->right != NULL) a2 = query_seg(nod->right, x, y, mij + 1, dr);
        }

        return a1 + a2;
    }

    int query(int x, int y){
        int Sum = 0;
        for(int i = x; i > 0 ; i = i - (i & (-i)))
            Sum = Sum + query_seg(bit[i], y, n);
        return Sum;
    }

    void build(){
        for(int i = 1; i <= n ; ++i)
            bit[i] = new node;
    }
};
data_structure A;

void elim(int x, int tmp){
    set <interv> :: iterator it = s.lower_bound({x + 1, 0, 0});
    --it;

    A.update(it->x, it->y, tmp - it->tmp);

    if(x - 1 >= it->x) s.insert({it->x, x - 1, tmp});
    if(x + 1 <= it->y) s.insert({x + 1, it->y, tmp});

    s.erase(it);
}

void add(int x, int tmp){
    if(s.empty()){
        s.insert({x, x, tmp});
        return ;
    }

    set <interv> :: iterator it = s.lower_bound({x + 1, 0, 0});

    if(it != s.begin() && it != s.end()){
        set <interv> :: iterator it2 = it;
        --it2;

        if(it2->y == x - 1 && it->x == x + 1){
            A.update(it->x, it->y, tmp - it->tmp);
            A.update(it2->x, it2->y, tmp - it2->tmp);

            s.insert({it2->x, it->y, tmp});

            s.erase(it);
            s.erase(it2);
            return ;
        }
    }

    set <interv> :: iterator it2 = it;
    if(it2 != s.begin()) --it2;
    if(it == s.end()) --it;

    if(it2->y == x - 1){
        A.update(it2->x, it2->y, tmp - it2->tmp);

        s.insert({it2->x, x, tmp});
        s.erase(it2);
        return ;
    }

    if(it->x == x + 1){
        A.update(it->x, it->y, tmp - it->tmp);

        s.insert({x, it->y, tmp});
        s.erase(it);
        return ;
    }

    s.insert({x, x, tmp});
}

int main(){

    scanf("%d%d", &n, &q);
    scanf("%s", light + 1);

    for(int i = 1; i <= n ; ++i){
        if(light[i] == '0') continue ;
        int j = i;
        while(light[j + 1] == '1' && j + 1 <= n) ++j;

        s.insert({i, j, 0});
        i = j;
    }

    A.build();
    int x, y;
    for(int tmp = 1; tmp <= q ; ++tmp){
        scanf("%s", t);

        if(t[0] == 'q'){
            scanf("%d%d", &x, &y);
            --y;
            int v = A.query(x, y);

            set <interv> :: iterator it = s.lower_bound({x + 1, 0, 0});

            if(it != s.begin()){
                --it;
                if(it->x <= x && y <= it->y) v = v + (tmp - it->tmp);
            }

            printf("%d\n", v);
        }
        else{
            scanf("%d", &x);
            if(x == 3){
                bool ok = 0;

            }

            if(light[x] == '1') light[x] = '0', elim(x, tmp);
            else light[x] = '1', add(x, tmp);
        }
    }


    return 0;
}






Compilation message

street_lamps.cpp: In function 'int main()':
street_lamps.cpp:174:22: warning: unused variable 'ok' [-Wunused-variable]
                 bool ok = 0;
                      ^~
street_lamps.cpp:140:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &q);
     ~~~~~^~~~~~~~~~~~~~~~
street_lamps.cpp:141:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s", light + 1);
     ~~~~~^~~~~~~~~~~~~~~~~
street_lamps.cpp:155:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%s", t);
         ~~~~~^~~~~~~~~
street_lamps.cpp:158:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d%d", &x, &y);
             ~~~~~^~~~~~~~~~~~~~~~
street_lamps.cpp:172:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d", &x);
             ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 197 ms 4536 KB Output is correct
2 Correct 244 ms 5272 KB Output is correct
3 Correct 517 ms 11284 KB Output is correct
4 Correct 2050 ms 200312 KB Output is correct
5 Correct 2322 ms 224820 KB Output is correct
6 Correct 2272 ms 215892 KB Output is correct
7 Correct 232 ms 18936 KB Output is correct
8 Correct 237 ms 20384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 888 KB Output is correct
2 Correct 4 ms 888 KB Output is correct
3 Correct 4 ms 760 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 2698 ms 287868 KB Output is correct
6 Correct 2519 ms 274204 KB Output is correct
7 Correct 1854 ms 224292 KB Output is correct
8 Correct 235 ms 20308 KB Output is correct
9 Correct 105 ms 3960 KB Output is correct
10 Correct 113 ms 4344 KB Output is correct
11 Correct 115 ms 4480 KB Output is correct
12 Correct 225 ms 18936 KB Output is correct
13 Correct 289 ms 20276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 632 KB Output is correct
3 Correct 4 ms 760 KB Output is correct
4 Correct 4 ms 764 KB Output is correct
5 Correct 514 ms 33580 KB Output is correct
6 Correct 1198 ms 157920 KB Output is correct
7 Correct 1805 ms 215872 KB Output is correct
8 Correct 2674 ms 261572 KB Output is correct
9 Correct 232 ms 4344 KB Output is correct
10 Correct 191 ms 3448 KB Output is correct
11 Correct 231 ms 4436 KB Output is correct
12 Correct 190 ms 3320 KB Output is correct
13 Correct 231 ms 4420 KB Output is correct
14 Correct 194 ms 3460 KB Output is correct
15 Correct 231 ms 18936 KB Output is correct
16 Correct 240 ms 20408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 197 ms 4536 KB Output is correct
9 Correct 244 ms 5272 KB Output is correct
10 Correct 517 ms 11284 KB Output is correct
11 Correct 2050 ms 200312 KB Output is correct
12 Correct 2322 ms 224820 KB Output is correct
13 Correct 2272 ms 215892 KB Output is correct
14 Correct 232 ms 18936 KB Output is correct
15 Correct 237 ms 20384 KB Output is correct
16 Correct 4 ms 888 KB Output is correct
17 Correct 4 ms 888 KB Output is correct
18 Correct 4 ms 760 KB Output is correct
19 Correct 3 ms 376 KB Output is correct
20 Correct 2698 ms 287868 KB Output is correct
21 Correct 2519 ms 274204 KB Output is correct
22 Correct 1854 ms 224292 KB Output is correct
23 Correct 235 ms 20308 KB Output is correct
24 Correct 105 ms 3960 KB Output is correct
25 Correct 113 ms 4344 KB Output is correct
26 Correct 115 ms 4480 KB Output is correct
27 Correct 225 ms 18936 KB Output is correct
28 Correct 289 ms 20276 KB Output is correct
29 Correct 3 ms 376 KB Output is correct
30 Correct 3 ms 632 KB Output is correct
31 Correct 4 ms 760 KB Output is correct
32 Correct 4 ms 764 KB Output is correct
33 Correct 514 ms 33580 KB Output is correct
34 Correct 1198 ms 157920 KB Output is correct
35 Correct 1805 ms 215872 KB Output is correct
36 Correct 2674 ms 261572 KB Output is correct
37 Correct 232 ms 4344 KB Output is correct
38 Correct 191 ms 3448 KB Output is correct
39 Correct 231 ms 4436 KB Output is correct
40 Correct 190 ms 3320 KB Output is correct
41 Correct 231 ms 4420 KB Output is correct
42 Correct 194 ms 3460 KB Output is correct
43 Correct 231 ms 18936 KB Output is correct
44 Correct 240 ms 20408 KB Output is correct
45 Correct 87 ms 2804 KB Output is correct
46 Correct 123 ms 2876 KB Output is correct
47 Correct 840 ms 81924 KB Output is correct
48 Correct 1772 ms 199764 KB Output is correct
49 Correct 123 ms 4464 KB Output is correct
50 Correct 123 ms 4356 KB Output is correct
51 Correct 127 ms 4592 KB Output is correct
52 Correct 125 ms 4912 KB Output is correct
53 Correct 124 ms 4948 KB Output is correct
54 Correct 124 ms 4872 KB Output is correct