Submission #209698

# Submission time Handle Problem Language Result Execution time Memory
209698 2020-03-15T09:30:10 Z DodgeBallMan Fortune Telling 2 (JOI14_fortune_telling2) C++14
100 / 100
434 ms 9960 KB
#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second 

using namespace std;

const int N = 2e5 + 10;
int n, k, que[N];
long long ans;
int seg[4*N], fen[N];
vector<int> coord;
vector<pii> quer, card;

void update( int pos, int val, int l = 1, int r = k, int idx = 1 ) {
    //printf("l %d r %d\n",l,r);
    if( l == r ) return void( seg[idx] = val );
    int mid = l + r >> 1;
    if( pos <= mid ) update( pos, val, l, mid, idx<<1 );
    else update( pos, val, mid+1, r, idx<<1|1 );
    seg[idx] = max( seg[idx<<1], seg[idx<<1|1] );
}

int query( int ll, int rr, int l = 1, int r = k, int idx = 1 ) {
    if( ll > rr ) return 0;
    if( l > rr || r < ll ) return 0;
    if( l >= ll && r <= rr ) return seg[idx];
    int mid = l + r >> 1;
    return max( query( ll, rr, l, mid, idx<<1 ), query( ll, rr, mid+1, r, idx<<1|1 ) );
}

void fup( int idx ) { for( int i = idx ; i < N ; i+= i & -i ) fen[i] += 1; }
int fque( int idx ) {
    int ret = 0;
    for( int i = idx ; i > 0 ; i -= i & -i ) ret += fen[i];
    return ret;
}

int main()
{
    scanf("%d %d",&n,&k);
    for( int i = 1, a, b ; i <= n ; i++ ) {
        scanf("%d %d",&a,&b);
        card.emplace_back( a, b );
    }
    for( int i = 1, a ; i <= k ; i++ ) {
        scanf("%d",&que[i]);
        coord.emplace_back( que[i] );
    }
    sort( coord.begin(), coord.end() );
    for( int i = 1 ; i <= k ; i++ ) {
        que[i] = lower_bound( coord.begin(), coord.end(), que[i] ) - coord.begin() + 1;
        //printf("%d %d\n",i,que[i]);
        update( que[i], i );
    }
    //printf("%d\n",query( 1, k ) );
    for( int i = 0 ; i < card.size() ; i++ ) {
        int a = min( card[i].x, card[i].y ), b = max( card[i].x, card[i].y );
        a = lower_bound( coord.begin(), coord.end(), a ) - coord.begin() + 1;
        b = upper_bound( coord.begin(), coord.end(), b-1 ) - coord.begin();
        int qq = query( a, b );
        //printf("%d %d %d %d\n",i,a,b,qq);
        if( qq ) 
            if( card[i].x < card[i].y ) swap( card[i].x, card[i].y );
        quer.emplace_back( qq, i );
    }
    sort( quer.begin(), quer.end(), greater<pii>() );
    int ptr = k;
    for( pii i : quer ) {
        //printf("%d %d\n",i.x,i.y);
        while( ptr > 0 && ptr > i.x ) {
            //printf("idx ptr%d %d\n",que[ptr],ptr);
            fup( que[ptr] );
            ptr--;
        }
        int b = max( card[i.y].x, card[i.y].y );
        int idx = lower_bound( coord.begin(), coord.end(), b ) - coord.begin();
        int cnt = fque( N-1 ) - fque( idx );
        //printf("%d\n",fque(N-1));
        //printf("idx%d cnt%d\n",idx,cnt);
        if( cnt % 2 ) swap( card[i.y].x, card[i.y].y );
        ans += ( long long )card[i.y].x;
        //printf("%d %d\n",i.y,card[i.y].x);
    }
    printf("%lld",ans);
    return 0;
}

Compilation message

fortune_telling2.cpp: In function 'void update(int, int, int, int, int)':
fortune_telling2.cpp:18:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     int mid = l + r >> 1;
               ~~^~~
