Submission #51767

# Submission time Handle Problem Language Result Execution time Memory
51767 2018-06-21T04:53:07 Z model_code Employment (JOI16_employment) C++17
100 / 100
426 ms 9248 KB
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

class BIT{
private:
  vector<int> dat;
  int size;
public:
  void init( int n ){
    size = 1;
    while( size < n ) size *= 2;
    dat = vector<int>( size , 0 );
  }
  void add( int k , int v ){
    for( int i = k+1; i <= size; i += i & -i ) dat[i-1] += v;
  }
  int sum( int k ){ // [ 0 , k ]
    int res = 0;
    for( int i = k+1; i > 0; i -= i & -i ) res += dat[i-1];
    return res;
  }
};

vector<int> compress( vector<int> compress_array ){
  vector<int> ord = compress_array, res(0);
  sort( ord.begin() , ord.end() );
  ord.erase( unique( ord.begin() , ord.end() ) , ord.end() );
  for( int i = 0; i < compress_array.size(); i++ ) res.push_back( lower_bound( ord.begin() , ord.end() , compress_array[i] ) - ord.begin() );
  return res;
}

const int OUTPUT_QUERY = 1;
const int UPDATE_QUERY = 2;
const int MAX_N = 200000;
const int MAX_M = 200000;

int n, m;
BIT bit;
vector<int> vals, compressed;
int val[MAX_N+2], type[MAX_M], pos[MAX_M];

void add( int l , int r , int v ){ // [ l , r )
  bit.add( l , v );
  bit.add( r , -v );
}

int get( int k ){
  return bit.sum( k );
}

void update( int pos , int new_val ){
  int neighbor_min = min( val[pos-1] , val[pos+1] );
  int neighbor_max = max( val[pos-1] , val[pos+1] );
  
  if( val[pos] < new_val ){ // up
    if( val[pos] < neighbor_min ) add( val[pos] + 1 , min( neighbor_min , new_val ) + 1 , -1 );
    if( new_val  > neighbor_max ) add( max( val[pos] , neighbor_max ) + 1 , new_val + 1 , 1  );
  } else if( val[pos] > new_val ){ // down
    if( val[pos] > neighbor_max ) add( max( neighbor_max , new_val ) + 1 , val[pos] + 1 , -1 );
    if( new_val  < neighbor_min ) add( new_val + 1 , min( val[pos] , neighbor_min ) + 1 , 1  );
  }
  
  val[pos] = new_val;
}

void output( int threshold ){
  printf( "%d\n" , get( threshold ) );
}

int main(){

  scanf( "%d %d" , &n , &m );

  vals = vector<int>( n+m+1 , 0 );
  for( int i = 0; i < n; i++ ) scanf( "%d" , &vals[i] );
  for( int i = 0; i < m; i++ ){
    scanf( "%d" , &type[i] );
    if( type[i] == OUTPUT_QUERY ){
      scanf( "%d" , &vals[n+i] );
    } else if( type[i] == UPDATE_QUERY ){
      scanf( "%d %d" , &pos[i] , &vals[n+i] );
    }
  }

  compressed = compress( vals );

  bit.init( n+m+1 );
  for( int i = 0; i < n; i++ ) update( i+1 , compressed[i] );

  for( int i = 0; i < m; i++ ){
    if( type[i] == OUTPUT_QUERY ) output( compressed[n+i] );    
    else if( type[i] == UPDATE_QUERY ) update( pos[i] , compressed[n+i] );
  }
  
  return 0;
}

Compilation message

employment.cpp: In function 'std::vector<int> compress(std::vector<int>)':
employment.cpp:31:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for( int i = 0; i < compress_array.size(); i++ ) res.push_back( lower_bound( ord.begin() , ord.end() , compress_array[i] ) - ord.begin() );
                   ~~^~~~~~~~~~~~~~~~~~~~~~~
employment.cpp: In function 'int main()':
employment.cpp:75:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf( "%d %d" , &n , &m );
   ~~~~~^~~~~~~~~~~~~~~~~~~~~
employment.cpp:78:37: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   for( int i = 0; i < n; i++ ) scanf( "%d" , &vals[i] );
                                ~~~~~^~~~~~~~~~~~~~~~~~~
employment.cpp:80:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf( "%d" , &type[i] );
     ~~~~~^~~~~~~~~~~~~~~~~~~
employment.cpp:82:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf( "%d" , &vals[n+i] );
       ~~~~~^~~~~~~~~~~~~~~~~~~~~
employment.cpp:84:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf( "%d %d" , &pos[i] , &vals[n+i] );
       ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 256 KB Output is correct
