#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //set
// using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>; // multiset
static int LOCAL=0;
#define length(a) (sizeof(a)/sizeof(a[0]))
#define print(v,i,x) for(int j=i;j<=x;j++){cout<<v[j]<<" ";}cout<<endl;
#define pb push_back
#define mp make_pair
#define lli long long int
#define ulli unsigned long long int
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define f first
#define s second
#define si(x) scanf("%d",&x)
#define slli(x) scanf("%lld",&x)
#define si2(x,y) scanf("%d %d",&x,&y)
#define si3(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define slli2(x,y) scanf("%lld %lld",&x,&y)
#define slli3(x,y,z) scanf("%lld %lld %lld",&x,&y,&z)
#define pi(x) printf("%d",x)
#define pi2(x,y) printf("%d %d",x,y)
#define pi3(x,y,z) printf("%d %d %d",x,y,z)
#define pli(x) printf("%ld",x)
#define plli(x) printf("%lld",x)
#define plli2(x,y) printf("%lld %lld",x,y)
#define plli3(x,y,z) printf("%lld %lld %lld",x,y,z)
#define pn printf("\n")
#define ps printf(" ")
#define pc(c) printf("%c",c)
#define pstr(s) printf("%s",s)
#define FOR(i,x,n) for(int i=x;i<=n;i++)
#define FORl(i,x,n) for(lli i=x;i<=n;i++)
#define ROF(i,x,n) for(int i=x;i>=n;i--)
#define ROFl(i,x,n) for(lli i=x;i>=n;i--)
#define debug(x) cout << " - " << #x << ": " << x << endl;
#define debugs(x, y) cout << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define debugss(x, y, z) cout << " - " << #x << ": " << x << " " << #y << ": " << y << " " << #z << ": " << z << endl;
#define fastIO std::ios::sync_with_stdio(false);cin.tie(NULL);
#define cut cout<<"------------------------------------------\n";
#define cut1 cout<<"******************************************\n";
typedef vector<int> vi;
typedef vector<long long> vlli;
typedef vector<string> vstr;
typedef pair<int,int> prii;
typedef pair<int,lli> prilli;
typedef pair<lli,int> prllii;
typedef pair<lli,lli> prllilli;
const lli mod = 1000000007ll;
const lli MOD = 1000000009ll;
const lli INF = LLONG_MAX/10;
const int inf = INT_MAX/2;
lli count_bit(lli _x){lli _ret=0;while(_x){if(_x%2==1)_ret++;_x/=2;}return _ret;}
bool check_bit(lli _mask,lli _i){lli x=1;return (_mask&(x<<_i))==0?false:true;}
lli set_bit(lli _mask,lli _i){lli x=1;_mask=_mask|(x<<_i);return _mask;}
lli msb(lli _mask){lli ret=-1;int cnt=0;while(_mask){if(_mask&1)ret=cnt;_mask/=2;cnt++;}return ret;}
lli powermod(lli _a,lli _b,lli _m=mod){lli _r=1;while(_b){if(_b%2==1)_r=(_r*_a)%_m;_b/=2;_a=(_a*_a)%_m;}return _r;}
lli power(lli _a,lli _b){lli _r=1;while(_b){if(_b%2==1)_r=(_r*_a);_b/=2;_a=(_a*_a);}return _r;}
lli add(lli a,lli b,lli m=mod){lli x=a+b;while(x>=m)x-=m;return x;}
lli sub(lli a,lli b,lli m=mod){lli x=a-b;while(x<0)x+=m;return x;}
lli mul(lli a,lli b,lli m=mod){lli x=a*b;x%=m;return x;}
lli gcd(lli a,lli b){while(a&&b)a>b?a%=b:b%=a;return a+b;}
lli lcm(lli a,lli b){return (max(a,b)/gcd(a,b))*min(a,b);}
struct pair_hash {
std::size_t operator () (const std::pair<lli,lli> &p) const {
auto h1 = std::hash<lli>{}(p.first);
auto h2 = std::hash<lli>{}(p.second);
return h1 ^ h2;
}
};
struct cmp{
bool operator()(prii const & l,prii const & r){
return l.s<r.s;
}
}myobject;
ordered_set<int> tr[800010];
int a[200010],n,q,l,r;
void build(int node,int st,int en){
if(st==en){
tr[node].insert(st);
return;
}
int mid=(st+en)>>1;
build(2*node,st,mid);
build(2*node+1,mid+1,en);
FOR(i,st,en)tr[node].insert(i);
}
void update(int node,int st,int en,int idx,int val){
if(st==en){
tr[node].erase(a[idx]);
tr[node].insert(val);
return;
}
int mid=(st+en)>>1;
if(idx<=mid)
update(2*node,st,mid,idx,val);
else
update(2*node+1,mid+1,en,idx,val);
tr[node].erase(a[idx]);
tr[node].insert(val);
}
lli query(int node,int st,int en,int l,int r,int val){
if(en<l || st>r || l>r)return 0;
if(st>=l && en<=r)
return tr[node].order_of_key(val);
int mid=(st+en)>>1;
return query(2*node,st,mid,l,r,val)+query(2*node+1,mid+1,en,l,r,val);
}
int main()
{
LOCAL=0;
if(LOCAL){
freopen("C:\\Users\\Smit Patel\\Downloads\\D-large.in","r",stdin);
freopen("C:\\Users\\Smit Patel\\Desktop\\out.txt","w",stdout);
}
FOR(i,1,200000)a[i]=i;
si2(n,q);
build(1,1,n);
lli ans=0;
while(q--){
si2(l,r);
if(l>r)swap(l,r);
if(l==r)goto out;
ans-=r-l-1-query(1,1,n,l+1,r-1,a[r]);
ans-=query(1,1,n,l+1,r,a[l]);
update(1,1,n,l,a[r]);
update(1,1,n,r,a[l]);
swap(a[l],a[r]);
ans+=r-l-1-query(1,1,n,l+1,r-1,a[r]);
ans+=query(1,1,n,l+1,r,a[l]);
out:
plli(ans);pn;
}
return 0;
}
ICAgICNpbmNsdWRlPGJpdHMvc3RkYysrLmg+CiAgICB1c2luZyBuYW1lc3BhY2Ugc3RkOwogICAgI2luY2x1ZGUgPGV4dC9wYl9kcy9hc3NvY19jb250YWluZXIuaHBwPgogICAgI2luY2x1ZGUgPGV4dC9wYl9kcy90cmVlX3BvbGljeS5ocHA+CiAgICB1c2luZyBuYW1lc3BhY2UgX19nbnVfcGJkczsKICAgIHRlbXBsYXRlIDx0eXBlbmFtZSBUPgogICAgdXNpbmcgb3JkZXJlZF9zZXQgPSB0cmVlPFQsIG51bGxfdHlwZSwgbGVzczxUPiwgcmJfdHJlZV90YWcsIHRyZWVfb3JkZXJfc3RhdGlzdGljc19ub2RlX3VwZGF0ZT47ICAgICAgIC8vc2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Zmlyc3QpOwogICAgICAgICAgICBhdXRvIGgyID0gc3RkOjpoYXNoPGxsaT57fShwLnNlY29uZCk7CiAgICAgICAgICAgIHJldHVybiBoMSBeIGgyOwogICAgICAgIH0KICAgIH07CgogICAgc3RydWN0IGNtcHsKICAgICAgICBib29sIG9wZXJhdG9yKCkocHJpaSBjb25zdCAmIGwscHJpaSBjb25zdCAmIHIpewogICAgICAgICAgIHJldHVybiBsLnM8ci5zOwogICAgICAgIH0KICAgIH1teW9iamVjdDsKCiAgICBvcmRlcmVkX3NldDxpbnQ+IHRyWzgwMDAxMF07CiAgICBpbnQgYVsyMDAwMTBdLG4scSxsLHI7CgogICAgdm9pZCBidWlsZChpbnQgbm9kZSxpbnQgc3QsaW50IGVuKXsKICAgICAgICBpZihzdD09ZW4pewogICAgICAgICAgICB0cltub2RlXS5pbnNlcnQoc3QpOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogICAgICAgIGludCBtaWQ9KHN0K2VuKT4+MTsKICAgICAgICBidWlsZCgyKm5vZGUsc3QsbWlkKTsKICAgICAgICBidWlsZCgyKm5vZGUrMSxtaWQrMSxlbik7CiAgICAgICAgRk9SKGksc3QsZW4pdHJbbm9kZV0uaW5zZXJ0KGkpOwogICAgfQoKICAgIHZvaWQgdXBkYXRlKGludCBub2RlLGludCBzdCxpbnQgZW4saW50IGlkeCxpbnQgdmFsKXsKICAgICAgICBpZihzdD09ZW4pewogICAgICAgICAgICB0cltub2RlXS5lcmFzZShhW2lkeF0pOwogICAgICAgICAgICB0cltub2RlXS5pbnNlcnQodmFsKTsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBpbnQgbWlkPShzdCtlbik+PjE7CiAgICAgICAgaWYoaWR4PD1taWQpCiAgICAgICAgICAgIHVwZGF0ZSgyKm5vZGUsc3QsbWlkLGlkeCx2YWwpOwogICAgICAgIGVsc2UKICAgICAgICAgICAgdXBkYXRlKDIqbm9kZSsxLG1pZCsxLGVuLGlkeCx2YWwpOwoKICAgICAgICB0cltub2RlXS5lcmFzZShhW2lkeF0pOwogICAgICAgIHRyW25vZGVdLmluc2VydCh2YWwpOwogICAgfQoKICAgIGxsaSBxdWVyeShpbnQgbm9kZSxpbnQgc3QsaW50IGVuLGludCBsLGludCByLGludCB2YWwpewogICAgICAgIGlmKGVuPGwgfHwgc3Q+ciB8fCBsPnIpcmV0dXJuIDA7CgogICAgICAgIGlmKHN0Pj1sICYmIGVuPD1yKQogICAgICAgIHJldHVybiB0cltub2RlXS5vcmRlcl9vZl9rZXkodmFsKTsKCiAgICAgICAgaW50IG1pZD0oc3QrZW4pPj4xOwogICAgICAgIHJldHVybiBxdWVyeSgyKm5vZGUsc3QsbWlkLGwscix2YWwpK3F1ZXJ5KDIqbm9kZSsxLG1pZCsxLGVuLGwscix2YWwpOwogICAgfQoKICAgIGludCBtYWluKCkKICAgIHsKCiAgICAgICAgTE9DQUw9MDsKICAgICAgICBpZihMT0NBTCl7CiAgICAgICAgICAgIGZyZW9wZW4oIkM6XFxVc2Vyc1xcU21pdCBQYXRlbFxcRG93bmxvYWRzXFxELWxhcmdlLmluIiwiciIsc3RkaW4pOwogICAgICAgICAgICBmcmVvcGVuKCJDOlxcVXNlcnNcXFNtaXQgUGF0ZWxcXERlc2t0b3BcXG91dC50eHQiLCJ3IixzdGRvdXQpOwogICAgICAgIH0KCiAgICAgICAgRk9SKGksMSwyMDAwMDApYVtpXT1pOwoKICAgICAgICBzaTIobixxKTsKCiAgICAgICAgYnVpbGQoMSwxLG4pOwoKICAgICAgICBsbGkgYW5zPTA7CiAgICAgICAgd2hpbGUocS0tKXsKICAgICAgICAgICAgc2kyKGwscik7CiAgICAgICAgICAgIGlmKGw+cilzd2FwKGwscik7CiAgICAgICAgICAgIGlmKGw9PXIpZ290byBvdXQ7CiAgICAgICAgICAgIGFucy09ci1sLTEtcXVlcnkoMSwxLG4sbCsxLHItMSxhW3JdKTsKICAgICAgICAgICAgYW5zLT1xdWVyeSgxLDEsbixsKzEscixhW2xdKTsKICAgICAgICAgICAgdXBkYXRlKDEsMSxuLGwsYVtyXSk7CiAgICAgICAgICAgIHVwZGF0ZSgxLDEsbixyLGFbbF0pOwogICAgICAgICAgICBzd2FwKGFbbF0sYVtyXSk7CiAgICAgICAgICAgIGFucys9ci1sLTEtcXVlcnkoMSwxLG4sbCsxLHItMSxhW3JdKTsKICAgICAgICAgICAgYW5zKz1xdWVyeSgxLDEsbixsKzEscixhW2xdKTsKICAgICAgICAgICAgb3V0OgogICAgICAgICAgICBwbGxpKGFucyk7cG47CiAgICAgICAgfQoKICAgICAgICByZXR1cm4gMDsKICAgIH0K