Submission #532134

# Submission time Handle Problem Language Result Execution time Memory
532134 2022-03-02T06:55:08 Z 79brue Street Lamps (APIO19_street_lamps) C++14
100 / 100
2242 ms 84444 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct Interval {
    int l, r, s;
    Interval(){}
    Interval(int l, int r, int s): l(l), r(r), s(s){}

    bool operator<(const Interval &r)const{
        return l<r.l;
    }
};

struct Rect{
    int xl, xr, w, idx;
    Rect(){}
    Rect(int xl, int xr, int w, int idx): xl(xl), xr(xr), w(w), idx(idx){}
};

struct Query {
    int type;
    int x, y, w, idx, t;
    Query(){}
    Query(int type, int x, int y, int w, int idx, int t): type(type), x(x), y(y), w(w), idx(idx), t(t){}

    bool operator<(const Query &r)const{
        if(t != r.t) return t<r.t;
        return type < r.type;
    }
};

int n, q;
int arr[300005];
int qt[300005], qx[300005], qy[300005];
set<Interval> interval;
int ans[300005];

vector<Rect> rectangles;
vector<Query> queries;

void makeIntervals(){
    set<int> off;
    off.insert(0), off.insert(n+1);
    for(int i=1; i<=n; i++){
        if(!arr[i]) off.insert(i);
    }

    for(int i=1; i<=n; i++){
        if(!arr[i]) continue;
        int j = i;
        while(j<n && arr[j+1]) j++;
        interval.insert(Interval(i, j, 0));
        i=j;
    }

    for(int i=1; i<=q; i++){
        if(qt[i] == 0){
            if(!arr[qx[i]]){ /// OFF -> ON
                auto it = off.lower_bound(qx[i]);
                int xl = *prev(it), xr = *next(it);
                off.erase(it);
                arr[qx[i]] = 1;
                if(xl != qx[i]-1){
                    rectangles.push_back(Rect(xl+1, qx[i],
                                          interval.lower_bound(Interval(xl+1, qx[i], 0))->s, i));
                    interval.erase(interval.lower_bound(Interval(xl+1, qx[i], 0)));
                }
                if(xr != qx[i]+1){
                    rectangles.push_back(Rect(qx[i]+1, xr,
                                              interval.lower_bound(Interval(qx[i]+1, xr, 0))->s, i));
                    interval.erase(interval.lower_bound(Interval(qx[i]+1, xr, 0)));
                }

                interval.insert(Interval(xl+1, xr, i));
            }
            else{ /// ON -> OFF
                auto it = off.lower_bound(qx[i]); /// qx[i]는 현재 켜져 있음
                int xl = *prev(it), xr = *it;
                off.insert(qx[i]);
                arr[qx[i]] = 0;

                rectangles.push_back(Rect(xl+1, xr, interval.lower_bound(Interval(xl+1, xr, 0))->s, i));
                interval.erase(interval.lower_bound(Interval(xl+1, xr, 0)));

                if(xl != qx[i]-1){
                    interval.insert(Interval(xl+1, qx[i], i));
                }
                if(xr != qx[i]+1){
                    interval.insert(Interval(qx[i]+1, xr, i));
                }
            }
        }
        else{
            auto toff = off.lower_bound(qx[i]);
            if(*toff < qy[i]) continue;
            auto it = interval.lower_bound(Interval(*prev(toff)+1, *toff-1, 0));
            ans[i] += i - it->s;
        }
    }
}


struct QueryRect {
    int qt; /// 1: 더하기, 2: 답 계산
    int x, y, w, idx;
    QueryRect(){}
    QueryRect(int qt, int x, int y, int w, int idx): qt(qt), x(x), y(y), w(w), idx(idx){}

    bool operator<(const QueryRect &r)const{
        if(y != r.y) return y<r.y;
        return qt<r.qt;
    }
};

struct Fenwick {
    const int n = 300005;
    int tree[300010];

    void add(int x, int y){
        x++;
        while(x<=n){
            tree[x] += y;
            x += x&-x;
        }
    }

    int sum(int x){
        x++;
        int ret = 0;
        while(x){
            ret += tree[x];
            x -= x&-x;
        }
        return ret;
    }
} tree;

void processQueries(int l, int r){
    if(l==r) return;
    int m = (l+r)>>1;
    processQueries(l, m);
    processQueries(m+1, r);

    vector<QueryRect> vec;
    for(int i=l; i<=m; i++){
        if(queries[i].type == 1){
            vec.push_back(QueryRect(1, queries[i].x, queries[i].y, queries[i].w, queries[i].idx));
        }
    }
    for(int i=m+1; i<=r; i++){
        if(queries[i].type == 2){
            vec.push_back(QueryRect(2, queries[i].x, queries[i].y, queries[i].w, queries[i].idx));
        }
    }
    sort(vec.begin(), vec.end());

    for(QueryRect qrect: vec){
        if(qrect.qt == 1){ /// 덧셈 쿼리
            tree.add(qrect.x, qrect.w);
        }
        else{
            ans[qrect.idx] += tree.sum(qrect.x);
        }
    }
    for(QueryRect qrect: vec){
        if(qrect.qt == 1) tree.add(qrect.x, -qrect.w);
    }
}

