Submission #576229

# Submission time Handle Problem Language Result Execution time Memory
576229 2022-06-12T16:27:33 Z mircea_007 Addk (eJOI21_addk) C++17
36 / 100
63 ms 4428 KB
// This program was written on 12.06.2022
// for problem addk
// by Mircea Rebengiuc

#include <stdio.h>
#include <ctype.h>

#define ll long long

#define MAXN 131072
#define MAXK 10

int n;
int v[MAXN];
class AINT {
  private:
    struct Node {
      ll progresion;// sum{ i * v[i] }
      ll sum;// sum{ v[i] }
    } aint[MAXN * 2];
    
    int aintn;
    
    void _update( int root, int rl, int rr, int index, int delta ){
      int mij;

      while( rl != index || rr != index ){
        aint[root].sum += delta;
        aint[root].progresion += delta * (index - rl);
        
        if( (mij = ((rl + rr) >> 1)) < index ){
          root = (root << 1) + 2;
          rl = mij+1;
        }else{
          root = (root << 1) + 1;
          rr = mij;
        }
      }
      
      aint[root].sum += delta;
      // aint[root].progresion is 0
    }
    
    ll _query( int root, int rl, int rr, int ql, int qr, int pcoef, int scoef ){
      int mij = (rl + rr) >> 1;
      
      if( rr < ql || qr < rl )
        return 0;
      
      if( ql <= rl && rr <= qr ){
        return (
          aint[root].progresion * pcoef +
          aint[root].sum * (scoef + ((long long)pcoef) * (rl - ql))
        );
      }
      
      return (
        _query( (root << 1) + 1, rl, mij, ql, qr, pcoef, scoef ) +
        _query( (root << 1) + 2, mij+1, rr, ql, qr, pcoef, scoef )
      );
    }
    
  public:
    void init( int n ){
      aintn = 1;
      while( aintn < n )
        aintn <<= 1;
    }
    
    inline void update( int index, int delta ){
      _update( 0, 0, aintn - 1, index, delta );
    }
    
    inline ll query( int ql, int qr, int pcoef, int scoef ){
      return _query( 0, 0, aintn - 1, ql, qr, pcoef, scoef );
    }
} aint;

int poz[MAXK];

static inline int fgetint(){
  int n = 0, ch;
  
  while( !isdigit( ch = fgetc( stdin ) ) );
  do
    n = n * 10 + ch - '0';
  while( isdigit( ch = fgetc( stdin ) ) );
  
  return n;
}

static inline void update( int k ){
  int updated[MAXK];
  int i = 0;
  
  updated[k - 1] = v[poz[0]];
  for( i = 1 ; i < k ; i++ )
    updated[i - 1] = v[poz[i]];
  
  for( i = 0 ; i < k ; i++ )
    aint.update( poz[i], updated[i] - v[poz[i]] );
}

static inline int min( int a, int b ){ return a < b ? a : b; }

static inline ll query( int l, int r, int m ){
  m = min( m, r - l + 2 - m );
  
  return (
    aint.query( l, l + m - 2, 1, 1 ) +
    aint.query( l + m - 1, r - m + 1, 0, m ) +
    aint.query( r - m + 2, r, -1, m - 1 )
  );
}

int main(){
  int i, k, q, l, r, m;// n global
  
  n = fgetint();
  aint.init( n );
  k = fgetint();
  for( i = 0 ; i < n ; i++ ){
    v[i] = fgetint();
    aint.update( i, v[i] );
  }
  
  for( q = fgetint() ; q-- ; ){
    switch( fgetint() ){
    case 1:
      for( i = 0 ; i < k ; i++ )
        poz[i] = fgetint() - 1;
      
      update( k );
      break;
    case 2:
      l = fgetint() - 1;
      r = fgetint() - 1;
      m = fgetint();
      
      printf( "%lld\n", query( l, r, m ) );
      break;
    }
  }
  
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 432 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 3 ms 496 KB Output is correct
5 Correct 4 ms 468 KB Output is correct
6 Correct 5 ms 608 KB Output is correct
7 Correct 7 ms 692 KB Output is correct
8 Correct 7 ms 776 KB Output is correct
9 Correct 11 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 22 ms 1652 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 63 ms 4428 KB Output isn't correct
2 Halted 0 ms 0 KB -