結果

問題 No.5010 Better Mo's Algorithm is Needed!! (Unweighted)
ユーザー chro_96chro_96
提出日時 2022-12-17 23:12:41
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 118 ms / 5,000 ms
コード長 9,007 bytes
コンパイル時間 1,520 ms
実行使用メモリ 8,192 KB
スコア 58,964,548
最終ジャッジ日時 2022-12-17 23:19:21
合計ジャッジ時間 46,881 ms
ジャッジサーバーID
(参考情報)
judge16 / judge11
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 104 ms
8,092 KB
testcase_01 AC 104 ms
8,020 KB
testcase_02 AC 107 ms
8,100 KB
testcase_03 AC 101 ms
8,172 KB
testcase_04 AC 118 ms
8,092 KB
testcase_05 AC 98 ms
8,112 KB
testcase_06 AC 100 ms
8,092 KB
testcase_07 AC 99 ms
8,024 KB
testcase_08 AC 105 ms
8,172 KB
testcase_09 AC 100 ms
8,100 KB
testcase_10 AC 99 ms
8,096 KB
testcase_11 AC 98 ms
8,076 KB
testcase_12 AC 102 ms
8,124 KB
testcase_13 AC 102 ms
8,148 KB
testcase_14 AC 98 ms
8,096 KB
testcase_15 AC 101 ms
8,032 KB
testcase_16 AC 100 ms
8,160 KB
testcase_17 AC 101 ms
8,056 KB
testcase_18 AC 103 ms
8,156 KB
testcase_19 AC 99 ms
8,096 KB
testcase_20 AC 101 ms
8,128 KB
testcase_21 AC 100 ms
8,016 KB
testcase_22 AC 105 ms
8,156 KB
testcase_23 AC 102 ms
8,096 KB
testcase_24 AC 101 ms
8,020 KB
testcase_25 AC 101 ms
8,096 KB
testcase_26 AC 100 ms
8,160 KB
testcase_27 AC 100 ms
8,104 KB
testcase_28 AC 99 ms
8,096 KB
testcase_29 AC 100 ms
8,100 KB
testcase_30 AC 104 ms
8,052 KB
testcase_31 AC 103 ms
8,152 KB
testcase_32 AC 98 ms
8,032 KB
testcase_33 AC 104 ms
8,096 KB
testcase_34 AC 100 ms
8,052 KB
testcase_35 AC 106 ms
8,096 KB
testcase_36 AC 99 ms
8,156 KB
testcase_37 AC 100 ms
8,056 KB
testcase_38 AC 99 ms
8,148 KB
testcase_39 AC 102 ms
8,028 KB
testcase_40 AC 110 ms
8,024 KB
testcase_41 AC 99 ms
8,092 KB
testcase_42 AC 101 ms
8,184 KB
testcase_43 AC 100 ms
8,156 KB
testcase_44 AC 102 ms
8,152 KB
testcase_45 AC 102 ms
8,096 KB
testcase_46 AC 98 ms
8,188 KB
testcase_47 AC 98 ms
8,024 KB
testcase_48 AC 98 ms
8,056 KB
testcase_49 AC 101 ms
8,020 KB
testcase_50 AC 100 ms
8,164 KB
testcase_51 AC 97 ms
8,092 KB
testcase_52 AC 99 ms
8,096 KB
testcase_53 AC 107 ms
8,096 KB
testcase_54 AC 103 ms
8,096 KB
testcase_55 AC 96 ms
8,100 KB
testcase_56 AC 98 ms
8,108 KB
testcase_57 AC 97 ms
8,044 KB
testcase_58 AC 105 ms
8,152 KB
testcase_59 AC 101 ms
8,092 KB
testcase_60 AC 97 ms
8,024 KB
testcase_61 AC 97 ms
8,100 KB
testcase_62 AC 99 ms
8,184 KB
testcase_63 AC 106 ms
8,156 KB
testcase_64 AC 101 ms
8,020 KB
testcase_65 AC 98 ms
8,020 KB
testcase_66 AC 100 ms
8,024 KB
testcase_67 AC 100 ms
8,024 KB
testcase_68 AC 107 ms
8,024 KB
testcase_69 AC 98 ms
8,096 KB
testcase_70 AC 101 ms
8,096 KB
testcase_71 AC 98 ms
8,060 KB
testcase_72 AC 99 ms
8,096 KB
testcase_73 AC 97 ms
8,152 KB
testcase_74 AC 99 ms
8,104 KB
testcase_75 AC 99 ms
8,052 KB
testcase_76 AC 98 ms
8,108 KB
testcase_77 AC 102 ms
8,184 KB
testcase_78 AC 99 ms
8,096 KB
testcase_79 AC 98 ms
8,020 KB
testcase_80 AC 99 ms
8,192 KB
testcase_81 AC 101 ms
8,128 KB
testcase_82 AC 107 ms
8,104 KB
testcase_83 AC 97 ms
8,128 KB
testcase_84 AC 96 ms
8,092 KB
testcase_85 AC 98 ms
8,024 KB
testcase_86 AC 100 ms
8,088 KB
testcase_87 AC 116 ms
8,128 KB
testcase_88 AC 98 ms
8,096 KB
testcase_89 AC 98 ms
8,152 KB
testcase_90 AC 99 ms
8,096 KB
testcase_91 AC 100 ms
8,048 KB
testcase_92 AC 102 ms
8,108 KB
testcase_93 AC 98 ms
8,096 KB
testcase_94 AC 98 ms
8,128 KB
testcase_95 AC 99 ms
8,052 KB
testcase_96 AC 113 ms
8,056 KB
testcase_97 AC 100 ms
8,156 KB
testcase_98 AC 99 ms
8,020 KB
testcase_99 AC 101 ms
8,048 KB
testcase_100 AC 101 ms
8,060 KB
testcase_101 AC 114 ms
8,028 KB
testcase_102 AC 101 ms
8,156 KB
testcase_103 AC 100 ms
8,028 KB
testcase_104 AC 101 ms
8,156 KB
testcase_105 AC 98 ms
8,096 KB
testcase_106 AC 99 ms
8,152 KB
testcase_107 AC 98 ms
8,028 KB
testcase_108 AC 98 ms
8,092 KB
testcase_109 AC 98 ms
8,100 KB
testcase_110 AC 109 ms
8,088 KB
testcase_111 AC 99 ms
8,092 KB
testcase_112 AC 101 ms
8,156 KB
testcase_113 AC 99 ms
8,152 KB
testcase_114 AC 96 ms
8,100 KB
testcase_115 AC 104 ms
8,124 KB
testcase_116 AC 102 ms
8,124 KB
testcase_117 AC 98 ms
8,100 KB
testcase_118 AC 101 ms
8,052 KB
testcase_119 AC 100 ms
8,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>