int main(){
    scanf("%d %d", &n, &q);
    for(int i=1; i<=n; i++) scanf("%1d", &arr[i]);
    for(int i=1; i<=q; i++){
        string str;
        cin >> str;
        if(str[0] == 't'){
            qt[i] = 0;
            scanf("%d", &qx[i]);
        }
        else{
            qt[i] = 1;
            scanf("%d %d", &qx[i], &qy[i]);
            queries.push_back(Query(2, qx[i], qy[i], 1, i, i));
        }
    }

    makeIntervals();
    for(Rect rect: rectangles){
        queries.push_back(Query(1, rect.xr+1, rect.xr+1, rect.idx-rect.w, 0, rect.idx));
        queries.push_back(Query(1, rect.xl, rect.xr+1, -(rect.idx-rect.w), 0, rect.idx));
        queries.push_back(Query(1, rect.xr+1, rect.xl, -(rect.idx-rect.w), 0, rect.idx));
        queries.push_back(Query(1, rect.xl, rect.xl, rect.idx-rect.w, 0, rect.idx));
    }
    sort(queries.begin(), queries.end());

//    for(Query query: queries){
//        printf("%d %d %d %d %d %d\n", query.type, query.x, query.y, query.w, query.idx, query.t);
//    }
    processQueries(0, (int)queries.size()-1);

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

Compilation message

street_lamps.cpp: In function 'int main()':
street_lamps.cpp:174:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  174 |     scanf("%d %d", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~
street_lamps.cpp:175:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  175 |     for(int i=1; i<=n; i++) scanf("%1d", &arr[i]);
      |                             ~~~~~^~~~~~~~~~~~~~~~
street_lamps.cpp:181:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  181 |             scanf("%d", &qx[i]);
      |             ~~~~~^~~~~~~~~~~~~~
street_lamps.cpp:185:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  185 |             scanf("%d %d", &qx[i], &qy[i]);
      |             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1151 ms 44264 KB Output is correct
2 Correct 1116 ms 47076 KB Output is correct
3 Correct 1218 ms 47728 KB Output is correct
4 Correct 1511 ms 57300 KB Output is correct
5 Correct 1677 ms 65592 KB Output is correct
6 Correct 1725 ms 73744 KB Output is correct
7 Correct 743 ms 33828 KB Output is correct
8 Correct 460 ms 29188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 588 KB Output is correct
2 Correct 4 ms 588 KB Output is correct
3 Correct 4 ms 584 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 2242 ms 84444 KB Output is correct
6 Correct 2101 ms 81252 KB Output is correct
7 Correct 1559 ms 57952 KB Output is correct
8 Correct 464 ms 24068 KB Output is correct
9 Correct 285 ms 16432 KB Output is correct
10 Correct 302 ms 20500 KB Output is correct
11 Correct 312 ms 20192 KB Output is correct
12 Correct 650 ms 28696 KB Output is correct
13 Correct 454 ms 24072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 460 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
3 Correct 4 ms 584 KB Output is correct
4 Correct 6 ms 588 KB Output is correct
5 Correct 669 ms 38708 KB Output is correct
6 Correct 1086 ms 55872 KB Output is correct
7 Correct 1514 ms 73688 KB Output is correct
8 Correct 2208 ms 83652 KB Output is correct
9 Correct 1409 ms 64076 KB Output is correct
10 Correct 1893 ms 70092 KB Output is correct
11 Correct 1415 ms 64064 KB Output is correct
12 Correct 1857 ms 70080 KB Output is correct
13 Correct 1451 ms 64084 KB Output is correct
14 Correct 1911 ms 70196 KB Output is correct
15 Correct 661 ms 33864 KB Output is correct
16 Correct 455 ms 29288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 316 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1151 ms 44264 KB Output is correct
9 Correct 1116 ms 47076 KB Output is correct
10 Correct 1218 ms 47728 KB Output is correct
11 Correct 1511 ms 57300 KB Output is correct
12 Correct 1677 ms 65592 KB Output is correct
13 Correct 1725 ms 73744 KB Output is correct
14 Correct 743 ms 33828 KB Output is correct
15 Correct 460 ms 29188 KB Output is correct
16 Correct 4 ms 588 KB Output is correct
17 Correct 4 ms 588 KB Output is correct
18 Correct 4 ms 584 KB Output is correct
19 Correct 2 ms 332 KB Output is correct
20 Correct 2242 ms 84444 KB Output is correct
21 Correct 2101 ms 81252 KB Output is correct
22 Correct 1559 ms 57952 KB Output is correct
23 Correct 464 ms 24068 KB Output is correct
24 Correct 285 ms 16432 KB Output is correct
25 Correct 302 ms 20500 KB Output is correct
26 Correct 312 ms 20192 KB Output is correct
27 Correct 650 ms 28696 KB Output is correct
28 Correct 454 ms 24072 KB Output is correct
29 Correct 2 ms 460 KB Output is correct
30 Correct 3 ms 460 KB Output is correct
31 Correct 4 ms 584 KB Output is correct
32 Correct 6 ms 588 KB Output is correct
33 Correct 669 ms 38708 KB Output is correct
34 Correct 1086 ms 55872 KB Output is correct
35 Correct 1514 ms 73688 KB Output is correct
36 Correct 2208 ms 83652 KB Output is correct
37 Correct 1409 ms 64076 KB Output is correct
38 Correct 1893 ms 70092 KB Output is correct
39 Correct 1415 ms 64064 KB Output is correct
40 Correct 1857 ms 70080 KB Output is correct
41 Correct 1451 ms 64084 KB Output is correct
42 Correct 1911 ms 70196 KB Output is correct
43 Correct 661 ms 33864 KB Output is correct
44 Correct 455 ms 29288 KB Output is correct
45 Correct 730 ms 28432 KB Output is correct
46 Correct 754 ms 29196 KB Output is correct
47 Correct 809 ms 33396 KB Output is correct
48 Correct 1458 ms 59508 KB Output is correct
49 Correct 320 ms 24216 KB Output is correct
50 Correct 361 ms 24184 KB Output is correct
51 Correct 340 ms 24436 KB Output is correct
52 Correct 321 ms 24692 KB Output is correct
53 Correct 331 ms 24656 KB Output is correct
54 Correct 330 ms 24692 KB Output is correct