Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // uzumaki naruto
- #include <bits/stdc++.h>
- #define each(v,c) for(typeof((c).begin()) v = (c).begin(); v != (c).end(); ++v)
- #define pb push_back
- #define mp make_pair
- #define sz(a) ((int)(a.size()))
- #define all(a) (a).begin(), (a).end()
- #define fi first
- #define se second
- #define TRACE
- using namespace std;
- #ifdef TRACE
- #define debug(a,n) cerr << "["; for(int i = 0; i < n; ++i) cerr << a[i] << " ";cerr << "\b]\n";
- #define dbg(args...) {debug1,args; cerr<<endl;}
- #define pause() cin.get();cin.get();
- #else
- #define debug(a,n)
- #define dbg(args...)
- #define pause()
- #endif
- struct debugger {
- template<typename T> debugger& operator , (const T& v) {
- cerr<<v<<" "; return *this;
- }
- } debug1;
- template <typename T1, typename T2>
- inline ostream& operator << (ostream& os, const pair<T1, T2>& p) {
- return os << "(" << p.first << ", " << p.second << ")";
- }
- template<typename T>
- inline ostream &operator << (ostream & os,const vector<T>& v) {
- bool first = true; os << "[";
- for (typename vector<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) {
- if(!first) os << ", ";
- os << *ii; first = false;
- }
- return os << "]";
- }
- typedef long long LL;
- typedef pair<int,int> pii;
- typedef vector<int> vi;
- const int NN = 212345;
- int pos[NN], rnk[NN];
- int seg[NN];
- const string pr(bool i){ return (i > 0 ? "TRUE":"FALSE"); }
- void update(int i,int v){
- for(i += 5; i < NN; i += i&-i)
- seg[i] += v;
- }
- int get(int i){
- int ans = 0;
- for(i += 5; i > 0; i -= i&-i)
- ans += seg[i];
- return ans;
- }
- void rep(int i,int vv){
- int d = -1;
- for(int k = 0; k < 2; ++k){
- if (pos[i-1] > pos[i]) update(pos[i],d);
- if (pos[i+1] > pos[i]) update(pos[i],d);
- if (pos[i] > pos[i+1]) update(pos[i+1],d);
- if (pos[i] > pos[i-1]) update(pos[i-1],d);
- pos[i] = vv;
- d = (-1)*d;
- }
- }
- void solve(){
- int n,m;
- cin >> n >> m;
- for(int i = 1; i <= n; ++i)
- cin >> pos[i], rnk[pos[i]] = i;
- pos[0] = pos[n+1] = n+100;
- update(pos[1],1);
- update(pos[n],1);
- for(int i = 1; i <= n; ++i){
- if (pos[i] > pos[i-1]) update(pos[i-1],1);
- if (pos[i] > pos[i+1]) update(pos[i+1],1);
- }
- //for(int i = 1; i <= n; ++i)
- // dbg("at",i,get(i)-get(i-1));
- while(m--){
- int type,a,b,ans;
- cin >> type >> a;
- if (type & 1){
- cin >> b;
- int x = rnk[a], y = rnk[b];
- rep(x,b), rep(y,a);
- rnk[a] = y, rnk[b] = x;
- continue;
- }
- ans = ((n > a) ? n-a+1-(get(n)-get(a)) : 1);
- cout << ans << "\n";
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(0);
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment