Submission #543598

# Submission time Handle Problem Language Result Execution time Memory
543598 2022-03-31T00:14:41 Z chonka Examination (JOI19_examination) C++
100 / 100
148 ms 12200 KB
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;

// mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

#define MAXN 100007

int n , q ;

struct el {
    int x , y , sm , id ;
};

el a[ MAXN ] , qlist[ MAXN ] ;
int ans[ MAXN ] ;
vector < pair < int , int > > srtx , srty ;
int posx[ MAXN ] , posy[ MAXN ] ;

class fenw {
public :
    int tr[ MAXN ] ;
    void init ( ) {
        for ( int i = 0 ; i <= n ; ++ i ) {
            tr[ i ] = 0 ;
        }
    }
    void update ( int pos , int add ) {
        for ( int i = pos ; i <= n ; i += ( i & ( -i ) ) ) {
            tr[ i ] += add ;
        }
    }
    int query ( int pos ) {
        int ans = 0 ;
        for ( int i = pos ; i > 0 ; i -= ( i & ( -i ) ) ) {
            ans += tr[ i ] ;
        }
        return ans ;
    }
};
fenw w ;

int get_fst ( vector < pair < int , int > > &wh , int sr ) {
    int l , r , mid ;
    l = 0 , r = n - 1 ;
    if ( wh[ r ].first < sr ) { return n ; }
    while ( r - l > 3 ) {
        mid = ( l + r ) / 2 ;
        if ( wh[ mid ].first >= sr ) { r = mid ; }
        else { l = mid ; }
    }
    while ( wh[ l ].first < sr ) { ++ l ; }
    return l ;
}

void input ( ) {
    cin >> n >> q ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        cin >> a[ i ].x >> a[ i ].y ;
        a[ i ].sm = a[ i ].x + a[ i ].y ;
        a[ i ].id = i ;
        srtx.push_back ( { a[ i ].x , i } ) ;
        srty.push_back ( { a[ i ].y , i } ) ;
    }
    sort ( srtx.begin ( ) , srtx.end ( ) ) ;
    sort ( srty.begin ( ) , srty.end ( ) ) ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        posx[ srtx[ i - 1 ].second ] = i ;
        posy[ srty[ i - 1 ].second ] = i ;
    }
    for ( int i = 1 ; i <= q ; ++ i ) {
        cin >> qlist[ i ].x >> qlist[ i ].y >> qlist[ i ].sm ;
        qlist[ i ].id = i ;
    }
}

void solve ( ) {
    auto cmp_x = [ & ] ( el p1 , el p2 ) {
        return ( p1.x > p2.x ) ;
    };
    auto cmp_sm = [ & ] ( el p1 , el p2 ) {
        return ( p1.sm > p2.sm ) ;
    };
    sort ( qlist + 1 , qlist + q + 1 , cmp_x ) ;
    sort ( a + 1 , a + n + 1 , cmp_x ) ;
    int pos = 0 ;
    w.init ( ) ;
    for ( int i = 1 ; i <= q ; ++ i ) {
        while ( pos + 1 <= n && a[ pos + 1 ].x >= qlist[ i ].x ) {
            w.update ( posy[ a[ pos + 1 ].id ] , 1 ) ;
            ++ pos ;
        }
        if ( qlist[ i ].x + qlist[ i ].y >= qlist[ i ].sm ) {
            int en = get_fst ( srty , qlist[ i ].y ) ;
            ans[ qlist[ i ].id ] = pos - w.query ( en ) ;
        }
    }
    sort ( qlist + 1 , qlist + q + 1 , cmp_sm ) ;
    sort ( a + 1 , a + n + 1 , cmp_sm ) ;
    w.init ( ) , pos = 0 ;
    for ( int i = 1 ; i <= q ; ++ i ) {
        while ( pos + 1 <= n && a[ pos + 1 ].sm >= qlist[ i ].sm ) {
            w.update ( posx[ a[ pos + 1 ].id ] , 1 ) ;
            ++ pos ;
        }
        if ( qlist[ i ].x + qlist[ i ].y <= qlist[ i ].sm ) {
            ans[ qlist[ i ].id ] = pos ;
            int en = get_fst ( srtx , qlist[ i ].x ) ;
            ans[ qlist[ i ].id ] -= w.query ( en ) ;
        }
    }
    w.init ( ) , pos = 0 ;
    for ( int i = 1 ; i <= q ; ++ i ) {
        while ( pos + 1 <= n && a[ pos + 1 ].sm >= qlist[ i ].sm ) {
            w.update ( posy[ a[ pos + 1 ].id ] , 1 ) ;
            ++ pos ;
        }
        if ( qlist[ i ].x + qlist[ i ].y <= qlist[ i ].sm ) {
            int en = get_fst ( srty , qlist[ i ].y ) ;
            ans[ qlist[ i ].id ] -= w.query ( en ) ;
        }
    }
    for ( int i = 1 ; i <= q ; ++ i ) {
        cout << ans[ i ] << "\n" ;
    }
}

int main ( ) {
    ios_base :: sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    int t = 1 ;
    // cin >> t ;
    while ( t -- ) {
        input ( ) ;
        solve ( ) ;
    }
    return 0 ;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 352 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 4 ms 724 KB Output is correct
8 Correct 4 ms 724 KB Output is correct
9 Correct 4 ms 728 KB Output is correct
10 Correct 3 ms 600 KB Output is correct
11 Correct 4 ms 596 KB Output is correct
12 Correct 3 ms 596 KB Output is correct
13 Correct 4 ms 724 KB Output is correct
14 Correct 4 ms 608 KB Output is correct
15 Correct 3 ms 608 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 3 ms 596 KB Output is correct
18 Correct 2 ms 608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 115 ms 9864 KB Output is correct
2 Correct 112 ms 9852 KB Output is correct
3 Correct 112 ms 9816 KB Output is correct
4 Correct 97 ms 9056 KB Output is correct
5 Correct 102 ms 9072 KB Output is correct
6 Correct 82 ms 8332 KB Output is correct
7 Correct 110 ms 9720 KB Output is correct
8 Correct 108 ms 9728 KB Output is correct
9 Correct 108 ms 9824 KB Output is correct
10 Correct 85 ms 8884 KB Output is correct
11 Correct 82 ms 8856 KB Output is correct
12 Correct 55 ms 7992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 115 ms 9864 KB Output is correct
2 Correct 112 ms 9852 KB Output is correct
3 Correct 112 ms 9816 KB Output is correct
4 Correct 97 ms 9056 KB Output is correct
5 Correct 102 ms 9072 KB Output is correct
6 Correct 82 ms 8332 KB Output is correct
7 Correct 110 ms 9720 KB Output is correct
8 Correct 108 ms 9728 KB Output is correct
9 Correct 108 ms 9824 KB Output is correct
10 Correct 85 ms 8884 KB Output is correct
11 Correct 82 ms 8856 KB Output is correct
12 Correct 55 ms 7992 KB Output is correct
13 Correct 135 ms 10240 KB Output is correct
14 Correct 128 ms 10192 KB Output is correct
15 Correct 112 ms 9804 KB Output is correct
16 Correct 114 ms 9400 KB Output is correct
17 Correct 114 ms 9536 KB Output is correct
18 Correct 86 ms 8336 KB Output is correct
19 Correct 129 ms 10252 KB Output is correct
20 Correct 128 ms 10232 KB Output is correct
21 Correct 134 ms 10240 KB Output is correct
22 Correct 86 ms 8824 KB Output is correct
23 Correct 83 ms 8848 KB Output is correct
24 Correct 55 ms 7960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 352 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 4 ms 724 KB Output is correct
8 Correct 4 ms 724 KB Output is correct
9 Correct 4 ms 728 KB Output is correct
10 Correct 3 ms 600 KB Output is correct
11 Correct 4 ms 596 KB Output is correct
12 Correct 3 ms 596 KB Output is correct
13 Correct 4 ms 724 KB Output is correct
14 Correct 4 ms 608 KB Output is correct
15 Correct 3 ms 608 KB Output is correct
16 Correct 3 ms 596 KB Output is correct
17 Correct 3 ms 596 KB Output is correct
18 Correct 2 ms 608 KB Output is correct
19 Correct 115 ms 9864 KB Output is correct
20 Correct 112 ms 9852 KB Output is correct
21 Correct 112 ms 9816 KB Output is correct
22 Correct 97 ms 9056 KB Output is correct
23 Correct 102 ms 9072 KB Output is correct
24 Correct 82 ms 8332 KB Output is correct
25 Correct 110 ms 9720 KB Output is correct
26 Correct 108 ms 9728 KB Output is correct
27 Correct 108 ms 9824 KB Output is correct
28 Correct 85 ms 8884 KB Output is correct
29 Correct 82 ms 8856 KB Output is correct
30 Correct 55 ms 7992 KB Output is correct
31 Correct 135 ms 10240 KB Output is correct
32 Correct 128 ms 10192 KB Output is correct
33 Correct 112 ms 9804 KB Output is correct
34 Correct 114 ms 9400 KB Output is correct
35 Correct 114 ms 9536 KB Output is correct
36 Correct 86 ms 8336 KB Output is correct
37 Correct 129 ms 10252 KB Output is correct
38 Correct 128 ms 10232 KB Output is correct
39 Correct 134 ms 10240 KB Output is correct
40 Correct 86 ms 8824 KB Output is correct
41 Correct 83 ms 8848 KB Output is correct
42 Correct 55 ms 7960 KB Output is correct
43 Correct 148 ms 12200 KB Output is correct
44 Correct 139 ms 12172 KB Output is correct
45 Correct 133 ms 12176 KB Output is correct
46 Correct 119 ms 10580 KB Output is correct
47 Correct 124 ms 10708 KB Output is correct
48 Correct 93 ms 8156 KB Output is correct
49 Correct 135 ms 12060 KB Output is correct
50 Correct 140 ms 12100 KB Output is correct
51 Correct 133 ms 12060 KB Output is correct
52 Correct 110 ms 10484 KB Output is correct
53 Correct 86 ms 9692 KB Output is correct