답안 #130715

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
130715 2019-07-16T02:25:26 Z ae04071 가로등 (APIO19_street_lamps) C++11
100 / 100
776 ms 25720 KB
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
using pii = pair<int,int>;

int n,q,cnt[300001],l[300001][2],r[300001][2],v[300001][2];
int ans[300001],qt[300001];
char str[300010];

set<pair<pii,int>> tr;

struct seg_tr{
    int tr[300001];
    void upd(int cur,int val) {
        while(cur<=n) {
            tr[cur] += val;
            cur += cur & -cur;
        }
    }
    int get(int  cur) {
        int ret=0;
        while(cur) {
            ret += tr[cur];
            cur -= cur & -cur;
        }
        return ret;
    }
}st;

void solve(int s,int f) {
    if(s==f) return;

    int md=(s+f)>>1;
    solve(s,md);
    solve(md+1,f);

    vector<pair<pii,int>> ia,qa;
    for(int i=s;i<=md;i++) if(cnt[i]) {
        for(int j=0;j<cnt[i];j++) ia.push_back({pii(l[i][j],r[i][j]),v[i][j]});
    }
    for(int i=md+1;i<=f;i++) if(!cnt[i]) {
        qa.push_back({pii(l[i][0],r[i][0]),i});
    }
    sort(ia.begin(),ia.end(),[](const pair<pii,int> &a, const pair<pii,int> &b) {
        return a.fi.se > b.fi.se;
    });
    sort(qa.begin(),qa.end(),[](const pair<pii,int> &a, const pair<pii,int> &b) {
        return a.fi.se > b.fi.se;
    });

    int i,j;
    for(i=0,j=0;i<(int)qa.size();i++) {
        for(;j<(int)ia.size() && ia[j].fi.se >= qa[i].fi.se; j++) st.upd(ia[j].fi.fi,ia[j].se);
        ans[qa[i].se] += st.get(qa[i].fi.fi);
    }
    for(j=j-1;j>=0;j--) st.upd(ia[j].fi.fi,-ia[j].se);
}

int main() {
    scanf("%d%d",&n,&q);
    scanf("%s",str+1);
    for(int i=1;i<=n;i++) str[i] -= '0';
    
    for(int i=1,j=1;i<=n;i=j) {
        if(!str[i]) {
            j=i+1; continue;
        }
        for(j=i;j<=n && str[j];j++);
        tr.insert({pii(i,j-1), 0});
    }

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

            auto it = tr.upper_bound({pii(a,n+1),n+1});
            if(it!=tr.begin()) {
                it--;
                if(it->fi.se >= b) ans[i] += i-it->se;
            }
            qt[i]=1;
        } else {
            scanf("%d",&a);
            
            str[a]^=1;
            if(str[a]) {
                int li=a,ri=a;
                auto it = tr.lower_bound({pii(a,n), 0});
                if(it!=tr.begin()) {
                    it--;
                    if(it->fi.se == li-1) {
                        tie(l[i][cnt[i]], r[i][cnt[i]]) = it->fi;
                        v[i][cnt[i]++] = i - it->se;
                        li = it->fi.fi;
                        it = tr.erase(it);
                    } else it++;
                }
                if(it!=tr.end()) {
                    if(it->fi.fi == ri+1) {
                        tie(l[i][cnt[i]], r[i][cnt[i]]) = it->fi;
                        v[i][cnt[i]++] = i - it->se;
                        ri = it->fi.se;
                        tr.erase(it);
                    }
                }
                tr.insert({pii(li,ri), i});
            } else {
                auto it = tr.upper_bound({pii(a,n+1),n+1});
                it--;
                tie(l[i][cnt[i]], r[i][cnt[i]]) = it->fi;
                v[i][cnt[i]++] = i-it->se;

                if(it->fi.fi != a) {
                    tr.insert({pii(it->fi.fi, a-1), i});
                }
                if(it->fi.se != a) {
                    tr.insert({pii(a+1, it->fi.se), i});
                }
                tr.erase(it);
            }
        }
    }

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

Compilation message

street_lamps.cpp: In function 'int main()':
street_lamps.cpp:61: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:62:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s",str+1);
     ~~~~~^~~~~~~~~~~~
street_lamps.cpp:76:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%s",tmp);
         ~~~~~^~~~~~~~~~
street_lamps.cpp:78:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d%d",&a,&b);
             ~~~~~^~~~~~~~~~~~~~
