답안 #681258

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
681258 2023-01-12T15:38:36 Z qwerasdfzxcl 가로등 (APIO19_street_lamps) C++17
100 / 100
2488 ms 45384 KB
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
struct Seg1{
    int tree[1201200], sz;
    void init(int i, int l, int r, char s[]){
        if (i==1) sz = r;
        if (l==r){tree[i] = s[l]-'0'; return;}

        int m = (l+r)>>1;
        init(i<<1, l, m, s); init(i<<1|1, m+1, r, s);
        tree[i] = tree[i<<1] + tree[i<<1|1];
    }
    void update(int i, int l, int r, int p){
        if (r<p || p<l) return;
        if (l==r) {tree[i] ^= 1; return;}
        int m = (l+r)>>1;
        update(i<<1, l, m, p); update(i<<1|1, m+1, r, p);
        tree[i] = tree[i<<1] + tree[i<<1|1];
    }
    int query(int i, int l, int r, int p){
        if (r<p || p<l) return 0;
        if (l==r) return tree[i];
        int m = (l+r)>>1;
        return query(i<<1, l, m, p) + query(i<<1|1, m+1, r, p);
    }

    pair<int, int> _search(int i, int l, int r, int p, int x){
        if (tree[i]==(r-l+1) * x) return {l, r};
        if (l==r) return {-1, -1};

        int m = (l+r)>>1;
        if (r<p || (m+1 <= p && p <= r)){
            auto ret = _search(i<<1|1, m+1, r, p, x);
            if (ret.first != m+1) return ret;
            auto ret2 = _search(i<<1, l, m, p, x);

            if (ret2.second==-1) return ret;
            return {ret2.first, ret.second};
        }
        else{
            auto ret = _search(i<<1, l, m, p, x);
            if (ret.second != m) return ret;
            auto ret2 = _search(i<<1|1, m+1, r, p, x);

            if (ret2.first==-1) return ret;
            return {ret.first, ret2.second};
        }
    }

    pair<pair<int, int>, int> calc(int p, bool no = 0){
        int x = query(1, 1, sz, p);
        if (x==0) update(1, 1, sz, p);

        auto ret = _search(1, 1, sz, p, 1);
        if ((x==0) == no) update(1, 1, sz, p);
        return {ret, x};
    }
    void toggle(int p){update(1, 1, sz, p);}
}tree1;

struct Seg2{
    ll tree[600600], sz;
    vector<int> st;

    void init(int n){
        sz = n;
        for (auto &x:st) tree[x] = 0;
        st.clear();
    }

    void update(int l, int r, int x){
        ++r;
        //printf(" update: %d %d %d\n", l, r, x);
        for (l+=sz, r+=sz;l<r;l>>=1, r>>=1){
            if (l&1){
                st.push_back(l);
                tree[l] += x;
                l++;
            }
            if (r&1){
                --r;
                st.push_back(r);
                tree[r] += x;
            }
        }
    }
    ll query(int p){
        //printf(" query: %d\n", p);
        ll ret = 0;
        for (p+=sz;p;p>>=1) ret += tree[p];
        return ret;
    }
}tree2;

char buf[1010];
struct Query{
    int a, b, i, typ;

    Query(){}

    void input(int _i){
        i = _i;
        scanf("%s", buf);
        if (buf[0]=='q'){
            scanf("%d %d", &a, &b);
            typ = 1;
            --b;
        }
        else{
            scanf("%d", &a);
            b = -1;
            typ = 0;
        }
    }
}query[300300];

struct Query2{
    int x, y1, y2, z, typ;
    Query2(){}
    Query2(int _x, int _y1, int _y2, int _z, int _typ): x(_x), y1(_y1), y2(_y2), z(_z), typ(_typ) {}
    bool operator < (const Query2 &Q) const{
        if (x==Q.x) return typ < Q.typ;
        return x < Q.x;
    }
};
char s[300300];
int n, q;
ll ans[300300];

void process(const vector<Query> &T, const vector<Query> &Q){
    vector<Query2> V;
    for (auto &[a, b, i, typ]:T){
        assert(typ==0);
        auto [p, x] = tree1.calc(a);
        auto [l, r] = p;
        //printf("%d %d %d\n", l, r, x);

        assert(l!=-1);
        int add = -(x?(q-i):(i-q));
        V.emplace_back(l, a, r, add, 0);
        V.emplace_back(a+1, a, r, -add, 0);
    }

    for (auto &[a, b, i, typ]:Q){
        assert(typ==1);
        V.emplace_back(a, b, -1, i, 1);
    }

    sort(V.begin(), V.end());
    tree2.init(n+3);
    for (auto &[x, y1, y2, z, typ]:V){
        if (typ==0) tree2.update(y1, y2, z);
        else ans[z] += tree2.query(y1);

        //if (typ==1) printf("%d -> add %lld\n", z, tree2.query(y1));
    }
}

void dnc(int l, int r){
    if (l==r){
        if (query[l].typ==0) return;
        auto [p, x] = tree1.calc(query[l].a, 1);
        if (p.second >= query[l].b && x==1){
            ans[query[l].i] -= q-query[l].i;
        }

        return;
    }

    int m = (l+r)>>1;
    dnc(l, m);

    vector<Query> T, Q;
    for (int i=l;i<=m;i++) if (query[i].typ==0) T.push_back(query[i]);
    for (int i=m+1;i<=r;i++) if (query[i].typ==1) Q.push_back(query[i]);
    process(T, Q);

    dnc(m+1, r);

    for (auto &[a, b, i, typ]:T) tree1.toggle(a);
}

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

    tree1.init(1, 1, n, s);

    for (int i=1;i<=q;i++){
        query[i].input(i);
        if (query[i].typ==1){
            auto [p, x] = tree1.calc(query[i].a, 1);

            if (p.second >= query[i].b && x==1) ans[i] += q;
        }
    }

    dnc(1, q);
    for (int i=1;i<=q;i++) if (query[i].typ==1) printf("%lld\n", ans[i]);
    return 0;
}

