結果

問題 No.1675 Strange Minimum Query
ユーザー moondrink
提出日時 2021-09-24 14:45:21
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 303 ms / 2,000 ms
コード長 2,240 bytes
コンパイル時間 1,782 ms
コンパイル使用メモリ 183,588 KB
実行使用メモリ 16,512 KB
最終ジャッジ日時 2024-07-05 09:43:12
合計ジャッジ時間 11,395 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:29:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
   29 |   for (auto [b, l, r] : query) {
      |             ^

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(),v.end()
#define endl "\n" //真的裂开,遇到卡endl的,就吐了,还是\n好,endl其实是一个函数,比较慢

int main() {
  int n, q;
  cin >> n >> q;
  vector<array<int, 3>> query(q);
  for (int i = 0; i < q; i++) {
    int l, r, b;
    cin >> l >> r >> b;
    l --;
    r --;  //由于下标是从0 - n - 1,所以l -- ,r --
    query[i] = {b, l, r};
  }
  
  sort(all(query));
  reverse(all(query));
  //把询问按照B从大到小排序

  int prev = -1;
  vector<int> tmp(n); //tmp存储位置
  for(int i = 0;i < n;i ++ ) tmp[i] = i;
  set<int> unused(all(tmp));  // 未使用的位置集合
  set<int> used_by_b;  // 已经使用的位置集合

  vector<int> ans(n); //答案
  for (auto [b, l, r] : query) {
    if (b != prev) {        //如果当前最小值不等于上一次的最小值,对于再次访问到之前访问过的区间,肯定就不存在,就必须要把unused清空,这样就找不到了,就输出-1
      prev = b;
      used_by_b.clear();
    }
    auto itr = unused.lower_bound(l);  //看还有没有没被访问过的位置,并且是在l-r里面的
    if (itr == unused.end() || *itr > r) {  // 如果当前询问的区间所有的点之前都访问过,并且赋了答案
      auto itr2 = used_by_b.lower_bound(l);  //如果当前B小于上一次的B,此时used已经被清空了,itr2就是used.end(),直接-1;但是如果等于的话,used没有被清空,相当于之前这个区间的最小值是B,现在又来一次还是B,根本就不影响嘛!!
      if (itr2 == used_by_b.end()) {
        cout << -1 << endl;  //不存在
        return 0;
      }
    } else {  // 如果找到了,说明还有没有访问的位置,while里面就把没有访问的位置全部置为B
      while (itr != unused.end() && *itr <= r) {
        ans[*itr] = b;   //
        used_by_b.insert(*itr);  // 
        itr = unused.erase(itr);  //
      }
    }
  }

  for (auto i : unused) ans[i] = int(1e9);  //没有涉及到的下标一律设为int中的最大值,这样直接保证答案不会受影响
  for (int i = 0; i < n; i++) cout << ans[i] << " \n"[i == n - 1];
  return 0;
}
//好了,完了
0