fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. inline int power(int a, int b) {
  6. int x = 1;
  7. while (b) {
  8. if (b & 1) x *= a;
  9. a *= a;
  10. b >>= 1;
  11. }
  12. return x;
  13. }
  14.  
  15.  
  16. const int M = 1000000007;
  17. const int N = 3e5+9;
  18. const int INF = 2e9+1;
  19. const int LINF = 2000000000000000001;
  20.  
  21. //_ ***************************** START Below *******************************
  22.  
  23.  
  24.  
  25. //* permutation given => no repetiton
  26.  
  27. vector<int> a;
  28. vector<int> b;
  29. void consistency(int n) {
  30.  
  31. unordered_map<int,int> freq;
  32. vector<int> ans(n, 0);
  33.  
  34. for(int i=0; i<n; i++){
  35. if(i-1>=0) ans[i] = ans[i-1];
  36. if(a[i] == b[i]){
  37. ans[i]++;
  38. }
  39. else{
  40.  
  41. if(freq.count(a[i]) ){
  42. ans[i]+=1;
  43. }
  44. if( freq.count(b[i]) ){
  45. ans[i]+=1;
  46. }
  47. }
  48. freq[a[i]]++;
  49. freq[b[i]]++;
  50.  
  51. }
  52.  
  53. for(int i=0; i<n; i++) cout << ans[i] << " "; cout << endl;
  54. }
  55.  
  56. void solve() {
  57.  
  58. int n;
  59. cin >> n;
  60. a.resize(n);
  61. b.resize(n);
  62. for(int i=0; i<n; i++) cin >> a[i];
  63. for(int i=0; i<n; i++) cin >> b[i];
  64. consistency(n) ;
  65.  
  66. }
  67.  
  68.  
  69.  
  70.  
  71.  
  72. int32_t main() {
  73. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  74.  
  75. int t = 1;
  76. while (t--) {
  77. solve();
  78. }
  79.  
  80. return 0;
  81. }
Success #stdin #stdout 0.01s 5292KB
stdin
5
1 2 3 5 4
2 1 4 5 3
stdout
0 2 2 3 5