Compilation message

street_lamps.cpp: In function 'int main()':
street_lamps.cpp:186:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  186 |     scanf("%d %d", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~
street_lamps.cpp:187:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  187 |     scanf("%s", s+1);
      |     ~~~~~^~~~~~~~~~~
street_lamps.cpp: In member function 'void Query::input(int)':
street_lamps.cpp:105:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |         scanf("%s", buf);
      |         ~~~~~^~~~~~~~~~~
street_lamps.cpp:107:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |             scanf("%d %d", &a, &b);
      |             ~~~~~^~~~~~~~~~~~~~~~~
street_lamps.cpp:112:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  112 |             scanf("%d", &a);
      |             ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 312 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 874 ms 22436 KB Output is correct
2 Correct 1000 ms 24012 KB Output is correct
3 Correct 1254 ms 24848 KB Output is correct
4 Correct 1662 ms 34680 KB Output is correct
5 Correct 1584 ms 34804 KB Output is correct
6 Correct 1897 ms 45384 KB Output is correct
7 Correct 819 ms 26704 KB Output is correct
8 Correct 499 ms 27656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 340 KB Output is correct
2 Correct 3 ms 464 KB Output is correct
3 Correct 3 ms 460 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 2488 ms 42688 KB Output is correct
6 Correct 2032 ms 35912 KB Output is correct
7 Correct 1538 ms 34976 KB Output is correct
8 Correct 488 ms 27748 KB Output is correct
9 Correct 225 ms 14736 KB Output is correct
10 Correct 269 ms 19448 KB Output is correct
11 Correct 250 ms 19380 KB Output is correct
12 Correct 796 ms 26648 KB Output is correct
13 Correct 489 ms 27700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 3 ms 504 KB Output is correct
3 Correct 3 ms 468 KB Output is correct
4 Correct 3 ms 340 KB Output is correct
5 Correct 770 ms 30584 KB Output is correct
6 Correct 1328 ms 44816 KB Output is correct
7 Correct 1928 ms 45380 KB Output is correct
8 Correct 2411 ms 40136 KB Output is correct
9 Correct 1210 ms 33848 KB Output is correct
10 Correct 1168 ms 30092 KB Output is correct
11 Correct 1198 ms 33928 KB Output is correct
12 Correct 1166 ms 30064 KB Output is correct
13 Correct 1241 ms 33860 KB Output is correct
14 Correct 1166 ms 30008 KB Output is correct
15 Correct 754 ms 26688 KB Output is correct
16 Correct 493 ms 27780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 1 ms 312 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 874 ms 22436 KB Output is correct
9 Correct 1000 ms 24012 KB Output is correct
10 Correct 1254 ms 24848 KB Output is correct
11 Correct 1662 ms 34680 KB Output is correct
12 Correct 1584 ms 34804 KB Output is correct
13 Correct 1897 ms 45384 KB Output is correct
14 Correct 819 ms 26704 KB Output is correct
15 Correct 499 ms 27656 KB Output is correct
16 Correct 4 ms 340 KB Output is correct
17 Correct 3 ms 464 KB Output is correct
18 Correct 3 ms 460 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 2488 ms 42688 KB Output is correct
21 Correct 2032 ms 35912 KB Output is correct
22 Correct 1538 ms 34976 KB Output is correct
23 Correct 488 ms 27748 KB Output is correct
24 Correct 225 ms 14736 KB Output is correct
25 Correct 269 ms 19448 KB Output is correct
26 Correct 250 ms 19380 KB Output is correct
27 Correct 796 ms 26648 KB Output is correct
28 Correct 489 ms 27700 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 3 ms 504 KB Output is correct
31 Correct 3 ms 468 KB Output is correct
32 Correct 3 ms 340 KB Output is correct
33 Correct 770 ms 30584 KB Output is correct
34 Correct 1328 ms 44816 KB Output is correct
35 Correct 1928 ms 45380 KB Output is correct
36 Correct 2411 ms 40136 KB Output is correct
37 Correct 1210 ms 33848 KB Output is correct
38 Correct 1168 ms 30092 KB Output is correct
39 Correct 1198 ms 33928 KB Output is correct
40 Correct 1166 ms 30064 KB Output is correct
41 Correct 1241 ms 33860 KB Output is correct
42 Correct 1166 ms 30008 KB Output is correct
43 Correct 754 ms 26688 KB Output is correct
44 Correct 493 ms 27780 KB Output is correct
45 Correct 416 ms 17840 KB Output is correct
46 Correct 572 ms 18116 KB Output is correct
47 Correct 971 ms 21660 KB Output is correct
48 Correct 1641 ms 34484 KB Output is correct
49 Correct 306 ms 20900 KB Output is correct
50 Correct 321 ms 20672 KB Output is correct
51 Correct 302 ms 21032 KB Output is correct
52 Correct 321 ms 21076 KB Output is correct
53 Correct 333 ms 20628 KB Output is correct
54 Correct 324 ms 21052 KB Output is correct