Submission #268302

# Submission time Handle Problem Language Result Execution time Memory
268302 2020-08-16T11:04:51 Z evpipis Street Lamps (APIO19_street_lamps) C++11
100 / 100
3796 ms 408720 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef pair<int, int> ii;

const int len = 3e5+5;
set<ii> mys;
int light[len]; // n_len

struct seg_tree{
    struct node{
        int sum, num;
        node *lef, *rig;

        node(int s = 0, int n = 0, node *l = NULL, node *r = NULL){
            sum = s;
            num = n;
            lef = l;
            rig = r;
        }
    };

    typedef node *pnode;
    pnode null, root;
    int mx;

    seg_tree(int m = 0){
        mx = m;
        null = new node();
        null->lef = null->rig = null;
        root = null;
    }

    pnode update(pnode t, int l, int r, int i, int s, int n){
        if (t == null)
            t = new node(0, 0, null, null);
        t->sum += s;
        t->num += n;

        if (l == r)
            return t;

        int mid = (l+r)/2;
        if (i <= mid)
            t->lef = update(t->lef, l, mid, i, s, n);
        else
            t->rig = update(t->rig, mid+1, r, i, s, n);
        return t;
    }

    void upd(int y, int sum, int num){
        root = update(root, 1, mx, y, sum, num);
    }

    ii query(pnode t, int l, int r, int i, int j){
        if (r < i || j < l)
            return mp(0, 0);
        if (i <= l && r <= j)
            return mp(t->sum, t->num);

        int mid = (l+r)/2;
        ii lef = query(t->lef, l, mid, i, j);
        ii rig = query(t->rig, mid+1, r, i, j);
        return mp(lef.fi+rig.fi, lef.se+rig.se);
    }

    ii ask(int y){
        return query(root, 1, mx, y, mx);
    }
};

struct bit{
    seg_tree tree[len];
    int mx;

    bit(int r = 0, int c = 0){
        mx = r;

        for (int i = 1; i <= r; i++)
            tree[i] = seg_tree(c);
    }

    void upd(int x, int y, int sum, int num){
        while (x <= mx)
            tree[x].upd(y, sum, num), x += x&(-x);
    }

    int ask(int x, int y, int tim){
        int sum = 0, num = 0;
        while (x > 0){
            sum += tree[x].ask(y).fi;
            num += tree[x].ask(y).se;
            x -= x&(-x);
        }

        return sum + num*tim;
    }
};

bit data;

ii rig(int i){
    auto it = mys.lower_bound(mp(i, 0));
    if (it == mys.end())
        return mp(-1, -1);
    return *it;
}

ii lef(int i){
    auto it = mys.lower_bound(mp(i+1, 0));
    if (it == mys.begin())
        return mp(-1, -1);
    return *(--it);
}

void print(){
    printf("current intervals:");
    for (auto it: mys)
        printf(" (%d, %d)", it.fi, it.se);
    printf("\n");
}

void toggle(int i, int t = 0){
    light[i] ^= 1;

    if (light[i] == 1){
        ii cur = mp(i, i), l = lef(i-1), r = rig(i+1);
        if (l.se+1 == cur.fi){
            // remove l and update data and extend cur
            mys.erase(l);
            data.upd(l.fi, l.se, +t, -1);
            cur.fi = l.fi;
        }
        if (cur.se == r.fi-1){
            // remove r and update data and extend cur
            mys.erase(r);
            data.upd(r.fi, r.se, +t, -1);
            cur.se = r.se;
        }
        // insert cur and update data
        mys.insert(cur);
        data.upd(cur.fi, cur.se, -t, +1);
    }
    else{
        ii cur = lef(i), l = mp(cur.fi, i-1), r = mp(i+1, cur.se);
        // remove cur and update data
        mys.erase(cur);
        data.upd(cur.fi, cur.se, +t, -1);
        if (l.fi <= l.se){
            // insert l and update data
            mys.insert(l);
            data.upd(l.fi, l.se, -t, +1);
        }
        if (r.fi <= r.se){
            // insert r and update data
            mys.insert(r);
            data.upd(r.fi, r.se, -t, +1);
        }
    }

    //print();
}

int main(){
    int n, q;
    scanf("%d %d ", &n, &q);

    data = bit(n, n);

    for (int i = 1; i <= n; i++){
        char temp;
        scanf("%c", &temp);
        if (temp == '1')
            toggle(i);
    }

    for (int i = 1; i <= q; i++){
        char str[15];
        scanf("%s", str);
        if (str[0] == 'q'){
            int a, b;
            scanf("%d %d", &a, &b);
            b--;

            printf("%d\n", data.ask(a, b, i));
            //printf("asked for interval (%d, %d)\n", a, b);
        }
        else{
            int pos;
            scanf("%d", &pos);
            toggle(pos, i);
        }

        //printf("%d) ", i);
        //print();
    }
    return 0;
}

Compilation message

