結果

問題 No.1698 Face to Face
ユーザー 👑 NachiaNachia
提出日時 2021-10-02 00:29:35
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 443 ms / 5,000 ms
コード長 3,205 bytes
コンパイル時間 1,691 ms
コンパイル使用メモリ 103,176 KB
実行使用メモリ 44,000 KB
最終ジャッジ日時 2023-09-27 00:50:01
合計ジャッジ時間 15,089 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 298 ms
42,088 KB
testcase_04 AC 309 ms
43,456 KB
testcase_05 AC 361 ms
42,728 KB
testcase_06 AC 348 ms
42,712 KB
testcase_07 AC 326 ms
43,052 KB
testcase_08 AC 328 ms
42,772 KB
testcase_09 AC 329 ms
42,920 KB
testcase_10 AC 294 ms
44,000 KB
testcase_11 AC 354 ms
42,604 KB
testcase_12 AC 391 ms
42,756 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 419 ms
42,640 KB
testcase_16 AC 425 ms
42,624 KB
testcase_17 AC 443 ms
42,840 KB
testcase_18 AC 382 ms
39,896 KB
testcase_19 AC 376 ms
39,416 KB
testcase_20 AC 358 ms
38,696 KB
testcase_21 AC 175 ms
21,580 KB
testcase_22 AC 130 ms
19,116 KB
testcase_23 AC 29 ms
7,004 KB
testcase_24 AC 209 ms
23,316 KB
testcase_25 AC 409 ms
41,052 KB
testcase_26 AC 17 ms
5,156 KB
testcase_27 AC 88 ms
12,656 KB
testcase_28 AC 95 ms
13,296 KB
testcase_29 AC 67 ms
11,620 KB
testcase_30 AC 246 ms
24,872 KB
testcase_31 AC 174 ms
21,580 KB
testcase_32 AC 309 ms
35,944 KB
testcase_33 AC 312 ms
36,084 KB
testcase_34 AC 420 ms
41,440 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 1 ms
4,376 KB
testcase_44 AC 2 ms
4,376 KB
testcase_45 AC 338 ms
43,528 KB
testcase_46 AC 354 ms
43,264 KB
testcase_47 AC 397 ms
43,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <atcoder/segtree>
using namespace std;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
#define rep(i,n) for(int i=0; i<(n); i++)


const int maxN = 100000;

namespace RQ{
  struct S { int Amax, Bmax, q, i; };
  S op(S l, S r){
    S res = {
      max(l.Amax,r.Amax),
      max(l.Bmax,r.Bmax),
      0,0
    };
    res.q = max(l.q,r.Bmax);
    res.i = l.i;
    if(res.q > max(l.Amax,r.q)){
      res.q = max(l.Amax,r.q);
      res.i = r.i;
    }
    return res;
  }
  S e(){ return { 0,0,maxN*2,0 }; }
  using RQ = atcoder::segtree<S,op,e>;
  RQ createRQ1(int N, const vector<int>& A, const vector<int>& B){
    vector<S> tmp(2*N-1);
    rep(i,N-1) tmp[2*i+1].Amax = max(A[i],A[i+1]);
    rep(i,N-1) tmp[2*i+1].Bmax = max(B[i],B[i+1]);
    rep(i,N-1) tmp[2*i+1].q = max(tmp[2*i+1].Amax, tmp[2*i+1].Bmax);
    rep(i,2*N-1) tmp[i].i = i/2;
    auto t = op(tmp[4],tmp[5]);
    return RQ(tmp);
  }
  RQ createRQ2(int N, const vector<int>& A, const vector<int>& B){
    vector<S> tmp(2*N-1);
    rep(i,N-1) tmp[2*i+1].Bmax = max(A[i],A[i+1]);
    rep(i,N-1) tmp[2*i+1].Amax = max(B[i],B[i+1]);
    rep(i,N-1) tmp[2*i+1].q = max(tmp[2*i+1].Amax, tmp[2*i+1].Bmax);
    rep(i,2*N-1) tmp[i].i = i/2;
    return RQ(tmp);
  }
}


struct CompNode{
  int pos;
  int type;
  bool operator<(const CompNode& r) const {
    if(pos != r.pos) return pos < r.pos;
    return type < r.type;
  }
};

struct ValNode{
  int w;
  int cnt;
};


int main(){
  int N; cin >> N;
  vector<int> A(N); rep(i,N){ cin >> A[i]; A[i]--; }
  vector<int> B(N); rep(i,N){ cin >> B[i]; B[i]--; }
  vector<int> Z(N); rep(i,N){ cin >> Z[i]; Z[i]--; }
  vector<int> invA(N); rep(i,N) invA[A[i]] = i;
  vector<int> invB(N); rep(i,N) invB[B[i]] = i;

  map<CompNode, ValNode> G;
  rep(i,N+1) G[{i,0}] = {0,0};
  rep(i,N+1) G[{i,1}] = {0,0};
  for(int i=1; i<=N; i++) G[{i,0}].w += 1;

  auto rq1 = RQ::createRQ1(N,A,B);
  auto rq2 = RQ::createRQ2(N,A,B);
  vector<vector<int>> toAddCnt(N);
  rep(i,N){
    int za = invA[i], zb = invB[Z[i]];
    RQ::S q;
    if(za <= zb) q = rq1.prod(za*2, zb*2+1);
    else q = rq2.prod(zb*2, za*2+1);
    toAddCnt[q.q].push_back(q.i);
  }

  vector<vector<CompNode>> toEraseSep(N);
  rep(i,N-1) toEraseSep[max(A[i],A[i+1])].push_back({i+1,0});
  rep(i,N-1) toEraseSep[max(B[i],B[i+1])].push_back({i+1,1});

  int ans = 0;

  rep(i,N){
    for(auto s : toEraseSep[i]){
      auto itr = G.find(s);
      auto nxitr = itr; nxitr++;
      ans -= min(itr->second.cnt, itr->second.w);
      ans -= min(nxitr->second.cnt, nxitr->second.w);
      nxitr->second.cnt += itr->second.cnt;
      nxitr->second.w += itr->second.w;
      ans += min(nxitr->second.cnt, nxitr->second.w);
      G.erase(itr);
    }
    for(auto s : toAddCnt[i]){
      auto itr = G.upper_bound({s,2});
      ans -= min(itr->second.cnt, itr->second.w);
      itr->second.cnt += 1;
      ans += min(itr->second.cnt, itr->second.w);
    }
    cout << ans << endl;
  }

  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