結果

問題 No.877 Range ReLU Query
ユーザー LayCurseLayCurse
提出日時 2019-09-21 11:53:09
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 5,155 bytes
コンパイル時間 3,053 ms
コンパイル使用メモリ 219,448 KB
実行使用メモリ 10,776 KB
最終ジャッジ日時 2023-08-08 04:34:01
合計ジャッジ時間 5,373 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,588 KB
testcase_01 AC 2 ms
5,668 KB
testcase_02 AC 3 ms
5,488 KB
testcase_03 AC 2 ms
5,428 KB
testcase_04 AC 2 ms
5,432 KB
testcase_05 AC 2 ms
5,428 KB
testcase_06 AC 2 ms
5,444 KB
testcase_07 AC 2 ms
5,404 KB
testcase_08 AC 2 ms
5,508 KB
testcase_09 AC 2 ms
5,424 KB
testcase_10 AC 2 ms
5,420 KB
testcase_11 AC 42 ms
10,360 KB
testcase_12 AC 38 ms
10,108 KB
testcase_13 AC 31 ms
9,648 KB
testcase_14 AC 31 ms
9,584 KB
testcase_15 AC 46 ms
10,568 KB
testcase_16 AC 44 ms
10,460 KB
testcase_17 AC 44 ms
10,648 KB
testcase_18 AC 44 ms
10,532 KB
testcase_19 AC 41 ms
10,656 KB
testcase_20 AC 44 ms
10,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void *wmem;
char memarr[96000000];
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
template<class T1> void sortA_L(int N, T1 a[], void *mem = wmem){
  sort(a, a+N);
}
template<class T1, class T2> void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem){
  int i;
  pair<T1, T2> *arr;
  walloc1d(&arr, N, &mem);
  for(i=(0);i<(N);i++){
    arr[i].first = a[i];
    arr[i].second = b[i];
  }
  sort(arr, arr+N);
  for(i=(0);i<(N);i++){
    a[i] = arr[i].first;
    b[i] = arr[i].second;
  }
}
template<class T1, class T2, class T3> void sortA_L(int N, T1 a[], T2 b[], T3 c[], void *mem = wmem){
  int i;
  pair<T1, pair<T2, T3> > *arr;
  walloc1d(&arr, N, &mem);
  for(i=(0);i<(N);i++){
    arr[i].first = a[i];
    arr[i].second.first = b[i];
    arr[i].second.second = c[i];
  }
  sort(arr, arr+N);
  for(i=(0);i<(N);i++){
    a[i] = arr[i].first;
    b[i] = arr[i].second.first;
    c[i] = arr[i].second.second;
  }
}
template<class T1, class T2, class T3, class T4> void sortA_L(int N, T1 a[], T2 b[], T3 c[], T4 d[], void *mem = wmem){
  int i;
  pair<pair<T1, T2>, pair<T3, T4> > *arr;
  walloc1d(&arr, N, &mem);
  for(i=(0);i<(N);i++){
    arr[i].first.first = a[i];
    arr[i].first.second = b[i];
    arr[i].second.first = c[i];
    arr[i].second.second = d[i];
  }
  sort(arr, arr+N);
  for(i=(0);i<(N);i++){
    a[i] = arr[i].first.first;
    b[i] = arr[i].first.second;
    c[i] = arr[i].second.first;
    d[i] = arr[i].second.second;
  }
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void wt_L(char a){
  putchar_unlocked(a);
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    putchar_unlocked('-');
  }
  while(s--){
    putchar_unlocked(f[s]+'0');
  }
}
template<class T> struct fenwick{
  int size;
  int memory;
  T *data;
  void malloc(int mem){
    memory = mem;
    data = (T*)std::malloc(sizeof(T)*mem);
  }
  void walloc(int mem, void **workMemory=&wmem){
    memory = mem;
    walloc1d(&data, mem, workMemory);
  }
  void free(void){
    memory = 0;
    free(data);
  }
  void init(int N){
    size = N;
    memset(data,0,sizeof(T)*N);
  }
  void add(int k, T val){
    while(k < size){
      data[k] += val;
      k |= k+1;
    }
  }
  T get(int k){
    T res = 0;
    while(k>=0){
      res += data[k];
      k = (k&(k+1))-1;
    }
    return res;
  }
  T range(int a, int b){
    if(b==-1){
      b=size-1;
    }
    return get(b) - get(a-1);
  }
  int kth(T k){
    int i=0;
    int j=size;
    int c;
    T v;
    while(i<j){
      c = (i+j)/2;
      v = get(c);
      if(v <= k){
        i=c+1;
      }
      else{
        j=c;
      }
    }
    return i==size?-1:i;
  }
}
;
int N;
int Q;
int A[100000];
int L[100000];
int R[100000];
int X[100000];
int ind[100000];
int val[100000];
int qind[100000];
long long res[100000];
int main(){
  wmem = memarr;
  int i;
  int k;
  fenwick<long long> v;
  fenwick<int> c;
  rd(N);
  rd(Q);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);
    }
  }
  {
    int KL2GvlyY;
    for(KL2GvlyY=(0);KL2GvlyY<(Q);KL2GvlyY++){
      rd(ind[KL2GvlyY]);
      rd(L[KL2GvlyY]);L[KL2GvlyY] += (-1);
      rd(R[KL2GvlyY]);R[KL2GvlyY] += (-1);
      rd(X[KL2GvlyY]);
    }
  }
  v.malloc(N);
  v.init(N);
  c.malloc(N);
  c.init(N);
  for(i=(0);i<(N);i++){
    ind[i] = i;
    val[i] = A[i];
  }
  for(i=(0);i<(Q);i++){
    qind[i] = i;
  }
  sortA_L(N, val, ind);
  sortA_L(Q, X, L, R, qind);
  k = N-1;
  for(i=Q-1;i>=0;i--){
    while(k>=0 && val[k] >= X[i]){
      v.add(ind[k],val[k]);
      c.add(ind[k],1);
      k--;
    }
    res[qind[i]] = v.range(L[i], R[i]) - (long long) X[i] * c.range(L[i], R[i]);
  }
  for(i=(0);i<(Q);i++){
    wt_L(res[i]);
    wt_L('\n');
  }
  return 0;
}
// cLay varsion 20190921-1

// --- original code ---
// int N, Q, A[1d5];
// int L[1d5], R[1d5], X[1d5];
// 
// int ind[1d5], val[1d5], qind[1d5];
// ll res[1d5];
// {
//   int i, k;
//   fenwick<ll> v;
//   fenwick<int> c;
// 
//   rd(N,Q,A(N),(ind,L--,R--,X)(Q));
//   v.malloc(N); v.init(N);
//   c.malloc(N); c.init(N);
// 
//   rep(i,N) ind[i] = i, val[i] = A[i];
//   rep(i,Q) qind[i] = i;
// 
//   sortA(N, val, ind);
//   sortA(Q, X, L, R, qind);
// 
//   k = N-1;
//   for(i=Q-1;i>=0;i--){
//     while(k>=0 && val[k] >= X[i]){
//       v.add(ind[k],val[k]);
//       c.add(ind[k],1);
//       k--;
//     }
//     res[qind[i]] = v.range(L[i], R[i]) - (ll) X[i] * c.range(L[i], R[i]);
//   }
// 
//   rep(i,Q) wt(res[i]);
// }
0