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. const int INF = 1e18;
  22. const int MOD = 1e9 + 7;
  23.  
  24. struct Fenwick {
  25. int n;
  26. vector<int> bit;
  27. Fenwick(int _n) {
  28. n = _n;
  29. bit.assign(n + 1, 0);
  30. }
  31. void add(int i, int v) {
  32. for (; i <= n; i += i & -i) bit[i] += v;
  33. }
  34. int sum(int i) {
  35. int s = 0;
  36. for (; i > 0; i -= i & -i) s += bit[i];
  37. return s;
  38. }
  39. };
  40.  
  41. void solve() {
  42. int n, q;
  43. cin >> n >> q;
  44.  
  45. vector<int> a(n), t(q);
  46. rep(i,0,n) cin >> a[i];
  47. rep(j,0,q) cin >> t[j];
  48.  
  49. int S = n + q + 5;
  50. Fenwick fw(S);
  51. unordered_map<int,int> pos;
  52.  
  53. rep(i,0,n) {
  54. int p = q + i + 1;
  55. pos[a[i]] = p;
  56. fw.add(p, 1);
  57. }
  58.  
  59. int curFront = q;
  60. vector<int> b;
  61. b.reserve(q);
  62.  
  63. rep(j,0,q) {
  64. int x = t[j];
  65. int p = pos[x];
  66. int ans = fw.sum(p);
  67. b.pb(ans);
  68. fw.add(p, -1);
  69. fw.add(curFront, 1);
  70. pos[x] = curFront;
  71. curFront--;
  72. }
  73.  
  74. each(x, b) cout << x << " ";
  75. cout << endl;
  76. }
  77.  
  78. int32_t main() {
  79. fast_io;
  80. int t = 1;
  81. while (t--) solve();
  82. return 0;
  83. }
  84.  
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