chang2394

Untitled

Oct 8th, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. // uzumaki naruto
  2. #include <bits/stdc++.h>
  3. #define each(v,c)  for(typeof((c).begin()) v = (c).begin(); v != (c).end(); ++v)
  4. #define pb         push_back
  5. #define mp         make_pair
  6. #define sz(a)      ((int)(a.size()))
  7. #define all(a)     (a).begin(), (a).end()
  8. #define fi         first
  9. #define se         second
  10. #define TRACE
  11. using namespace std;
  12.  
  13. #ifdef TRACE
  14. #define debug(a,n)    cerr << "["; for(int i = 0; i < n; ++i) cerr << a[i] << " ";cerr << "\b]\n";
  15. #define dbg(args...)  {debug1,args; cerr<<endl;}
  16. #define pause()       cin.get();cin.get();
  17.  
  18. #else
  19.  
  20. #define debug(a,n)
  21. #define dbg(args...)
  22. #define pause()
  23.  
  24. #endif
  25.  
  26. struct debugger {
  27.     template<typename T> debugger& operator , (const T& v) {
  28.         cerr<<v<<" "; return *this;
  29.     }
  30. } debug1;
  31.  
  32. template <typename T1, typename T2>
  33. inline ostream& operator << (ostream& os, const pair<T1, T2>& p) {
  34.     return os << "(" << p.first << ", " << p.second << ")";
  35. }
  36.  
  37. template<typename T>
  38. inline ostream &operator << (ostream & os,const vector<T>& v) {
  39.     bool first = true; os << "[";
  40.     for (typename vector<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) {
  41.         if(!first) os << ", ";
  42.         os << *ii; first = false;
  43.     }
  44.     return os << "]";
  45. }
  46.  
  47. typedef long long LL;
  48. typedef pair<int,int> pii;
  49. typedef vector<int> vi;
  50.  
  51. const int NN = 212345;
  52. int pos[NN], rnk[NN];
  53. int seg[NN];
  54.  
  55. const string pr(bool i){ return (i > 0 ? "TRUE":"FALSE"); }
  56. void update(int i,int v){
  57.     for(i += 5; i < NN; i += i&-i)
  58.         seg[i] += v;
  59. }
  60.  
  61. int get(int i){
  62.     int ans = 0;
  63.     for(i += 5; i > 0; i -= i&-i)
  64.         ans += seg[i];
  65.     return ans;
  66. }
  67.  
  68. void rep(int i,int vv){
  69.     int d = -1;
  70.     for(int k = 0; k < 2; ++k){
  71.         if (pos[i-1] > pos[i]) update(pos[i],d);
  72.         if (pos[i+1] > pos[i]) update(pos[i],d);
  73.         if (pos[i] > pos[i+1]) update(pos[i+1],d);
  74.         if (pos[i] > pos[i-1]) update(pos[i-1],d);
  75.         pos[i] = vv;
  76.         d = (-1)*d;
  77.     }
  78. }
  79.  
  80. void solve(){
  81.     int n,m;
  82.     cin >> n >> m;
  83.  
  84.     for(int i = 1; i <= n; ++i)
  85.         cin >> pos[i], rnk[pos[i]] = i;
  86.  
  87.     pos[0] = pos[n+1] = n+100;
  88.     update(pos[1],1);
  89.     update(pos[n],1);
  90.     for(int i = 1; i <= n; ++i){
  91.         if (pos[i] > pos[i-1]) update(pos[i-1],1);
  92.         if (pos[i] > pos[i+1]) update(pos[i+1],1);
  93.     }
  94.  
  95.     //for(int i = 1; i <= n; ++i)
  96.     //    dbg("at",i,get(i)-get(i-1));
  97.     while(m--){
  98.         int type,a,b,ans;
  99.         cin >> type >> a;
  100.  
  101.         if (type & 1){
  102.             cin >> b;
  103.             int x = rnk[a], y = rnk[b];
  104.             rep(x,b), rep(y,a);
  105.             rnk[a] = y, rnk[b] = x;
  106.             continue;
  107.         }
  108.  
  109.         ans = ((n > a) ? n-a+1-(get(n)-get(a)) : 1);
  110.         cout << ans << "\n";
  111.     }
  112. }
  113.  
  114. int main()
  115. {
  116.     ios_base::sync_with_stdio(0);
  117.     solve();
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment