結果

問題 No.1609 String Division Machine
ユーザー 👑 NachiaNachia
提出日時 2021-07-16 22:10:06
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 5,251 bytes
コンパイル時間 1,059 ms
コンパイル使用メモリ 88,436 KB
実行使用メモリ 6,484 KB
最終ジャッジ日時 2023-09-20 14:14:58
合計ジャッジ時間 3,268 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,384 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,384 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 43 ms
6,484 KB
testcase_34 AC 57 ms
6,432 KB
testcase_35 AC 50 ms
6,428 KB
testcase_36 AC 39 ms
6,484 KB
testcase_37 AC 59 ms
6,484 KB
testcase_38 AC 31 ms
6,484 KB
testcase_39 AC 5 ms
6,436 KB
testcase_40 AC 66 ms
6,464 KB
testcase_41 AC 31 ms
6,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// Nachia くんだよ

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

template<class S, S op(S,S), S e()>
struct RQ {

  int N;
  vector<S> A;

  void mergev(int i){
    if(i<N) A[i] = op(A[i*2], A[i*2+1]);
  }
  
  RQ(int n = 0){
    N=1; while (N<n) N*=2;
    A.assign(N*2,e());
  }
  RQ(const vector<S>& a) : RQ(a.size()){
    for(int i=0; i<a.size(); i++) A[i+N] = a[i];
    for(int i=N-1; i>=1; i--) mergev(i);
  }

  void set(int p, S x){
    p+=N; A[p] = x;
    for(int d=1; (1<<d)<=N; d++) mergev(p>>d);
  }

  S prod(int l, int r, int a=0, int b=0, int i=-1){
    if(i==-1){ a=0; b=N; i=1; }
    if(r<=a || b<=l) return e();
    if(l<=a && b<=r) return A[i];
    S q1 = prod(l, r, a, (a+b)/2, i*2);
    S q2 = prod(l, r, (a+b)/2, b, i*2+1);
    return op(q1, q2);
  }

  // bool cmp(S)
  template<class E>
  int minleft(int r, E cmp, int a=0, int b=0, int i=-1){
    static S x;
    if(i == -1){ a = 0; b = N; i = 1; x = e(); }
    if(r <= a) return a;
    if(b <= r){
      S nx = op(A[i], x);
      if(cmp(nx)){ x = nx; return a; }
    }
    if(b-a == 1) return b;
    int q = minleft(r, cmp, (a+b)/2, b, i*2+1);
    if(q > (a+b)/2) return q;
    return minleft(r, cmp, a, (a+b)/2, i*2);
  }

  // bool cmp(S)
  template<class E>
  int maxright(int l, E cmp, int a=0, int b=0, int i=-1){
    static S x;
    if(i == -1){ a = 0; b = N; i = 1; x = e(); }
    if(b <= l) return b;
    if(l <= a){
      S nx = op(x, A[i]);
      if(cmp(nx)){ x = nx; return b; }
    }
    if(b-a == 1) return a;
    int q = maxright(l, cmp, a, (a+b)/2, i*2);
    if(q < (a+b)/2) return q;
    return maxright(l, cmp, (a+b)/2, b, i*2+1);
  }
};


struct RQ2 {
  struct S{ int a; };
  struct F{ int x; };
  static S op(S l, S r){ return { min(l.a, r.a) }; }
  static S e(){ return { 1001001001 }; }
  static void mapping(F f, S& x){ x.a = min(f.x,x.a); }
  static void compose(F f, F& x){ x.x = min(f.x,x.x); }
  static F id(){ return { 1001001001 }; }

  struct Node{ S s; F f; };
  int N;
  vector<Node> A;

  void mapf(int i, F f){
    compose(f, A[i].f);
    mapping(f, A[i].s);
  }
  void mergev(int i){
    if(i<N) A[i].s = op(A[i*2].s, A[i*2+1].s);
    mapping(A[i].f, A[i].s);
  }
  void spread(int i){
    if(i<N){
      mapf(i*2, A[i].f);
      mapf(i*2+1, A[i].f);
    }
    A[i].f = id();
  }
  