street_lamps.cpp:89:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d",&a);
             ~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 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 3 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 416 ms 14252 KB Output is correct
2 Correct 444 ms 17636 KB Output is correct
3 Correct 508 ms 18232 KB Output is correct
4 Correct 733 ms 25544 KB Output is correct
5 Correct 745 ms 23236 KB Output is correct
6 Correct 729 ms 24800 KB Output is correct
7 Correct 405 ms 18388 KB Output is correct
8 Correct 436 ms 18428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 504 KB Output is correct
3 Correct 4 ms 508 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 662 ms 20884 KB Output is correct
6 Correct 758 ms 21580 KB Output is correct
7 Correct 741 ms 23252 KB Output is correct
8 Correct 416 ms 18276 KB Output is correct
9 Correct 223 ms 11544 KB Output is correct
10 Correct 257 ms 14868 KB Output is correct
11 Correct 262 ms 14628 KB Output is correct
12 Correct 406 ms 18428 KB Output is correct
13 Correct 417 ms 18368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 504 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 3 ms 504 KB Output is correct
5 Correct 612 ms 24492 KB Output is correct
6 Correct 665 ms 25720 KB Output is correct
7 Correct 720 ms 24800 KB Output is correct
8 Correct 776 ms 24824 KB Output is correct
9 Correct 444 ms 17112 KB Output is correct
10 Correct 413 ms 17248 KB Output is correct
11 Correct 448 ms 17232 KB Output is correct
12 Correct 412 ms 17240 KB Output is correct
13 Correct 441 ms 17120 KB Output is correct
14 Correct 415 ms 17332 KB Output is correct
15 Correct 407 ms 18516 KB Output is correct
16 Correct 426 ms 18456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 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 3 ms 348 KB Output is correct
8 Correct 416 ms 14252 KB Output is correct
9 Correct 444 ms 17636 KB Output is correct
10 Correct 508 ms 18232 KB Output is correct
11 Correct 733 ms 25544 KB Output is correct
12 Correct 745 ms 23236 KB Output is correct
13 Correct 729 ms 24800 KB Output is correct
14 Correct 405 ms 18388 KB Output is correct
15 Correct 436 ms 18428 KB Output is correct
16 Correct 3 ms 376 KB Output is correct
17 Correct 3 ms 504 KB Output is correct
18 Correct 4 ms 508 KB Output is correct
19 Correct 3 ms 376 KB Output is correct
20 Correct 662 ms 20884 KB Output is correct
21 Correct 758 ms 21580 KB Output is correct
22 Correct 741 ms 23252 KB Output is correct
23 Correct 416 ms 18276 KB Output is correct
24 Correct 223 ms 11544 KB Output is correct
25 Correct 257 ms 14868 KB Output is correct
26 Correct 262 ms 14628 KB Output is correct
27 Correct 406 ms 18428 KB Output is correct
28 Correct 417 ms 18368 KB Output is correct
29 Correct 3 ms 504 KB Output is correct
30 Correct 3 ms 376 KB Output is correct
31 Correct 3 ms 376 KB Output is correct
32 Correct 3 ms 504 KB Output is correct
33 Correct 612 ms 24492 KB Output is correct
34 Correct 665 ms 25720 KB Output is correct
35 Correct 720 ms 24800 KB Output is correct
36 Correct 776 ms 24824 KB Output is correct
37 Correct 444 ms 17112 KB Output is correct
38 Correct 413 ms 17248 KB Output is correct
39 Correct 448 ms 17232 KB Output is correct
40 Correct 412 ms 17240 KB Output is correct
41 Correct 441 ms 17120 KB Output is correct
42 Correct 415 ms 17332 KB Output is correct
43 Correct 407 ms 18516 KB Output is correct
44 Correct 426 ms 18456 KB Output is correct
45 Correct 228 ms 10992 KB Output is correct
46 Correct 276 ms 11268 KB Output is correct
47 Correct 406 ms 14416 KB Output is correct
48 Correct 724 ms 25456 KB Output is correct
49 Correct 285 ms 15872 KB Output is correct
50 Correct 286 ms 15860 KB Output is correct
51 Correct 290 ms 16048 KB Output is correct
52 Correct 286 ms 16020 KB Output is correct
53 Correct 285 ms 16032 KB Output is correct
54 Correct 286 ms 16108 KB Output is correct