fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
  5.  
  6. #define int long long
  7. #define pb push_back
  8. #define ff first
  9. #define ss second
  10. #define all(x) (x).begin(), (x).end()
  11. #define rall(x) (x).rbegin(), (x).rend()
  12. #define sz(x) ((int)(x).size())
  13. #define endl '\n'
  14. #define yes cout << "yes\n"
  15. #define no cout << "no\n"
  16.  
  17. #define rep(i,a,b) for (int i = (a); i < (b); ++i)
  18. #define per(i,a,b) for (int i = (b) - 1; i >= (a); --i)
  19. #define each(x, a) for (auto &x : (a))
  20.  
  21. struct Fenwick {
  22. int n;
  23. vector<int> bit;
  24. Fenwick(int _n) {
  25. n = _n;
  26. bit.assign(n + 1, 0);
  27. }
  28. void add(int i, int v) {
  29. for (; i <= n; i += i & -i) bit[i] += v;
  30. }
  31. int sum(int i) {
  32. int s = 0;
  33. for (; i > 0; i -= i & -i) s += bit[i];
  34. return s;
  35. }
  36. };
  37.  
  38. void solve() {
  39. int n, q;
  40. cin >> n >> q;
  41.  
  42. vector<int> a(n), t(q);
  43. rep(i,0,n) cin >> a[i];
  44. rep(j,0,q) cin >> t[j];
  45.  
  46. int S = n + q + 5;
  47. Fenwick fw(S);
  48. unordered_map<int,int> pos;
  49.  
  50. rep(i,0,n) {
  51. int p = q + i + 1;
  52. pos[a[i]] = p;
  53. fw.add(p, 1);
  54. }
  55.  
  56. int curFront = q;
  57. vector<int> b;
  58. b.reserve(q);
  59.  
  60. rep(j,0,q) {
  61. int x = t[j];
  62. int p = pos[x];
  63. int ans = fw.sum(p);
  64. b.pb(ans);
  65. fw.add(p, -1);
  66. fw.add(curFront, 1);
  67. pos[x] = curFront;
  68. curFront--;
  69. }
  70.  
  71. each(x, b) cout << x << " ";
  72. cout << endl;
  73. }
  74.  
  75. int32_t main() {
  76. fast_io;
  77. int t = 1;
  78. while (t--) solve();
  79. return 0;
  80. }
  81.  
Success #stdin #stdout 0.01s 5284KB
stdin
7 5
2 1 1 4 3 3 1
3 2 1 1 4
stdout
6 2 7 1 6