結果

問題 No.1240 Or Sum of Xor Pair
ユーザー LayCurseLayCurse
提出日時 2020-10-02 20:27:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 267 ms / 2,000 ms
コード長 5,427 bytes
コンパイル時間 3,082 ms
コンパイル使用メモリ 216,812 KB
実行使用メモリ 10,080 KB
最終ジャッジ日時 2023-09-23 02:55:04
合計ジャッジ時間 9,470 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,364 KB
testcase_01 AC 2 ms
5,644 KB
testcase_02 AC 2 ms
5,468 KB
testcase_03 AC 5 ms
5,436 KB
testcase_04 AC 5 ms
5,520 KB
testcase_05 AC 5 ms
5,492 KB
testcase_06 AC 5 ms
5,416 KB
testcase_07 AC 4 ms
5,424 KB
testcase_08 AC 4 ms
5,428 KB
testcase_09 AC 5 ms
5,476 KB
testcase_10 AC 24 ms
5,692 KB
testcase_11 AC 24 ms
5,804 KB
testcase_12 AC 49 ms
5,920 KB
testcase_13 AC 43 ms
6,076 KB
testcase_14 AC 42 ms
5,968 KB
testcase_15 AC 266 ms
9,988 KB
testcase_16 AC 249 ms
10,072 KB
testcase_17 AC 238 ms
10,080 KB
testcase_18 AC 249 ms
10,008 KB
testcase_19 AC 239 ms
10,008 KB
testcase_20 AC 267 ms
10,004 KB
testcase_21 AC 260 ms
10,020 KB
testcase_22 AC 261 ms
10,068 KB
testcase_23 AC 228 ms
10,068 KB
testcase_24 AC 255 ms
10,004 KB
testcase_25 AC 210 ms
9,988 KB
testcase_26 AC 260 ms
10,000 KB
testcase_27 AC 2 ms
5,388 KB
testcase_28 AC 2 ms
5,388 KB
testcase_29 AC 23 ms
10,004 KB
testcase_30 AC 39 ms
8,756 KB
testcase_31 AC 104 ms
9,156 KB
testcase_32 AC 116 ms
10,008 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;
  }
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_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){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
int N;
int X;
int A[200000];
int v[200000];
long long res;
long long solve(int n, int a[], int m, int b[], int bt){
  int i;
  int j;
  long long n1;
  long long n2;
  long long m1;
  long long m2;
  long long c1;
  long long c2;
  long long res = 0;
  if(bt == -1 || n == 0 || m == 0){
    return res;
  }
  for(i=(0);i<(n);i++){
    v[i] = a[i] % (1<<(bt+1));
  }
  sortA_L(n,v,a);
  for(i=(0);i<(m);i++){
    v[i] = b[i] % (1<<(bt+1));
  }
  sortA_L(m,v,b);
  for(i=(0);i<(n);i++){
    if(((a[i]) &(1<<(bt)))){
      break;
    }
  }
  n1 = i;
  n2 = n - i;
  for(i=(0);i<(m);i++){
    if(((b[i]) &(1<<(bt)))){
      break;
    }
  }
  m1 = i;
  m2 = m - i;
  if(((X) &(1<<(bt)))){
    int k;
    for(k=(0);k<(18);k++){
      c1 = c2 = 0;
      for(i=(0);i<(n1);i++){
        if(!((a[i]) &(1<<(k)))){
          c1++;
        }
      }
      for(i=(0);i<(m1);i++){
        if(!((b[i]) &(1<<(k)))){
          c2++;
        }
      }
      res += (n1 * m1 - c1 * c2) *(1<<(k));
    }
    for(k=(0);k<(18);k++){
      c1 = c2 = 0;
      for(i=(0);i<(n2);i++){
        if(!((a[n1+i]) &(1<<(k)))){
          c1++;
        }
      }
      for(i=(0);i<(m2);i++){
        if(!((b[m1+i]) &(1<<(k)))){
          c2++;
        }
      }
      res += (n2 * m2 - c1 * c2) *(1<<(k));
    }
    res += solve(n1, a, m2, b+m1, bt-1);
    res += solve(n2, a+n1, m1, b, bt-1);
  }
  else{
    res += solve(n1, a, m1, b, bt-1);
    res += solve(n2, a+n1, m2, b+m1, bt-1);
  }
  return res;
}
int main(){
  int i;
  wmem = memarr;
  int*a;
  int*b;
  long long res;
  rd(N);
  rd(X);
  {
    int ZIeRIny5;
    for(ZIeRIny5=(0);ZIeRIny5<(N);ZIeRIny5++){
      rd(A[ZIeRIny5]);
    }
  }
  walloc1d(&a,N);
  walloc1d(&b,N);
  for(i=(0);i<(N);i++){
    a[i] = b[i] = A[i];
  }
  res = solve(N,a,N,b,18);
  for(i=(0);i<(N);i++){
    res -= A[i];
  }
  res /= 2;
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20200926-1

// --- original code ---
// int N, X, A[2d5], v[2d5];
// ll res;
// 
// ll solve(int n, int a[], int m, int b[], int bt){
//   int i, j; ll n1, n2, m1, m2, c1, c2;
//   ll res = 0;
// 
//   if(bt == -1 || n == 0 || m == 0) return res;
// 
//   rep(i,n) v[i] = a[i] % (1<<(bt+1));
//   sortA(n,v,a);
//   rep(i,m) v[i] = b[i] % (1<<(bt+1));
//   sortA(m,v,b);
// 
//   rep(i,n) if(BIT_ith(a[i],bt)) break;
//   n1 = i;
//   n2 = n - i;
// 
//   rep(i,m) if(BIT_ith(b[i],bt)) break;
//   m1 = i;
//   m2 = m - i;
// 
//   if(BIT_ith(X, bt)){
//     rep(k,18){
//       c1 = c2 = 0;
//       rep(i,n1) if(!BIT_ith(a[i],k)) c1++;
//       rep(i,m1) if(!BIT_ith(b[i],k)) c2++;
//       res += (n1 * m1 - c1 * c2) * BIT_ith(k);
//     }
//     rep(k,18){
//       c1 = c2 = 0;
//       rep(i,n2) if(!BIT_ith(a[n1+i],k)) c1++;
//       rep(i,m2) if(!BIT_ith(b[m1+i],k)) c2++;
//       res += (n2 * m2 - c1 * c2) * BIT_ith(k);
//     }
//     res += solve(n1, a, m2, b+m1, bt-1);
//     res += solve(n2, a+n1, m1, b, bt-1);
//   } else {
//     res += solve(n1, a, m1, b, bt-1);
//     res += solve(n2, a+n1, m2, b+m1, bt-1);
//   }
// 
//   return res;
// }
// 
// {
//   int *a, *b;
//   ll res;
//   rd(N,X,A(N));
//   walloc1d(&a,N);
//   walloc1d(&b,N);
//   rep(i,N) a[i] = b[i] = A[i];
//   res = solve(N,a,N,b,18);
//   rep(i,N) res -= A[i];
//   res /= 2;
//   wt(res);
// }
0