fortune_telling2.cpp: In function 'int query(int, int, int, int, int)':
fortune_telling2.cpp:28:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     int mid = l + r >> 1;
               ~~^~~
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:46:21: warning: unused variable 'a' [-Wunused-variable]
     for( int i = 1, a ; i <= k ; i++ ) {
                     ^
fortune_telling2.cpp:57:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for( int i = 0 ; i < card.size() ; i++ ) {
                      ~~^~~~~~~~~~~~~
fortune_telling2.cpp:41:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d",&n,&k);
     ~~~~~^~~~~~~~~~~~~~~
fortune_telling2.cpp:43:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d",&a,&b);
         ~~~~~^~~~~~~~~~~~~~~
fortune_telling2.cpp:47:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&que[i]);
         ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 6 ms 376 KB Output is correct
3 Correct 6 ms 504 KB Output is correct
4 Correct 6 ms 376 KB Output is correct
5 Correct 6 ms 376 KB Output is correct
6 Correct 6 ms 376 KB Output is correct
7 Correct 6 ms 376 KB Output is correct
8 Correct 6 ms 376 KB Output is correct
9 Correct 6 ms 376 KB Output is correct
10 Correct 6 ms 376 KB Output is correct
11 Correct 6 ms 376 KB Output is correct
12 Correct 7 ms 376 KB Output is correct
13 Correct 6 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 6 ms 376 KB Output is correct
3 Correct 6 ms 504 KB Output is correct
4 Correct 6 ms 376 KB Output is correct
5 Correct 6 ms 376 KB Output is correct
6 Correct 6 ms 376 KB Output is correct
7 Correct 6 ms 376 KB Output is correct
8 Correct 6 ms 376 KB Output is correct
9 Correct 6 ms 376 KB Output is correct
10 Correct 6 ms 376 KB Output is correct
11 Correct 6 ms 376 KB Output is correct
12 Correct 7 ms 376 KB Output is correct
13 Correct 6 ms 376 KB Output is correct
14 Correct 21 ms 1148 KB Output is correct
15 Correct 39 ms 1780 KB Output is correct
16 Correct 57 ms 1652 KB Output is correct
17 Correct 78 ms 3184 KB Output is correct
18 Correct 79 ms 2412 KB Output is correct
19 Correct 75 ms 2416 KB Output is correct
20 Correct 79 ms 2544 KB Output is correct
21 Correct 70 ms 2284 KB Output is correct
22 Correct 55 ms 2288 KB Output is correct
23 Correct 57 ms 2416 KB Output is correct
24 Correct 56 ms 2416 KB Output is correct
25 Correct 57 ms 2288 KB Output is correct
26 Correct 66 ms 2412 KB Output is correct
27 Correct 68 ms 2416 KB Output is correct
28 Correct 66 ms 2544 KB Output is correct
29 Correct 63 ms 2416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 6 ms 376 KB Output is correct
3 Correct 6 ms 504 KB Output is correct
4 Correct 6 ms 376 KB Output is correct
5 Correct 6 ms 376 KB Output is correct
6 Correct 6 ms 376 KB Output is correct
7 Correct 6 ms 376 KB Output is correct
8 Correct 6 ms 376 KB Output is correct
9 Correct 6 ms 376 KB Output is correct
10 Correct 6 ms 376 KB Output is correct
11 Correct 6 ms 376 KB Output is correct
12 Correct 7 ms 376 KB Output is correct
13 Correct 6 ms 376 KB Output is correct
14 Correct 21 ms 1148 KB Output is correct
15 Correct 39 ms 1780 KB Output is correct
16 Correct 57 ms 1652 KB Output is correct
17 Correct 78 ms 3184 KB Output is correct
18 Correct 79 ms 2412 KB Output is correct
19 Correct 75 ms 2416 KB Output is correct
20 Correct 79 ms 2544 KB Output is correct
21 Correct 70 ms 2284 KB Output is correct
22 Correct 55 ms 2288 KB Output is correct
23 Correct 57 ms 2416 KB Output is correct
24 Correct 56 ms 2416 KB Output is correct
25 Correct 57 ms 2288 KB Output is correct
26 Correct 66 ms 2412 KB Output is correct
27 Correct 68 ms 2416 KB Output is correct
28 Correct 66 ms 2544 KB Output is correct
29 Correct 63 ms 2416 KB Output is correct
30 Correct 146 ms 4972 KB Output is correct
31 Correct 213 ms 5996 KB Output is correct
32 Correct 283 ms 6380 KB Output is correct
33 Correct 423 ms 9960 KB Output is correct
34 Correct 134 ms 4072 KB Output is correct
35 Correct 420 ms 9956 KB Output is correct
36 Correct 434 ms 9956 KB Output is correct
37 Correct 425 ms 9956 KB Output is correct
38 Correct 409 ms 9188 KB Output is correct
39 Correct 405 ms 9956 KB Output is correct
40 Correct 361 ms 9188 KB Output is correct
41 Correct 409 ms 9416 KB Output is correct
42 Correct 397 ms 9828 KB Output is correct
43 Correct 248 ms 9192 KB Output is correct
44 Correct 263 ms 9188 KB Output is correct
45 Correct 247 ms 9156 KB Output is correct
46 Correct 307 ms 9160 KB Output is correct
47 Correct 281 ms 9956 KB Output is correct
48 Correct 362 ms 9956 KB Output is correct
49 Correct 345 ms 9956 KB Output is correct