답안 #295701

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
295701 2020-09-09T20:20:07 Z CaroLinda 가로등 (APIO19_street_lamps) C++14
100 / 100
4953 ms 179264 KB
#include <bits/stdc++.h>

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimization ("O2")


#define lp(i,a,b) for(int i = a; i < b; i++)
#define pb push_back
#define sz(x) (int)(x.size())
#define all(x) x.begin(),x.end()
#define debug printf
#define tiii tuple<int,int,int>
#define mkt make_tuple
#define pii pair<int,int>
#define mk make_pair
#define ll long long
#define ff first
#define ss second

const int MAXN = 3e5+10 ;
const int MAX = 1e7+100 ;

using namespace std ;

vector<int> e , d , soma ;
int cnt ;

struct SegNode
{

    void upd(int &pos, int l , int r, int k, int toSum )
    {
        if(!pos) pos = cnt++ ;

        soma[pos] += toSum ;

        if(l == r) return;

        int aux , mid = (l+r)>>1 ;

        if( k <= mid ) upd( e[pos] , l , mid , k , toSum ) ;
        else upd(d[pos] , mid+1, r, k , toSum ) ;

    }

    int qry(int pos, int l, int r, int k)
    {
        if(!pos) return 0 ;
        if(l == r) return soma[pos] ;

        int mid = (l+r)>>1 ;

        if( k <= mid ) return qry(e[pos] , l , mid , k ) ;
        return soma[ e[pos] ] + qry( d[pos] , mid+1, r, k ) ;

    }

};

int N , Q ;
set<pii> intervalos ;
char str[MAXN] , q[MAXN] ;

SegNode tree[MAXN] ;

void upd(int pos, int l, int r, int toSum) {

    for(int i = pos ; i <= N ; i+=(i&-i))
    {
        tree[i].upd( i ,1,N+5, l , toSum) ;
        tree[i].upd( i , 1 , N+5 , r+1, -toSum ) ;
    }
}
int qry(int x , int y )
{
    int tot = 0 ;
    for(int i = x ; i > 0 ; i -= (i&-i)) tot += tree[i].qry( i ,1,N+5, y) ;

    return tot ;
}

int main()
{

    scanf("%d%d", &N , &Q ) ;
    scanf("%s", str ) ;

    str[N] = '0';
    N++;

    for(int i = 0 ; i < MAX ; i++ )
    {
        e.pb(0);
        d.pb(0);
        soma.pb(0) ;
    }
    cnt = N+1 ;

    for(int i = 0 ; i < N ; i++ )
    {

        int j = i ;
        while( j < N && str[j] == '1' ) j++ ;

        //O intervalo eh [i,j]
        intervalos.insert( mk(i+1,j+1) ) ;

        upd( i+1 , i+1, j+1, -1 ) ;
        upd(j+2, i+1, j+1, 1 ) ;

        i = j ;


    }

    for(int i = 1 ; i <= Q ; i++ )
    {
        scanf("%s", q ) ;

        if(q[0] == 'q')
        {

            int a , b , segQuery ;
            scanf("%d%d", &a, &b );

            segQuery = qry(a,b);

            auto it = intervalos.upper_bound( mk(a,N+5) ) ;
            it-- ;

            if( it->ss >= b ) segQuery += i+1 ;

            printf("%d\n" , segQuery ) ;

        }

        else
        {
            int lamp ;
            scanf("%d", &lamp ) ;

            auto meContem = intervalos.upper_bound(mk(lamp,N+5)) ;
            meContem--;

            if( str[lamp-1] == '0' )
            {

                pii toInsert=*meContem ;
                auto prox = meContem ; prox++ ;

                toInsert.ss = prox->ss ;
                intervalos.erase(prox) ;

                meContem = intervalos.upper_bound(mk(lamp,N+5)) ; meContem-- ;
                intervalos.erase(meContem) ;

                intervalos.insert( toInsert ) ;
                upd( toInsert.ff, lamp+1, toInsert.ss , -i-1 ) ;
                upd(lamp + 1 , lamp+1, toInsert.ss , i+1 ) ;

                str[lamp-1] = '1' ;

            }

            else
            {

                pii toInsert1 = mk(meContem->ff,lamp) ;
                pii toInsert2 = mk(lamp+1, meContem->ss ) ;

                upd(toInsert1.ff, toInsert2.ff, toInsert2.ss , i+1 ) ;
                upd( toInsert1.ss + 1 , toInsert2.ff, toInsert2.ss , -i-1 ) ;


                intervalos.erase( meContem ) ;
                intervalos.insert(toInsert1) ;
                intervalos.insert(toInsert2) ;

                str[lamp-1] = '0' ;

            }

        }

    }

}

/*
1 3
0
query 1 2
toggle 1
query 1 2

5 9
11011
query 1 2
query 1 2
query 1 6
query 3 4
toggle 3
query 3 4
query 1 6
toggle 3
query 1 6
*/

Compilation message

street_lamps.cpp:5: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
    5 | #pragma GCC optimization ("unroll-loops")
      | 
street_lamps.cpp:6: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
    6 | #pragma GCC optimization ("O2")
      | 
street_lamps.cpp: In member function 'void SegNode::upd(int&, int, int, int, int)':
street_lamps.cpp:41:13: warning: unused variable 'aux' [-Wunused-variable]
   41 |         int aux , mid = (l+r)>>1 ;
      |             ^~~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:87:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   87 |     scanf("%d%d", &N , &Q ) ;
      |     ~~~~~^~~~~~~~~~~~~~~~~~
street_lamps.cpp:88:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   88 |     scanf("%s", str ) ;
      |     ~~~~~^~~~~~~~~~~~