2 Correct 2 ms 360 KB Output is correct
3 Correct 2 ms 564 KB Output is correct
4 Correct 4 ms 564 KB Output is correct
5 Correct 3 ms 564 KB Output is correct
6 Correct 2 ms 564 KB Output is correct
7 Correct 4 ms 568 KB Output is correct
8 Correct 4 ms 696 KB Output is correct
9 Correct 5 ms 696 KB Output is correct
10 Correct 4 ms 696 KB Output is correct
11 Correct 4 ms 696 KB Output is correct
12 Correct 4 ms 716 KB Output is correct
13 Correct 4 ms 748 KB Output is correct
14 Correct 4 ms 748 KB Output is correct
15 Correct 5 ms 752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 760 KB Output is correct
2 Correct 4 ms 760 KB Output is correct
3 Correct 4 ms 760 KB Output is correct
4 Correct 20 ms 1376 KB Output is correct
5 Correct 40 ms 1908 KB Output is correct
6 Correct 63 ms 2676 KB Output is correct
7 Correct 90 ms 4040 KB Output is correct
8 Correct 116 ms 4504 KB Output is correct
9 Correct 226 ms 7560 KB Output is correct
10 Correct 177 ms 7560 KB Output is correct
11 Correct 235 ms 8016 KB Output is correct
12 Correct 426 ms 8612 KB Output is correct
13 Correct 293 ms 8724 KB Output is correct
14 Correct 252 ms 8724 KB Output is correct
15 Correct 295 ms 8724 KB Output is correct
16 Correct 255 ms 8768 KB Output is correct
17 Correct 263 ms 8768 KB Output is correct
18 Correct 255 ms 8956 KB Output is correct
19 Correct 291 ms 8956 KB Output is correct
20 Correct 338 ms 8956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 256 KB Output is correct
2 Correct 2 ms 360 KB Output is correct
3 Correct 2 ms 564 KB Output is correct
4 Correct 4 ms 564 KB Output is correct
5 Correct 3 ms 564 KB Output is correct
6 Correct 2 ms 564 KB Output is correct
7 Correct 4 ms 568 KB Output is correct
8 Correct 4 ms 696 KB Output is correct
9 Correct 5 ms 696 KB Output is correct
10 Correct 4 ms 696 KB Output is correct
11 Correct 4 ms 696 KB Output is correct
12 Correct 4 ms 716 KB Output is correct
13 Correct 4 ms 748 KB Output is correct
14 Correct 4 ms 748 KB Output is correct
15 Correct 5 ms 752 KB Output is correct
16 Correct 4 ms 760 KB Output is correct
17 Correct 4 ms 760 KB Output is correct
18 Correct 4 ms 760 KB Output is correct
19 Correct 20 ms 1376 KB Output is correct
20 Correct 40 ms 1908 KB Output is correct
21 Correct 63 ms 2676 KB Output is correct
22 Correct 90 ms 4040 KB Output is correct
23 Correct 116 ms 4504 KB Output is correct
24 Correct 226 ms 7560 KB Output is correct
25 Correct 177 ms 7560 KB Output is correct
26 Correct 235 ms 8016 KB Output is correct
27 Correct 426 ms 8612 KB Output is correct
28 Correct 293 ms 8724 KB Output is correct
29 Correct 252 ms 8724 KB Output is correct
30 Correct 295 ms 8724 KB Output is correct
31 Correct 255 ms 8768 KB Output is correct
32 Correct 263 ms 8768 KB Output is correct
33 Correct 255 ms 8956 KB Output is correct
34 Correct 291 ms 8956 KB Output is correct
35 Correct 338 ms 8956 KB Output is correct
36 Correct 3 ms 8956 KB Output is correct
37 Correct 5 ms 8956 KB Output is correct
38 Correct 7 ms 8956 KB Output is correct
39 Correct 22 ms 8956 KB Output is correct
40 Correct 50 ms 8956 KB Output is correct
41 Correct 69 ms 8956 KB Output is correct
42 Correct 105 ms 8956 KB Output is correct
43 Correct 117 ms 8956 KB Output is correct
44 Correct 215 ms 8956 KB Output is correct
45 Correct 174 ms 8956 KB Output is correct
46 Correct 198 ms 8956 KB Output is correct
47 Correct 265 ms 8960 KB Output is correct
48 Correct 265 ms 8992 KB Output is correct
49 Correct 306 ms 9152 KB Output is correct
50 Correct 257 ms 9152 KB Output is correct
51 Correct 269 ms 9152 KB Output is correct
52 Correct 267 ms 9248 KB Output is correct
53 Correct 276 ms 9248 KB Output is correct
54 Correct 278 ms 9248 KB Output is correct
55 Correct 328 ms 9248 KB Output is correct