This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// 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 += ((long long)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]] );
for( i = 0 ; i < k ; i++ )
v[poz[i]] = updated[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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |