結果

問題 No.3251 kthmex
ユーザー 👑 hos.lyric
提出日時 2025-08-29 23:14:03
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
MLE  
実行時間 -
コード長 8,005 bytes
コンパイル時間 2,880 ms
コンパイル使用メモリ 178,156 KB
実行使用メモリ 560,932 KB
最終ジャッジ日時 2025-08-29 23:14:12
合計ジャッジ時間 8,347 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 4 MLE * 1 -- * 27
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:256:38: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  256 |     for (int i = 0; i < N; ++i) scanf("%d", &A[i]);
      |                                 ~~~~~^~~~~~~~~~~~~
main.cpp:264:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  264 |       scanf("%d%d%d", &O[q], &L[q], &R[q]);
      |       ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:267:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  267 |         scanf("%d%d", &X[q], &Y[q]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
main.cpp:269:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  269 |         scanf("%d", &K[q]);
      |         ~~~~~^~~~~~~~~~~~~

ソースコード

diff #

// O(N^(2/3) log(N)^998244353) qwq

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


// cannot use count
// no move constructor (==> use pointer for merge tech)
// unordered_set by value: __gnu_pbds::null_type
// no erase(iterator)
#include <ext/pb_ds/assoc_container.hpp>
using __gnu_pbds::gp_hash_table;

// https://codeforces.com/blog/entry/62393
#include <chrono>
struct Hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM = std::chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
  size_t operator()(const pair<int, int> &a) const {
    return operator()((uint64_t)a.first << 32 | a.second);
  }
};
template <class K> using Set = gp_hash_table<K, __gnu_pbds::null_type, Hash>;
template <class K, class V> using Map = gp_hash_table<K, V, Hash>;


// iterator find_by_order(k): 0-indexed
// size_type order_of_key(key): min id s.t. >= key
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
template <class K, class V> using rbtree = __gnu_pbds::tree<
  K,
  V,
  std::less<K>,
  __gnu_pbds::rb_tree_tag,
  __gnu_pbds::tree_order_statistics_node_update
>;


int N, Q;
vector<int> A;
vector<int> O, L, R, X, Y, K;

vector<int> brute() {
  vector<int> ans(Q, 0);
  auto as = A;
cerr<<"[brute] "<<as<<endl;
  for (int q = 0; q < Q; ++q) {
    if (O[q] == 1) {
      for (int i = L[q]; i < R[q]; ++i) if (as[i] == X[q]) as[i] = Y[q];
cerr<<"[brute] "<<as<<endl;
    } else {
      vector<int> bs(as.begin() + L[q], as.begin() + R[q]);
      sort(bs.begin(), bs.end());
      bs.erase(unique(bs.begin(), bs.end()), bs.end());
      int lo = 0, hi = (int)bs.size() + K[q];
      for (; lo + 1 < hi; ) {
        const int mid = lo + (hi - lo) / 2;
        // ex in [1, mid]
        int ex = mid;
        ex -= (upper_bound(bs.begin(), bs.end(), mid) - bs.begin());
        ((ex >= K[q]) ? hi : lo) = mid;
      }
      ans[q] = hi;
cerr<<"[brute] "<<as<<" "<<bs<<"; "<<K[q]<<" "<<ans[q]<<endl;
    }
  }
  return ans;
}