street_lamps.cpp:120:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  120 |         scanf("%s", q ) ;
      |         ~~~~~^~~~~~~~~~
street_lamps.cpp:126:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  126 |             scanf("%d%d", &a, &b );
      |             ~~~~~^~~~~~~~~~~~~~~~~
street_lamps.cpp:142:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  142 |             scanf("%d", &lamp ) ;
      |             ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 255 ms 131912 KB Output is correct
2 Correct 254 ms 131832 KB Output is correct
3 Correct 256 ms 131884 KB Output is correct
4 Correct 252 ms 131892 KB Output is correct
5 Correct 249 ms 131828 KB Output is correct
6 Correct 249 ms 131912 KB Output is correct
7 Correct 253 ms 131944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 566 ms 131956 KB Output is correct
2 Correct 649 ms 131876 KB Output is correct
3 Correct 966 ms 131900 KB Output is correct
4 Correct 3086 ms 134456 KB Output is correct
5 Correct 2667 ms 132656 KB Output is correct
6 Correct 3340 ms 142236 KB Output is correct
7 Correct 3005 ms 164016 KB Output is correct
8 Correct 439 ms 132720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 256 ms 131976 KB Output is correct
2 Correct 255 ms 131888 KB Output is correct
3 Correct 253 ms 131916 KB Output is correct
4 Correct 251 ms 131828 KB Output is correct
5 Correct 4953 ms 179264 KB Output is correct
6 Correct 3673 ms 153948 KB Output is correct
7 Correct 2464 ms 132960 KB Output is correct
8 Correct 440 ms 132708 KB Output is correct
9 Correct 367 ms 132016 KB Output is correct
10 Correct 379 ms 131876 KB Output is correct
11 Correct 377 ms 131896 KB Output is correct
12 Correct 2565 ms 163836 KB Output is correct
13 Correct 459 ms 132656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 258 ms 131852 KB Output is correct
2 Correct 264 ms 131832 KB Output is correct
3 Correct 259 ms 131848 KB Output is correct
4 Correct 287 ms 131896 KB Output is correct
5 Correct 1567 ms 132260 KB Output is correct
6 Correct 2271 ms 132188 KB Output is correct
7 Correct 3162 ms 137012 KB Output is correct
8 Correct 4075 ms 151336 KB Output is correct
9 Correct 699 ms 131884 KB Output is correct
10 Correct 661 ms 131988 KB Output is correct
11 Correct 737 ms 131832 KB Output is correct
12 Correct 663 ms 131896 KB Output is correct
13 Correct 724 ms 132040 KB Output is correct
14 Correct 656 ms 131892 KB Output is correct
15 Correct 2430 ms 163868 KB Output is correct
16 Correct 442 ms 132724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 255 ms 131912 KB Output is correct
2 Correct 254 ms 131832 KB Output is correct
3 Correct 256 ms 131884 KB Output is correct
4 Correct 252 ms 131892 KB Output is correct
5 Correct 249 ms 131828 KB Output is correct
6 Correct 249 ms 131912 KB Output is correct
7 Correct 253 ms 131944 KB Output is correct
8 Correct 566 ms 131956 KB Output is correct
9 Correct 649 ms 131876 KB Output is correct
10 Correct 966 ms 131900 KB Output is correct
11 Correct 3086 ms 134456 KB Output is correct
12 Correct 2667 ms 132656 KB Output is correct
13 Correct 3340 ms 142236 KB Output is correct
14 Correct 3005 ms 164016 KB Output is correct
15 Correct 439 ms 132720 KB Output is correct
16 Correct 256 ms 131976 KB Output is correct
17 Correct 255 ms 131888 KB Output is correct
18 Correct 253 ms 131916 KB Output is correct
19 Correct 251 ms 131828 KB Output is correct
20 Correct 4953 ms 179264 KB Output is correct
21 Correct 3673 ms 153948 KB Output is correct
22 Correct 2464 ms 132960 KB Output is correct
23 Correct 440 ms 132708 KB Output is correct
24 Correct 367 ms 132016 KB Output is correct
25 Correct 379 ms 131876 KB Output is correct
26 Correct 377 ms 131896 KB Output is correct
27 Correct 2565 ms 163836 KB Output is correct
28 Correct 459 ms 132656 KB Output is correct
29 Correct 258 ms 131852 KB Output is correct
30 Correct 264 ms 131832 KB Output is correct
31 Correct 259 ms 131848 KB Output is correct
32 Correct 287 ms 131896 KB Output is correct
33 Correct 1567 ms 132260 KB Output is correct
34 Correct 2271 ms 132188 KB Output is correct
35 Correct 3162 ms 137012 KB Output is correct
36 Correct 4075 ms 151336 KB Output is correct
37 Correct 699 ms 131884 KB Output is correct
38 Correct 661 ms 131988 KB Output is correct
39 Correct 737 ms 131832 KB Output is correct
40 Correct 663 ms 131896 KB Output is correct
41 Correct 724 ms 132040 KB Output is correct
42 Correct 656 ms 131892 KB Output is correct
43 Correct 2430 ms 163868 KB Output is correct
44 Correct 442 ms 132724 KB Output is correct
45 Correct 383 ms 131892 KB Output is correct
46 Correct 446 ms 131888 KB Output is correct
47 Correct 1407 ms 132156 KB Output is correct
48 Correct 2871 ms 138404 KB Output is correct
49 Correct 386 ms 131948 KB Output is correct
50 Correct 392 ms 131892 KB Output is correct
51 Correct 385 ms 131924 KB Output is correct
52 Correct 389 ms 131896 KB Output is correct
53 Correct 392 ms 131912 KB Output is correct
54 Correct 386 ms 131892 KB Output is correct