結果
| 問題 |
No.1698 Face to Face
|
| コンテスト | |
| ユーザー |
Nachia
|
| 提出日時 | 2021-10-02 00:29:35 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 439 ms / 5,000 ms |
| コード長 | 3,205 bytes |
| コンパイル時間 | 4,730 ms |
| コンパイル使用メモリ | 150,008 KB |
| 最終ジャッジ日時 | 2025-01-24 19:50:04 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 45 |
ソースコード
#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;
Nachia