int main () {
  int n = 0;
  int q = 0;
  int wt = 0;
  int st = 0;
  long long w[200000] = {};
  int l[200000] = {};
  int r[200000] = {};
  
  int res = 0;
  
  int ans[200000] = {};
  long long score = 0LL;
  
  int work[200000] = {};
  long long work_score = 0LL;
  
  long long sum_w[200001] = {};
  
  int t = 1000;
  
  res = scanf("%d", &n);
  res = scanf("%d", &q);
  res = scanf("%d", &wt);
  res = scanf("%d", &st);
  for (int i = 0; i < n; i++) {
    res = scanf("%lld", w+i);
    sum_w[i+1] = sum_w[i]+w[i];
  }
  for (int i = 0; i < q; i++) {
    res = scanf("%d", l+i);
    res = scanf("%d", r+i);
    l[i]--;
    r[i]--;
  }
  
  srand(1);
  
  score = sum_w[r[0]+1]-sum_w[l[0]];
  for (int i = 1; i < q; i++) {
    ans[i] = i;
    work[i] = i;
    if (l[i-1] < l[i]) {
      score += sum_w[l[i]]-sum_w[l[i-1]];
    } else {
      score += sum_w[l[i-1]]-sum_w[l[i]];
    }
    if (r[i-1] < r[i]) {
      score += sum_w[r[i]+1]-sum_w[r[i-1]+1];
    } else {
      score += sum_w[r[i-1]+1]-sum_w[r[i]+1];
    }
  }
  work_score = score;
  
  while (t > 0) {
    int idx1 = rand()%q;
    int idx2 = rand()%q;
    long long tmp_score = work_score;
    while (idx1 == idx2) {
      idx2 = rand()%q;
    }
    if (idx1 > idx2) {
      int swap = idx2;
      idx2 = idx1;
      idx1 = swap;
    }
    if (idx1+1 == idx2) {
      if (idx1 <= 0) {
        tmp_score -= sum_w[r[work[idx1]]+1]-sum_w[l[work[idx1]]];
        tmp_score += sum_w[r[work[idx2]]+1]-sum_w[l[work[idx2]]];
        if (l[work[idx1]] < l[work[idx2+1]]) {
          tmp_score += sum_w[l[work[idx2+1]]]-sum_w[l[work[idx1]]];
        } else {
          tmp_score += sum_w[l[work[idx1]]]-sum_w[l[work[idx2+1]]];
        }
        if (r[work[idx1]] < r[work[idx2+1]]) {
          tmp_score += sum_w[r[work[idx2+1]]+1]-sum_w[r[work[idx1]]+1];
        } else {
          tmp_score += sum_w[r[work[idx1]]+1]-sum_w[r[work[idx2+1]]+1];
        }
        if (l[work[idx2]] < l[work[idx2+1]]) {
          tmp_score -= sum_w[l[work[idx2+1]]]-sum_w[l[work[idx2]]];
        } else {
          tmp_score -= sum_w[l[work[idx2]]]-sum_w[l[work[idx2+1]]];
        }
        if (r[work[idx2]] < r[work[idx2+1]]) {
          tmp_score -= sum_w[r[work[idx2+1]]+1]-sum_w[r[work[idx2]]+1];
        } else {
          tmp_score -= sum_w[r[work[idx2]]+1]-sum_w[r[work[idx2+1]]+1];
        }
      } else if (idx2+1 == q) {
        if (l[work[idx1]] < l[work[idx1-1]]) {
          tmp_score -= sum_w[l[work[idx1-1]]]-sum_w[l[work[idx1]]];
        } else {
          tmp_score -= sum_w[l[work[idx1]]]-sum_w[l[work[idx1-1]]];
        }
        if (r[work[idx1]] < r[work[idx1-1]]) {
          tmp_score -= sum_w[r[work[idx1-1]]+1]-sum_w[r[work[idx1]]+1];
        } else {
          tmp_score -= sum_w[r[work[idx1]]+1]-sum_w[r[work[idx1-1]]+1];
        }
        if (l[work[idx2]] < l[work[idx1-1]]) {
          tmp_score += sum_w[l[work[idx1-1]]]-sum_w[l[work[idx2]]];
        } else {
          tmp_score += sum_w[l[work[idx2]]]-sum_w[l[work[idx1-1]]];
        }
        if (r[work[idx2]] < r[work[idx1-1]]) {
          tmp_score += sum_w[r[work[idx1-1]]+1]-sum_w[r[work[idx2]]+1];
        } else {
          tmp_score += sum_w[r[work[idx2]]+1]-sum_w[r[work[idx1-1]]+1];
        }
      } else {
        if (l[work[idx1]] < l[work[idx1-1]]) {
          tmp_score -= sum_w[l[work[idx1-1]]]-sum_w[l[work[idx1]]];
        } else {
          tmp_score -= sum_w[l[work[idx1]]]-sum_w[l[work[idx1-1]]];
        }
        if (r[work[idx1]] < r[work[idx1-1]]) {
          tmp_score -= sum_w[r[work[idx1-1]]+1]-sum_w[r[work[idx1]]+1];
        } else {
          tmp_score -= sum_w[r[work[idx1]]+1]-sum_w[r[work[idx1-1]]+1];
        }
        if (l[work[idx2]] < l[work[idx1-1]]) {
          tmp_score += sum_w[l[work[idx1-1]]]-sum_w[l[work[idx2]]];
        } else {
          tmp_score += sum_w[l[work[idx2]]]-sum_w[l[work[idx1-1]]];
        }
        if (r[work[idx2]] < r[work[idx1-1]]) {
          tmp_score += sum_w[r[work[idx1-1]]+1]-sum_w[r[work[idx2]]+1];
        } else {
          tmp_score += sum_w[r[work[idx2]]+1]-sum_w[r[work[idx1-1]]+1];
        }
        if (l[work[idx1]] < l[work[idx2+1]]) {
          tmp_score += sum_w[l[work[idx2+1]]]-sum_w[l[work[idx1]]];
        } else {
          tmp_score += sum_w[l[work[idx1]]]-sum_w[l[work[idx2+1]]];
        }
        if (r[work[idx1]] < r[work[idx2+1]]) {
          tmp_score += sum_w[r[work[idx2+1]]+1]-sum_w[r[work[idx1]]+1];
        } else {
          tmp_score += sum_w[r[work[idx1]]+1]-sum_w[r[work[idx2+1]]+1];
        }
        if (l[work[idx2]] < l[work[idx2+1]]) {
          tmp_score -= sum_w[l[work[idx2+1]]]-sum_w[l[work[idx2]]];
        } else {
          tmp_score -= sum_w[l[work[idx2]]]-sum_w[l[work[idx2+1]]];
        }
        if (r[work[idx2]] < r[work[idx2+1]]) {
          tmp_score -= sum_w[r[work[idx2+1]]+1]-sum_w[r[work[idx2]]+1];
        } else {
          tmp_score -= sum_w[r[work[idx2]]+1]-sum_w[r[work[idx2+1]]+1];
        }
      }
    } else {
      if (idx1 <= 0) {
        tmp_score -= sum_w[r[work[idx1]]+1]-sum_w[l[work[idx1]]];
        tmp_score += sum_w[r[work[idx2]]+1]-sum_w[l[work[idx2]]];
      } else {
        if (l[work[idx1]] < l[work[idx1-1]]) {
          tmp_score -= sum_w[l[work[idx1-1]]]-sum_w[l[work[idx1]]];
        } else {
          tmp_score -= sum_w[l[work[idx1]]]-sum_w[l[work[idx1-1]]];
        }
        if (r[work[idx1]] < r[work[idx1-1]]) {
          tmp_score -= sum_w[r[work[idx1-1]]+1]-sum_w[r[work[idx1]]+1];
        } else {
          tmp_score -= sum_w[r[work[idx1]]+1]-sum_w[r[work[idx1-1]]+1];
        }
        if (l[work[idx2]] < l[work[idx1-1]]) {
          tmp_score += sum_w[l[work[idx1-1]]]-sum_w[l[work[idx2]]];
        } else {
          tmp_score += sum_w[l[work[idx2]]]-sum_w[l[work[idx1-1]]];
        }
        if (r[work[idx2]] < r[work[idx1-1]]) {
          tmp_score += sum_w[r[work[idx1-1]]+1]-sum_w[r[work[idx2]]+1];
        } else {
          tmp_score += sum_w[r[work[idx2]]+1]-sum_w[r[work[idx1-1]]+1];
        }
      }
      if (idx2+1 != q) {
        if (l[work[idx1]] < l[work[idx2+1]]) {
          tmp_score += sum_w[l[work[idx2+1]]]-sum_w[l[work[idx1]]];
        } else {
          tmp_score += sum_w[l[work[idx1]]]-sum_w[l[work[idx2+1]]];
        }
        if (r[work[idx1]] < r[work[idx2+1]]) {
          tmp_score += sum_w[r[work[idx2+1]]+1]-sum_w[r[work[idx1]]+1];
        } else {
          tmp_score += sum_w[r[work[idx1]]+1]-sum_w[r[work[idx2+1]]+1];
        }
        if (l[work[idx2]] < l[work[idx2+1]]) {
          tmp_score -= sum_w[l[work[idx2+1]]]-sum_w[l[work[idx2]]];
        } else {
          tmp_score -= sum_w[l[work[idx2]]]-sum_w[l[work[idx2+1]]];
        }
        if (r[work[idx2]] < r[work[idx2+1]]) {
          tmp_score -= sum_w[r[work[idx2+1]]+1]-sum_w[r[work[idx2]]+1];
        } else {
          tmp_score -= sum_w[r[work[idx2]]+1]-sum_w[r[work[idx2+1]]+1];
        }
      }
      if (l[work[idx1]] < l[work[idx1+1]]) {
        tmp_score -= sum_w[l[work[idx1+1]]]-sum_w[l[work[idx1]]];
      } else {
        tmp_score -= sum_w[l[work[idx1]]]-sum_w[l[work[idx1+1]]];
      }
      if (r[work[idx1]] < r[work[idx1+1]]) {
        tmp_score -= sum_w[r[work[idx1+1]]+1]-sum_w[r[work[idx1]]+1];
      } else {
        tmp_score -= sum_w[r[work[idx1]]+1]-sum_w[r[work[idx1+1]]+1];
      }
      if (l[work[idx2]] < l[work[idx1+1]]) {
        tmp_score += sum_w[l[work[idx1+1]]]-sum_w[l[work[idx2]]];
      } else {
        tmp_score += sum_w[l[work[idx2]]]-sum_w[l[work[idx1+1]]];
      }
      if (r[work[idx2]] < r[work[idx1+1]]) {
        tmp_score += sum_w[r[work[idx1+1]]+1]-sum_w[r[work[idx2]]+1];
      } else {
        tmp_score += sum_w[r[work[idx2]]+1]-sum_w[r[work[idx1+1]]+1];
      }
      if (l[work[idx1]] < l[work[idx2-1]]) {
        tmp_score += sum_w[l[work[idx2-1]]]-sum_w[l[work[idx1]]];
      } else {
        tmp_score += sum_w[l[work[idx1]]]-sum_w[l[work[idx2-1]]];
      }
      if (r[work[idx1]] < r[work[idx2-1]]) {
        tmp_score += sum_w[r[work[idx2-1]]+1]-sum_w[r[work[idx1]]+1];
      } else {
        tmp_score += sum_w[r[work[idx1]]+1]-sum_w[r[work[idx2-1]]+1];
      }
      if (l[work[idx2]] < l[work[idx2-1]]) {
        tmp_score -= sum_w[l[work[idx2-1]]]-sum_w[l[work[idx2]]];
      } else {
        tmp_score -= sum_w[l[work[idx2]]]-sum_w[l[work[idx2-1]]];
      }
      if (r[work[idx2]] < r[work[idx2-1]]) {
        tmp_score -= sum_w[r[work[idx2-1]]+1]-sum_w[r[work[idx2]]+1];
      } else {
        tmp_score -= sum_w[r[work[idx2]]+1]-sum_w[r[work[idx2-1]]+1];
      }
    }
    if (tmp_score < work_score) {
      int swap = work[idx1];
      work[idx1] = work[idx2];
      work[idx2] = swap;
      work_score = tmp_score;
    }
    if (work_score < score) {
      score = work_score;
      for (int i = 0; i < q; i++) {
        ans[i] = work[i];
      }
    }
    t--;
  }
  
  printf("%d", ans[0]+1);
  for (int i = 1; i < q; i++) {
    printf(" %d", ans[i]+1);
  }
  printf("\n");
  
  return 0;
}
0