  RQ2(int n = 0){
    N=1; while (N<n) N*=2;
    A.assign(N*2,{ e(), id() });
  }
  RQ2(const vector<S>& a) : RQ2(a.size()){
    for(int i=0; i<a.size(); i++) A[i+N].s = a[i];
    for(int i=N-1; i>=1; i--) mergev(i);
  }

  void set(int p, S x){
    for(int d=1; (1<<d)<=N; d++) spread(p>>d);
    p+=N; A[p].s = x;
    for(int d=1; (1<<d)<=N; d++) mergev(p>>d);
  }

  void apply(int l, int r, F x, int a=0, int b=0, int i=-1){
    if(i==-1){ a=0; b=N; i=1; }
    if(r<=a || b<=l) return;
    if(l<=a && b<=r){ mapf(i, x); return; }
    spread(i);
    apply(l, r, x, a, (a+b)/2, i*2);
    apply(l, r, x, (a+b)/2, b, i*2+1);
    A[i].s = op(A[i*2].s, A[i*2+1].s);
  }

  S prod(int l, int r, int a=0, int b=0, int i=-1){
    if(i==-1){ a=0; b=N; i=1; }
    if(r<=a || b<=l) return e();
    if(l<=a && b<=r) return A[i].s;
    spread(i);
    S q1 = prod(l, r, a, (a+b)/2, i*2);
    S q2 = prod(l, r, (a+b)/2, b, i*2+1);
    return op(q1, q2);
  }

  // bool cmp(S)
  template<class E>
  int minleft(int r, E cmp, int a=0, int b=0, int i=-1){
    static S x;
    if(i == -1){ a = 0; b = N; i = 1; x = e(); }
    if(r <= a) return a;
    if(b <= r){
      S nx = op(A[i].s, x);
      if(cmp(nx)){ x = nx; return a; }
    }
    if(b-a == 1) return b;
    spread(i);
    int q = minleft(r, cmp, (a+b)/2, b, i*2+1);
    if(q > (a+b)/2) return q;
    return minleft(r, cmp, a, (a+b)/2, i*2);
  }

  // bool cmp(S)
  template<class E>
  int maxright(int l, E cmp, int a=0, int b=0, int i=-1){
    static S x;
    if(i == -1){ a = 0; b = N; i = 1; x = e(); }
    if(b <= l) return b;
    if(l <= a){
      S nx = op(x, A[i].s);
      if(cmp(nx)){ x = nx; return b; }
    }
    if(b-a == 1) return a;
    spread(i);
    int q = maxright(l, cmp, a, (a+b)/2, i*2);
    if(q < (a+b)/2) return q;
    return maxright(l, cmp, (a+b)/2, b, i*2+1);
  }
};


int RmQe(){ return 1001001001; }
int RmQop(int l,int r){ return min(l,r); }
using RmQ = RQ2; //<int,RmQop,RmQe>;
int RMQe(){ return -1001001001; }
int RMQop(int l,int r){ return max(l,r); }
using RMQ = RQ<int,RMQop,RMQe>;


int main() {
  const int X = 200000;
  string S; cin >> S;
  int N = S.size();
  RmQ ans(N);
  RMQ G(N);
  rep(i,N) if(S[i] != '?') G.set(i,S[i]*X+X-i-1);
  ans.apply(0,N,{(int)'z'});
  ans.set(0,{(int)'z'});

  if(G.prod(0,N) < 0){ cout << string(N,'a') << "\n"; return 0; }
  
  while(G.prod(0,N) >= 0){
    int p = 0;
    while(true){
      int x = G.prod(p,N);
      if(x < 0) break;
      int i = X - x % X - 1;
      int c = x / X;
      G.set(i,-1);
      ans.apply(p,i,{c});
      p = i;
    }
    ans.apply(p+1,N,{'a'});
  }

  rep(i,N) if(S[i] == '?') S[i] = ans.prod(i,i+1).a;
  cout << S << "\n";
  return 0;
}


struct ios_do_not_sync{
  ios_do_not_sync(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
  }
} ios_do_not_sync_instance;


0