Submission #249028

# Submission time Handle Problem Language Result Execution time Memory
249028 2020-07-14T07:22:02 Z sealnot123 Street Lamps (APIO19_street_lamps) C++14
100 / 100
1542 ms 99864 KB
#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
#define SZ(a) (int)(a).size()
#define FOR(i, a, b) for(int i=(a); i<=(b); ++i)
#define ROF(i, a, b) for(int i=(a); i>=(b); --i)
#define make_unique(a) sort(all((a))), (a).resize(unique(all((a)))-(a).begin())

using namespace std;

typedef pair<int,int> PII;
typedef long long LL;
typedef double DD;
typedef long double LD;
typedef pair<LL,LL> PLL;
typedef pair<DD,DD> PDD;
typedef vector<int> VI;
typedef vector<LL> VL;

const int N = 300003;
set< pair<PII, int> > segments; // (l,r), when
set< pair<PII, int> >::iterator it;
vector<int> value_fw[N], fw[N];
vector< pair<PII, int> > to_update[N];
int ans[N], current_state[N], L[N], R[N];
int n;
char s[N];
void build_fw(int i, int j){
    while(i <= n){
        value_fw[i].pb(-j);
        i += (i&-i);
    }
}
void add(int i, int j, int v){
    j = -j;
    while(i <= n){
        int idx = upper_bound(all(value_fw[i]), j)-value_fw[i].begin();
        while(idx < SZ(fw[i])){
            fw[i][idx] += v;
            idx += (idx&-idx);
        }
        i += (i&-i);
    }
}
int get(int i, int j){
    j = -j;
    int sum = 0;
    while(i > 0){
        int idx = upper_bound(all(value_fw[i]), j)-value_fw[i].begin();
        while(idx > 0){
            sum += fw[i][idx];
            idx -= (idx&-idx);
        }
        i -= (i&-i);
    }
    return sum;
}
void connect(int i, int t){
    //printf("connect %d at %d\n",i,t);
    current_state[i] = 1;
    pair< PII, int > to_insert = { PII(i,i), t };
    PII p;
    int v;
    if(i < n && current_state[i+1]){
        it = segments.lower_bound({PII(i+1,0),0});
        tie(p, v) = *it;
        to_update[t].pb( {p, t-v} );
        build_fw(p.x, p.y);
        to_insert.x.y = p.y;
        segments.erase(it);
    }
    if(i > 1 && current_state[i-1]){
        it = segments.lower_bound({PII(i,0),0});
        --it;
        tie(p, v) = *it;
        to_update[t].pb( {p, t-v} );
        build_fw(p.x, p.y);
        to_insert.x.x = p.x;
        segments.erase(it);
    }
    segments.insert(to_insert);
}
void cut(int i, int t){
    //printf("cut %d at %d\n",i,t);
    current_state[i] = 0;
    it = segments.lower_bound({PII(i+1,0),0});
    --it;
    PII p;
    int v;
    tie(p, v) = *it;
    build_fw(p.x, p.y);
    to_update[t].pb( {p, t-v} );
    segments.erase(it);
    if(i < n && current_state[i+1]){
        segments.insert({PII(i+1,p.y),t});   
    }
    if(i > 1 && current_state[i-1]){
        segments.insert({PII(p.x,i-1),t});
    }
}
char cmd[10];
void solve(){
    int q;
    scanf("%d %d",&n,&q);
    scanf(" %s",s+1);
    FOR(i, 1, n){
        if(s[i] == '1') connect(i, 0);
    }
    FOR(i, 1, q){
        int a, b;
        scanf(" %s %d",cmd,&a);
        if(cmd[0] == 't'){
            if(current_state[a]) cut(a, i);
            else connect(a, i);
        }else{
            scanf("%d",&b);
            --b;
            L[i] = a, R[i] = b;
            // check if there is existing segment
            it = segments.lower_bound({PII(a+1,0),0});
            if(it != segments.begin()){
                --it;
                PII p;
                int v;
                tie(p, v) = *it;
                if(p.x <= a && b <= p.y){
                    ans[i] += i-v;
                }
            }
        }
        //printf("segments: ");
        //for(auto e : segments){
        //    printf("(%d, %d) ",e.x.x, e.x.y);
        //}
        //puts("");
    }
    // build fenwick
    FOR(i, 1, n){
        make_unique(value_fw[i]);
        fw[i].resize(SZ(value_fw[i])+1);
    }
    FOR(i, 1, q){
        for(auto e : to_update[i]){
            add(e.x.x, e.x.y, e.y);
        }
        if(L[i] == 0) continue;
        ans[i] += get(L[i], R[i]);
        printf("%d\n",ans[i]);
    }
}

int main(){
    solve();
	return 0;
}
/*
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */

Compilation message

street_lamps.cpp: In function 'void solve()':
street_lamps.cpp:107: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:108:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf(" %s",s+1);
     ~~~~~^~~~~~~~~~~
street_lamps.cpp:114:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf(" %s %d",cmd,&a);
         ~~~~~^~~~~~~~~~~~~~~~~
street_lamps.cpp:119:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
             scanf("%d",&b);
             ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 21504 KB Output is correct
