結果

問題 No.1508 Avoid being hit
ユーザー どららどらら
提出日時 2021-05-16 08:01:17
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 227 ms / 3,000 ms
コード長 3,154 bytes
コンパイル時間 4,397 ms
コンパイル使用メモリ 231,800 KB
実行使用メモリ 25,364 KB
最終ジャッジ日時 2024-04-15 01:38:04
合計ジャッジ時間 10,863 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 173 ms
14,128 KB
testcase_01 AC 4 ms
8,184 KB
testcase_02 AC 4 ms
8,220 KB
testcase_03 AC 4 ms
8,156 KB
testcase_04 AC 5 ms
8,256 KB
testcase_05 AC 4 ms
8,236 KB
testcase_06 AC 4 ms
8,332 KB
testcase_07 AC 4 ms
8,308 KB
testcase_08 AC 5 ms
8,188 KB
testcase_09 AC 5 ms
8,324 KB
testcase_10 AC 5 ms
8,124 KB
testcase_11 AC 4 ms
8,128 KB
testcase_12 AC 5 ms
8,124 KB
testcase_13 AC 6 ms
8,128 KB
testcase_14 AC 5 ms
8,232 KB
testcase_15 AC 5 ms
8,240 KB
testcase_16 AC 63 ms
13,916 KB
testcase_17 AC 38 ms
11,384 KB
testcase_18 AC 34 ms
10,696 KB
testcase_19 AC 54 ms
13,060 KB
testcase_20 AC 81 ms
14,760 KB
testcase_21 AC 56 ms
12,600 KB
testcase_22 AC 68 ms
13,680 KB
testcase_23 AC 74 ms
14,028 KB
testcase_24 AC 216 ms
23,516 KB
testcase_25 AC 199 ms
22,240 KB
testcase_26 AC 18 ms
9,096 KB
testcase_27 AC 102 ms
15,236 KB
testcase_28 AC 104 ms
15,244 KB
testcase_29 AC 202 ms
22,528 KB
testcase_30 AC 111 ms
15,804 KB
testcase_31 AC 181 ms
20,756 KB
testcase_32 AC 197 ms
21,788 KB
testcase_33 AC 54 ms
11,840 KB
testcase_34 AC 182 ms
22,252 KB
testcase_35 AC 23 ms
9,580 KB
testcase_36 AC 107 ms
16,004 KB
testcase_37 AC 49 ms
11,672 KB
testcase_38 AC 147 ms
19,172 KB
testcase_39 AC 95 ms
15,328 KB
testcase_40 AC 189 ms
22,800 KB
testcase_41 AC 110 ms
16,392 KB
testcase_42 AC 197 ms
22,840 KB
testcase_43 AC 227 ms
25,364 KB
testcase_44 AC 5 ms
8,304 KB
testcase_45 AC 5 ms
8,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;
//using mint = modint1000000007;
//using mint = modint998244353;

class Range {
public:
  struct RNG {
    ll l, r;
    bool operator<(const RNG& a) const {
      return r < a.r;
    }
  };
  set<RNG> rngs;

  void add(ll nl, ll nr){
    RNG rng;
    rng.l = nl;
    rng.r = nr;

    auto bgn = rngs.lower_bound((RNG){nl-1,nl-1});
    auto it = bgn;
    for(; it != rngs.end(); it++){
      if(it->l <= rng.r+1){
        nl = min(nl, it->l);
        nr = max(nr, it->r);
      }else{
        break;
      }
    }

    rngs.erase(bgn, it);
    rngs.insert((RNG){nl,nr});
  }
  void erase(ll nl, ll nr){
    RNG rng;
    rng.l = nl;
    rng.r = nr;

    set<RNG> tmp;
    auto bgn = rngs.lower_bound((RNG){nl-1,nl-1});
    auto it = bgn;
    for(; it != rngs.end(); it++){
      if(it->l <= rng.r){
        if(it->l < nl){
          tmp.insert((RNG){it->l,nl-1});
        }
        if(nr < it->r){
          tmp.insert((RNG){nr+1,it->r});
        }
      }else{
        break;
      }
    }

    rngs.erase(bgn, it);
    FOR(it,tmp){
      rngs.insert(*it);
    }
  }
};



Range ok[100005];

vector<int> solve(int N, const vector<pair<int,int>>& v){
  int Q = v.size();

  vector<int> ret;

  {
    int a = v[Q-1].first;
    int b = v[Q-1].second;
    if(a > b) swap(a,b);
    if(a+1 == b){
      if(a-1>=1 && 1<=a-1) ok[Q-1].add(1,a-1);
      if(b+1<=N && b+1<=N) ok[Q-1].add(b+1,N);
    }else{
      if(a-1>=1 && 1<=a-1) ok[Q-1].add(1,a-1);
      if(a+1<=b-1) ok[Q-1].add(a+1,b-1);
      if(b+1<=N && b+1<=N) ok[Q-1].add(b+1,N);
    }
  }
  for(int i=Q-2; i>=0; i--){
    int a = v[i].first;
    int b = v[i].second;
    if(a > b) swap(a,b);

    FOR(it,ok[i+1].rngs){
      int aa = it->l - 1;
      int bb = it->r + 1;
      aa = max(aa, 1);
      bb = min(bb, N);

      ok[i].add(aa,bb);
    }
    ok[i].erase(a,a);
    ok[i].erase(b,b);
  }
  /*
  rep(i,Q){
    cout << i << endl;
    FOR(it,ok[i]){
      cout << "\t" << it->first << " " << it->second << endl;
    }
  }
   */
  
  if(ok[0].rngs.size() == 0) return vector<int>();
  int pos = ok[0].rngs.begin()->l;
  ret.push_back(pos);
  REP(i,1,Q){
    int nxt = pos;
    REP(j,-1,2){
      int npos = pos + j;
      bool flg = false;
      if(1<=npos&&npos<=N){
        FOR(it,ok[i].rngs){
          if(it->l <= npos && npos <= it->r) flg = true;
        }
      }
      if(flg){
        nxt = npos;
        break;
      }
    }
    pos = nxt;
    ret.push_back(pos);
  }
    
  return ret;
}


int main(){
  int N, Q;
  cin >> N >> Q;
  vector<pair<int,int>> v(Q);
  rep(i,Q) cin >> v[i].first;
  rep(i,Q) cin >> v[i].second;

  vector<int> ret = solve(N, v);
  if(ret.size() > 0){
    cout << "YES" << endl;
    cout << ret[0] << endl;
    rep(i,ret.size()) cout << ret[i] << endl;
    return 0;
  }

  cout << "NO" << endl;
  
  return 0;
}

0