namespace fast {
constexpr int MAX_X = 110;

int U, V;
vector<int> as;
rbtree<int, int> freq[MAX_X][MAX_X];
vector<pair<int, int>> lz[MAX_X];

void push(int u) {
  const int l = u * V;
  const int r = min((u + 1) * V, N);
  Map<int, int> ma;
  for (; lz[u].size(); ) {
    const int a = lz[u].back().first;
    const int b = lz[u].back().second;
    lz[u].pop_back();
    int c = b;
    auto it = ma.find(b);
    if (it != ma.end()) {
      c = it->second;
    }
    ma[a] = c;
  }
  for (int i = l; i < r; ++i) {
    auto it = ma.find(as[i]);
    if (it != ma.end()) {
      as[i] = it->second;
    }
  }
}

vector<int> run() {
  V = pow<double>(N, 2.0/3.0);
  U = (N + V - 1) / V;
cerr<<string(40,'-')<<" U = "<<U<<", V = "<<V<<endl;
  as = A;
  for (int u0 = 0; u0 < U; ++u0) for (int u1 = u0 + 1; u1 <= U; ++u1) {
    freq[u0][u1].clear();
    const int l = u0 * V;
    const int r = min(u1 * V, N);
    for (int i = l; i < r; ++i) ++freq[u0][u1][as[i]];
  }
  for (int u = 0; u < U; ++u) {
    lz[u].clear();
  }
  vector<int> ans(Q, 0);
  for (int q = 0; q < Q; ++q) {
    const int uL = L[q] / V;
    const int uR = (R[q] - 1) / V;
    if (uL == uR) {
      push(uL);
    } else {
      push(uL);
      push(uR);
    }
//cerr<<COLOR("93")<<"q = "<<q<<"; "<<L[q]<<" "<<R[q]<<" "<<X[q]<<" "<<Y[q]<<" "<<K[q]<<COLOR()<<endl;
//cerr<<"as = "<<as<<endl;
//for(int u0=0;u0<U;++u0)for(int u1=u0+1;u1<=U;++u1){cerr<<"freq["<<u0<<"]["<<u1<<"] = ";pv(freq[u0][u1].begin(),freq[u0][u1].end());}
//cerr<<"lz = ";pv(lz,lz+U);
    if (O[q] == 1) {
      vector<int> changes(U, 0);
      auto modify = [&](int u, int l, int r) -> void {
        for (int i = l; i < r; ++i) if (as[i] == X[q]) {
          ++changes[u];
          as[i] = Y[q];
        }
      };
      if (uL == uR) {
        modify(uL, L[q], R[q]);
      } else {
        modify(uL, L[q], (uL + 1) * V);
        modify(uR, uR * V, R[q]);
        for (int u = uL + 1; u < uR; ++u) {
          auto it = freq[u][u + 1].find(X[q]);
          if (it != freq[u][u + 1].end()) {
            changes[u] = it->second;
            lz[u].emplace_back(X[q], Y[q]);
          }
        }
      }
//cerr<<"changes = "<<changes<<endl;
      for (int u0 = 0; u0 < U; ++u0) {
        int sum = 0;
        for (int u = u0; u < U; ++u) {
          sum += changes[u];
          if (sum) {
            const int u1 = u + 1;
            auto it = freq[u0][u1].find(X[q]);
            assert(it != freq[u0][u1].end());
            it->second -= sum;
            if (!it->second) freq[u0][u1].erase(it);
            freq[u0][u1][Y[q]] += sum;
          }
        }
      }
    } else {
      const rbtree<int, int> *f;
      vector<int> bs;
      auto san = [&](int l, int r) -> void {
        for (int i = l; i < r; ++i) {
          auto it = f->find(as[i]);
          if (it != f->end()) {
            //
          } else {
            bs.push_back(as[i]);
          }
        }
      };
      if (uL == uR) {
        f = &freq[0][0];
        san(L[q], R[q]);
      } else {
        f = &freq[uL + 1][uR];
        san(L[q], (uL + 1) * V);
        san(uR * V, R[q]);
      }
      sort(bs.begin(), bs.end());
      bs.erase(unique(bs.begin(), bs.end()), bs.end());
//cerr<<"uL = "<<uL<<", uR = "<<uR<<endl;
//cerr<<"f = ";pv(f->begin(),f->end());
//cerr<<"bs = "<<bs<<endl;
      auto calc = [&](int thr) -> int {
        int ret = 0;
        ret += f->order_of_key(thr + 1);
        ret += (upper_bound(bs.begin(), bs.end(), thr) - bs.begin());
        return ret;
      };
      int lo = 0, hi = (R[q] - L[q]) + K[q];
      for (; lo + 1 < hi; ) {
        const int mid = lo + (hi - lo) / 2;
        ((mid - calc(mid) >= K[q]) ? hi : lo) = mid;
      }
      ans[q] = hi;
    }
  }
  return ans;
}
}  // fast

int main() {
  for (; ~scanf("%d%d", &N, &Q); ) {
    A.resize(N);
    for (int i = 0; i < N; ++i) scanf("%d", &A[i]);
    O.resize(Q);
    L.resize(Q);
    R.resize(Q);
    X.assign(Q, 0);
    Y.assign(Q, 0);
    K.assign(Q, 0);
    for (int q = 0; q < Q; ++q) {
      scanf("%d%d%d", &O[q], &L[q], &R[q]);
      --L[q];
      if (O[q] == 1) {
        scanf("%d%d", &X[q], &Y[q]);
      } else if (O[q] == 2) {
        scanf("%d", &K[q]);
      } else {
        assert(false);
      }
    }
    
    const auto ans = fast::run();
    for (int q = 0; q < Q; ++q) if (O[q] == 2) {
      printf("%d\n", ans[q]);
    } 
#ifdef LOCAL
const auto brt=brute();
if(brt!=ans){
 cerr<<"FAIL"<<endl;
 cerr<<"brt = "<<brt<<endl;
 cerr<<"ans = "<<ans<<endl;
 assert(false);
}
#endif
  }
  return 0;
}
0