street_lamps.cpp: In function 'int main()':
street_lamps.cpp:170:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  170 |     scanf("%d %d ", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~
street_lamps.cpp:176:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  176 |         scanf("%c", &temp);
      |         ~~~~~^~~~~~~~~~~~~
street_lamps.cpp:183:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  183 |         scanf("%s", str);
      |         ~~~~~^~~~~~~~~~~
street_lamps.cpp:186:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  186 |             scanf("%d %d", &a, &b);
      |             ~~~~~^~~~~~~~~~~~~~~~~
street_lamps.cpp:194:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  194 |             scanf("%d", &pos);
      |             ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 42 ms 33144 KB Output is correct
2 Correct 33 ms 33152 KB Output is correct
3 Correct 38 ms 33144 KB Output is correct
4 Correct 37 ms 33272 KB Output is correct
5 Correct 33 ms 33272 KB Output is correct
6 Correct 39 ms 33144 KB Output is correct
7 Correct 35 ms 33272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 279 ms 37468 KB Output is correct
2 Correct 359 ms 38004 KB Output is correct
3 Correct 720 ms 44152 KB Output is correct
4 Correct 2964 ms 305400 KB Output is correct
5 Correct 3484 ms 317688 KB Output is correct
6 Correct 3093 ms 313976 KB Output is correct
7 Correct 1159 ms 49144 KB Output is correct
8 Correct 3713 ms 408668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 33784 KB Output is correct
2 Correct 44 ms 33784 KB Output is correct
3 Correct 37 ms 33784 KB Output is correct
4 Correct 43 ms 33912 KB Output is correct
5 Correct 3035 ms 327092 KB Output is correct
6 Correct 2990 ms 326244 KB Output is correct
7 Correct 3128 ms 317532 KB Output is correct
8 Correct 3699 ms 408720 KB Output is correct
9 Correct 173 ms 36856 KB Output is correct
10 Correct 213 ms 37240 KB Output is correct
11 Correct 190 ms 37496 KB Output is correct
12 Correct 1138 ms 49288 KB Output is correct
13 Correct 3796 ms 408604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 33528 KB Output is correct
2 Correct 41 ms 33576 KB Output is correct
3 Correct 44 ms 33692 KB Output is correct
4 Correct 48 ms 33760 KB Output is correct
5 Correct 1960 ms 250760 KB Output is correct
6 Correct 2389 ms 286472 KB Output is correct
7 Correct 2828 ms 313596 KB Output is correct
8 Correct 3549 ms 340264 KB Output is correct
9 Correct 430 ms 37240 KB Output is correct
10 Correct 327 ms 36216 KB Output is correct
11 Correct 396 ms 37192 KB Output is correct
12 Correct 341 ms 36216 KB Output is correct
13 Correct 411 ms 37372 KB Output is correct
14 Correct 325 ms 36296 KB Output is correct
15 Correct 1061 ms 49244 KB Output is correct
16 Correct 3597 ms 408696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 33144 KB Output is correct
2 Correct 33 ms 33152 KB Output is correct
3 Correct 38 ms 33144 KB Output is correct
4 Correct 37 ms 33272 KB Output is correct
5 Correct 33 ms 33272 KB Output is correct
6 Correct 39 ms 33144 KB Output is correct
7 Correct 35 ms 33272 KB Output is correct
8 Correct 279 ms 37468 KB Output is correct
9 Correct 359 ms 38004 KB Output is correct
10 Correct 720 ms 44152 KB Output is correct
11 Correct 2964 ms 305400 KB Output is correct
12 Correct 3484 ms 317688 KB Output is correct
13 Correct 3093 ms 313976 KB Output is correct
14 Correct 1159 ms 49144 KB Output is correct
15 Correct 3713 ms 408668 KB Output is correct
16 Correct 40 ms 33784 KB Output is correct
17 Correct 44 ms 33784 KB Output is correct
18 Correct 37 ms 33784 KB Output is correct
19 Correct 43 ms 33912 KB Output is correct
20 Correct 3035 ms 327092 KB Output is correct
21 Correct 2990 ms 326244 KB Output is correct
22 Correct 3128 ms 317532 KB Output is correct
23 Correct 3699 ms 408720 KB Output is correct
24 Correct 173 ms 36856 KB Output is correct
25 Correct 213 ms 37240 KB Output is correct
26 Correct 190 ms 37496 KB Output is correct
27 Correct 1138 ms 49288 KB Output is correct
28 Correct 3796 ms 408604 KB Output is correct
29 Correct 43 ms 33528 KB Output is correct
30 Correct 41 ms 33576 KB Output is correct
31 Correct 44 ms 33692 KB Output is correct
32 Correct 48 ms 33760 KB Output is correct
33 Correct 1960 ms 250760 KB Output is correct
34 Correct 2389 ms 286472 KB Output is correct
35 Correct 2828 ms 313596 KB Output is correct
36 Correct 3549 ms 340264 KB Output is correct
37 Correct 430 ms 37240 KB Output is correct
38 Correct 327 ms 36216 KB Output is correct
39 Correct 396 ms 37192 KB Output is correct
40 Correct 341 ms 36216 KB Output is correct
41 Correct 411 ms 37372 KB Output is correct
42 Correct 325 ms 36296 KB Output is correct
43 Correct 1061 ms 49244 KB Output is correct
44 Correct 3597 ms 408696 KB Output is correct
45 Correct 154 ms 35576 KB Output is correct
46 Correct 195 ms 35736 KB Output is correct
47 Correct 1279 ms 128596 KB Output is correct
48 Correct 3205 ms 305500 KB Output is correct
49 Correct 235 ms 37300 KB Output is correct
50 Correct 198 ms 37368 KB Output is correct
51 Correct 221 ms 37456 KB Output is correct
52 Correct 273 ms 37880 KB Output is correct
53 Correct 249 ms 37908 KB Output is correct
54 Correct 305 ms 38008 KB Output is correct