結果

問題 No.1568 Sushi
ユーザー SSRSSSRS
提出日時 2021-06-26 14:54:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,725 bytes
コンパイル時間 1,964 ms
コンパイル使用メモリ 179,036 KB
実行使用メモリ 29,956 KB
最終ジャッジ日時 2024-06-25 10:41:24
合計ジャッジ時間 54,562 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 TLE -
testcase_28 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long INF = 100000000000000000;
struct monoid{
  bool id;
  long long mn, mx, inc, dec;
  monoid(){
    id = true;
  }
  monoid(long long L, long long R){
    id = false;
    mn = min(L, R);
    mx = max(L, R);
    inc = max(R - L, (long long) 0);
    dec = min(R - L, (long long) 0);
  }
};
monoid f(monoid L, monoid R){
  if (L.id){
    return R;
  }
  if (R.id){
    return L;
  }
  monoid ans;
  ans.id = false;
  ans.mn = min(L.mn, R.mn);
  ans.mx = max(L.mx, R.mx);
  ans.inc = max({L.inc, R.inc, R.mx - L.mn});
  ans.dec = min({L.dec, R.dec, R.mn - L.mx});
  return ans;
}
struct segment_tree{
  int N;
  vector<long long> A;
  vector<long long> S;
  vector<monoid> ST;
  segment_tree(vector<long long> A2): A(A2){
    A.push_back(INF);
    int N2 = A.size();
    S = vector<long long>(N2 + 1);
    long long P = 0;
    S[0] = 0;
    for (int i = 0; i < N2; i++){
      if (i % 2 == 0){
        S[i + 1] = S[i] + (A[i] - P);
      } else {
        S[i + 1] = S[i] - (A[i] - P);
      }
      P = A[i];
    }
    N = 1;
    while (N < N2){
      N *= 2;
    }
    ST = vector<monoid>(N * 2 - 1);
    for (int i = 0; i < N2; i++){
      ST[N - 1 + i] = monoid(S[i], S[i + 1]);
    }
    for (int i = N - 2; i >= 0; i--){
      ST[i] = f(ST[i * 2 + 1], ST[i * 2 + 2]);
    }
  }
  monoid range_fold(int L, int R, int i, int l, int r){
    if (r <= L || R <= l){
      return monoid();
    } else if (L <= l && r <= R){
      return ST[i];
    } else {
      int m = (l + r) / 2;
      return f(range_fold(L, R, i * 2 + 1, l, m), range_fold(L, R, i * 2 + 2, m, r));
    }
  }
  monoid range_fold(int L, int R){
    return range_fold(L, R, 0, 0, N);
  }
  monoid query(long long tL, long long tR){
    int L = lower_bound(A.begin(), A.end(), tL) - A.begin();
    int R = lower_bound(A.begin(), A.end(), tR) - A.begin() - 1;
    if (R == L - 1){
      if (L % 2 == 0){
        return monoid(S[L + 1] - (A[L] - tL), S[L + 1] - (A[L] - tR));
      } else {
        return monoid(S[L + 1] + (A[L] - tL), S[L + 1] + (A[L] - tR));
      }
    }
    monoid l;
    if (L % 2 == 0){
      l = monoid(S[L + 1] - (A[L] - tL), S[L + 1]);
    } else {
      l = monoid(S[L + 1] + (A[L] - tL), S[L + 1]);
    }
    monoid m = range_fold(L + 1, R + 1, 0, 0, N);
    monoid r;
    if (R % 2 == 0){
      r = monoid(S[R + 1], S[R + 1] - (tR - A[R]));
    } else {
      r = monoid(S[R + 1], S[R + 1] + (tR - A[R]));
    }
    monoid ans = f(f(l, m), r);
    return ans;
  }
};
int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  long long L;
  int N, Q;
  cin >> L >> N >> Q;
  vector<long long> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
  }
  segment_tree ST(A);
  long long D = 0;
  for (int i = 0; i < Q; i++){
    long long B, C;
    cin >> B >> C;
    long long x, t;
    if (i == 0){
      x = B;
      t = C;
    } else {
      x = (B + D) % L;
      t = (C + D) % 1000000000000000;
    }
    int l = lower_bound(A.begin(), A.end(), t) - A.begin();
    monoid M = ST.query(t, ST.A[l]);
    if (M.inc >= x || M.dec <= -L + x){
      long long d = max(M.inc - x, (-L + x) - M.dec);
      D = ST.A[l] - d;
    } else {
      int fv = l, tv = N;
      while (tv - fv > 1){
        int mid = (tv + fv) / 2;
        M = ST.range_fold(l + 1, mid + 1);
        if (M.inc >= x || M.dec <= -L + x){
          tv = mid;
          long long d = max(M.inc - x, (-L + x) - M.dec);
          D = ST.A[mid] - d;
        } else {
          fv = mid;
        }
      }
      if (tv == N){
        M = ST.query(t, INF);
        long long d = max(M.inc - x, (-L + x) - M.dec);
        D = INF - d;
      }
    }
    D -= t;
    assert(D >= 0);
    cout << D << "\n";
  }
}
0