fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define name "HARDSWAP"
  5. const int MAXN = 2e5+5;
  6.  
  7. int n, q, a[MAXN];
  8.  
  9. void setIO(){
  10. ios_base::sync_with_stdio(0);
  11. cin.tie(0); cout.tie(0);
  12. if (fopen(name".inp", "r")){
  13. freopen(name".inp", "r", stdin);
  14. freopen(name".out", "w", stdout);
  15. }
  16. }
  17.  
  18. void inp(){
  19. cin >> n;
  20. for (int i = 1; i<=n; i++){
  21. cin >> a[i];
  22. }
  23. cin >> q;
  24. }
  25.  
  26.  
  27. void solve(){
  28. while (q--){ // Thực hiện q truy vấn
  29. int x, y;
  30. cin >> x >> y;
  31. swap(a[x], a[y]); // Hoán đổi vị trí của 2 phần tử ở vị trí x và y
  32. }
  33.  
  34. for (int i = 1; i<=n; i++){
  35. cout << a[i] << " ";
  36. }
  37. }
  38.  
  39. int main(){
  40. setIO(); // Nhập xuất file
  41. inp(); // Nhập dữ liệu (có nội dung trong chương trình con hay không cũng được)
  42. solve(); // Giải quyết vấn đề của đề bài đưa ra
  43. }
Success #stdin #stdout 0s 5320KB
stdin
5
10 20 30 40 50
3
1 3 
4 5
1 5
stdout
40 20 10 50 30