2 Correct 13 ms 21504 KB Output is correct
3 Correct 12 ms 21504 KB Output is correct
4 Correct 12 ms 21504 KB Output is correct
5 Correct 12 ms 21504 KB Output is correct
6 Correct 12 ms 21504 KB Output is correct
7 Correct 12 ms 21504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 222 ms 32880 KB Output is correct
2 Correct 268 ms 34784 KB Output is correct
3 Correct 405 ms 36840 KB Output is correct
4 Correct 1058 ms 66856 KB Output is correct
5 Correct 1191 ms 68036 KB Output is correct
6 Correct 1079 ms 66856 KB Output is correct
7 Correct 193 ms 36856 KB Output is correct
8 Correct 446 ms 95640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 21632 KB Output is correct
2 Correct 13 ms 21632 KB Output is correct
3 Correct 13 ms 21760 KB Output is correct
4 Correct 13 ms 21632 KB Output is correct
5 Correct 1385 ms 67544 KB Output is correct
6 Correct 1318 ms 67616 KB Output is correct
7 Correct 1169 ms 67356 KB Output is correct
8 Correct 479 ms 95640 KB Output is correct
9 Correct 125 ms 26744 KB Output is correct
10 Correct 139 ms 27000 KB Output is correct
11 Correct 137 ms 27064 KB Output is correct
12 Correct 194 ms 36856 KB Output is correct
13 Correct 480 ms 95516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 21632 KB Output is correct
2 Correct 13 ms 21648 KB Output is correct
3 Correct 13 ms 21632 KB Output is correct
4 Correct 13 ms 21632 KB Output is correct
5 Correct 515 ms 56232 KB Output is correct
6 Correct 806 ms 64680 KB Output is correct
7 Correct 1104 ms 69928 KB Output is correct
8 Correct 1542 ms 80032 KB Output is correct
9 Correct 279 ms 37604 KB Output is correct
10 Correct 257 ms 37604 KB Output is correct
11 Correct 289 ms 37732 KB Output is correct
12 Correct 250 ms 37472 KB Output is correct
13 Correct 285 ms 37732 KB Output is correct
14 Correct 253 ms 37476 KB Output is correct
15 Correct 203 ms 41208 KB Output is correct
16 Correct 469 ms 99864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 21504 KB Output is correct
2 Correct 13 ms 21504 KB Output is correct
3 Correct 12 ms 21504 KB Output is correct
4 Correct 12 ms 21504 KB Output is correct
5 Correct 12 ms 21504 KB Output is correct
6 Correct 12 ms 21504 KB Output is correct
7 Correct 12 ms 21504 KB Output is correct
8 Correct 222 ms 32880 KB Output is correct
9 Correct 268 ms 34784 KB Output is correct
10 Correct 405 ms 36840 KB Output is correct
11 Correct 1058 ms 66856 KB Output is correct
12 Correct 1191 ms 68036 KB Output is correct
13 Correct 1079 ms 66856 KB Output is correct
14 Correct 193 ms 36856 KB Output is correct
15 Correct 446 ms 95640 KB Output is correct
16 Correct 14 ms 21632 KB Output is correct
17 Correct 13 ms 21632 KB Output is correct
18 Correct 13 ms 21760 KB Output is correct
19 Correct 13 ms 21632 KB Output is correct
20 Correct 1385 ms 67544 KB Output is correct
21 Correct 1318 ms 67616 KB Output is correct
22 Correct 1169 ms 67356 KB Output is correct
23 Correct 479 ms 95640 KB Output is correct
24 Correct 125 ms 26744 KB Output is correct
25 Correct 139 ms 27000 KB Output is correct
26 Correct 137 ms 27064 KB Output is correct
27 Correct 194 ms 36856 KB Output is correct
28 Correct 480 ms 95516 KB Output is correct
29 Correct 13 ms 21632 KB Output is correct
30 Correct 13 ms 21648 KB Output is correct
31 Correct 13 ms 21632 KB Output is correct
32 Correct 13 ms 21632 KB Output is correct
33 Correct 515 ms 56232 KB Output is correct
34 Correct 806 ms 64680 KB Output is correct
35 Correct 1104 ms 69928 KB Output is correct
36 Correct 1542 ms 80032 KB Output is correct
37 Correct 279 ms 37604 KB Output is correct
38 Correct 257 ms 37604 KB Output is correct
39 Correct 289 ms 37732 KB Output is correct
40 Correct 250 ms 37472 KB Output is correct
41 Correct 285 ms 37732 KB Output is correct
42 Correct 253 ms 37476 KB Output is correct
43 Correct 203 ms 41208 KB Output is correct
44 Correct 469 ms 99864 KB Output is correct
45 Correct 124 ms 29864 KB Output is correct
46 Correct 156 ms 30836 KB Output is correct
47 Correct 482 ms 44336 KB Output is correct
48 Correct 1113 ms 70024 KB Output is correct
49 Correct 145 ms 29092 KB Output is correct
50 Correct 144 ms 29048 KB Output is correct
51 Correct 150 ms 29292 KB Output is correct
52 Correct 150 ms 29560 KB Output is correct
53 Correct 147 ms 29560 KB Output is correct
54 Correct 157 ms 29560 KB Output is correct