結果

問題 No.1024 Children in a Row
ユーザー CinnamorollCinnamoroll
提出日時 2020-03-10 02:26:07
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 787 ms / 2,000 ms
コード長 7,429 bytes
コンパイル時間 2,478 ms
コンパイル使用メモリ 176,496 KB
実行使用メモリ 76,724 KB
最終ジャッジ日時 2023-10-12 07:40:29
合計ジャッジ時間 19,894 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
18,380 KB
testcase_01 AC 10 ms
18,612 KB
testcase_02 AC 9 ms
18,508 KB
testcase_03 AC 11 ms
19,032 KB
testcase_04 AC 10 ms
18,824 KB
testcase_05 AC 10 ms
18,756 KB
testcase_06 AC 10 ms
18,872 KB
testcase_07 AC 10 ms
18,824 KB
testcase_08 AC 9 ms
18,908 KB
testcase_09 AC 10 ms
18,936 KB
testcase_10 AC 713 ms
55,788 KB
testcase_11 AC 703 ms
53,400 KB
testcase_12 AC 704 ms
54,500 KB
testcase_13 AC 742 ms
55,544 KB
testcase_14 AC 710 ms
54,000 KB
testcase_15 AC 739 ms
55,512 KB
testcase_16 AC 714 ms
53,780 KB
testcase_17 AC 695 ms
57,944 KB
testcase_18 AC 663 ms
52,696 KB
testcase_19 AC 787 ms
62,364 KB
testcase_20 AC 786 ms
62,380 KB
testcase_21 AC 787 ms
62,388 KB
testcase_22 AC 774 ms
62,320 KB
testcase_23 AC 770 ms
61,592 KB
testcase_24 AC 775 ms
61,516 KB
testcase_25 AC 762 ms
61,764 KB
testcase_26 AC 652 ms
63,948 KB
testcase_27 AC 639 ms
59,464 KB
testcase_28 AC 725 ms
76,724 KB
testcase_29 AC 505 ms
76,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// warm heart, wagging tail,and a smile just for you!
//                                                                     ███████████
//                                                                   ███╬╬╬╬╬╬╬╬╬╬███
//                                                                ███╬╬╬╬╬████╬╬╬╬╬╬███
//                                            ███████████       ██╬╬╬╬╬████╬╬████╬╬╬╬╬██
//                                      █████████╬╬╬╬╬████████████╬╬╬╬╬██╬╬╬╬╬╬███╬╬╬╬╬██
//                               ████████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬██╬╬╬╬╬╬╬██
//                             ████╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬╬╬╬╬╬██
//                           ███╬╬╬█╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬███╬╬╬╬╬╬╬█████
//                         ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬████████╬╬╬╬╬██
//                       ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬╬╬╬╬███
//                     ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬██
//                 ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬████
//     █████████████╬╬╬╬╬╬╬╬██╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬██████
//   ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬██████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████╬╬╬╬╬╬╬███████████╬╬╬╬╬╬╬╬██╬╬╬██╬╬╬██
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬█╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬██
// ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬▓▓▓▓▓▓╬╬╬████╬╬████╬╬╬╬╬╬╬▓▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬███
// ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬█████
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████████
//   ███╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██
//       ██████████████  ████╬╬╬╬╬╬███████████████████████████╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████
//                         ███████                           █████  ███████████████████  
//
#include "bits/stdc++.h"
using namespace std;
#define INF (1<<30)
#define LINF (1LL<<60)
#define fs first
#define sc second
#define int long long
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define RFOR(i,a,b) for(int i = (b-1);i>=(a);--i)
#define REP(i,n)  FOR(i,0,(n))
#define RREP(i,n) RFOR(i,0,(n))
#define ITR(itr,mp) for(auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr,mp) for(auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define range(i,a,b) ((a)<=(i) && (i)<(b))
#define debug(x)  cout << #x << " = " << (x) << endl
#define SP << " " << 
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
#define MSB(x) (63-__builtin_clzll(x))
#define pcnt(x) (__builtin_popcountll(x))
#define parity(i,j) (i&(1LL<<j))
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
typedef vector<int> vec;
typedef vector<vector<int>> mat;

const int N = 4e5+10;
vec edge[N], s(N), t(N);
map<int,int> mp;

void dfs(int no, int &cnt){
  s[no] = cnt; mp[cnt] = no; cnt++;
  if(edge[no].size()) RREP(i,edge[no].size()) dfs(edge[no][i],cnt);
  t[no] = cnt;
}

signed main(){
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n,m;
  cin >> n >> m;

  vector<int> a(m),b(m),k(m);
  REP(i,m) cin >> a[i] >> b[i] >> k[i], a[i]--, b[i]--;
  
  vec last(n), pre(n+m);
  REP(i,n) last[i] = i;
  int id = n;
  REP(i,m){
    pre[id] = last[a[i]];
    last[a[i]] = id; 
    edge[last[b[i]]].emplace_back(id);
    id++;
  }

  int cnt = 0;
  REP(i,n) dfs(i,cnt);

  vec sum(n+m+1,0); //区間に含まれる人数
  vec v(n); // 前からi人目のcntの値
  REP(i,n) sum[s[last[i]]+1]++; 
  REP(i,n+m){
    sum[i+1] += sum[i];
    if(sum[i+1]!=sum[i]) v[sum[i+1]-1] = i;
  }

  auto f = [&](int x, int l){
    int k = mp[v[sum[l]+x-1]];
    if(k < n) return k+1;
    else return a[k-n]+1;
  };

  REP(i,m){
    int l = s[n+i], r = t[n+i], l2 = s[pre[n+i]], r2 = t[pre[n+i]];
    // 後ろに移動
    if(l<l2){
      int x = sum[l], y = sum[l2]-sum[r], z = sum[r]-sum[l];
      if(k[i] <= x) cout << f(k[i],0) << endl;
      else if(k[i] <= x+y) cout << f(k[i]-x,r) << endl;
      else if(k[i] <= x+y+z) cout << f(k[i]-x-y,l) << endl;
      else  cout << f(k[i]-x-y-z,l2) << endl;
    }
    // 手前に移動
    else{
      int x = sum[l2], y = sum[l]-sum[l2], z = sum[r]-sum[l];
      if(k[i] <= x) cout << f(k[i],0) << endl;
      else if(k[i] <= x+z) cout << f(k[i]-x,l) << endl;
      else if(k[i] <= x+y+z) cout << f(k[i]-x-z,l2) << endl;
      else cout << f(k[i]-x-y-z,r) << endl;
    }
  }

  return 0;
}
0