fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.  
  6. int n,m;
  7. cin>>n>>m;
  8.  
  9. int a[n];
  10. int indices[n+1];
  11. for(int i=0;i<n;i++)
  12. {
  13. cin>>a[i];
  14. indices[a[i]]=i;
  15. }
  16.  
  17. int ans=1;
  18. for(int j=1;j<n;j++)
  19. {
  20. if(indices[j] > indices[j+1])
  21. {
  22. ans++;
  23. }
  24. }
  25.  
  26.  
  27. for(int i=0;i<m;i++)
  28. {
  29. int x,y;
  30. cin>>x>>y;
  31.  
  32. if(x < y)
  33. {
  34. if(a[x-1] > a[y-1])
  35. {
  36. ans--;
  37. }
  38. else
  39. {
  40. ans++;
  41. }
  42. }
  43. else
  44. {
  45. if(a[x-1] > a[y-1])
  46. {
  47. ans++;
  48. }
  49. else
  50. {
  51. ans--;
  52. }
  53. }
  54. int temp = a[x-1];
  55. a[x-1] = a[y-1];
  56. a[y-1] = temp;
  57.  
  58. temp = indices[a[x-1]];
  59. indices[a[x-1]] = indices[a[y-1]];
  60. indices[a[y-1]] = temp;
  61.  
  62. cout<<ans<<endl;
  63.  
  64. }
  65.  
  66.  
  67.  
  68. return 0;
  69. }
Success #stdin #stdout 0s 5280KB
stdin
5 3
4 2 1 5 3
2 3
1 5
2 3
stdout
2
1
2