#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef pair<int,int> pii;

typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> my_super_set;

#define DIM 200007

#define MAXNUMBER 1000000003

int a[DIM];
//my_super_set T[4*DIM];
int n;

const int INF = 1000000009;

//��������� "�������" ������
struct node{
    node *left, *right; //��������� �� ����
    int key, prior, cnt; //���� �� �������, ������� �������� � �������

    node(int newkey) { //����������� ����������� ���� ���������
        key = newkey; //�������� ��������� ����
        cnt = 1; //������� �������� � �������
        prior = rand(); //������� ������� ��������
        left = right = NULL; //��� � ����� ������
    }
};

typedef node* pnode; //��������� ��� ����� pnode - �������� �� node, ��� �� ������ ����� node*

//������� ������� ������ � ������� � ������� root
int get_cnt(pnode root) {
    if(root == NULL) return 0;
    else return root->cnt;
}

//������� �������� � ������ ���� ��������� ����
void update(pnode root) {
    if(root != NULL) root->cnt = 1 + get_cnt(root->left) + get_cnt(root->right);
}

//split ������ ����� ������ ��� ����� �������� � ���� �� ����� ��������. ������� ���� � ���� ��������� �����.
pair<pnode, pnode> split(pnode root, int key){
    if(root == NULL) return {NULL,NULL}; //���� ������  �����,  �� ��������� ��������� ������

    if(root->key < key) { //���� ���� ����� ����� ����� ������ - �� �������� ������� ����
        pair<pnode, pnode> sp = split(root->right, key); //�������� ����� ����������� �� �������� �����
        root->right = sp.first; //������ ��� ����� ������� ������� ������ ������
        update(root); //���������  �������� ���� ����������� ����
        return {root, sp.second}; //��������� ��������� ���������
    } else { //����������� ������� ��� ��� �������
        pair<pnode, pnode> sp = split(root->left, key);
        root->left = sp.second;
        update(root); //���������  �������� ���� ����������� ����
        return {sp.first,  root}; //��� ��� �� ������, �� ������� ���������� ��� �����������
    }
}

//����� ��� ���������� ������ L,R ����� �� �Ѳ ����� � L �� ����� �� ����� � R
pnode merge(pnode L, pnode R) {
    if(L == NULL) return R; //���� ��� ������ ����� - �� ��������� ������  ������� �������
    if(R == NULL) return L; //���������
    //����� ������� ���� ������ ������ �� ����. ������� �� ����� �� ������� � ��� ������ �������
    if(L->prior > R->prior) { //���� ������� ������ � ����� ������,
        L->right = merge(L->right, R); //�� ������� ������� ���� L � ������ �������
        update(L); //���������  �������� ���� ����������� ����
        return L; //��������� ����� ������� ������
    } else { //����������� ������
        R->left = merge(L, R->left);
        update(R); //���������  �������� ���� ����������� ����
        return R;
    }
}

void insert(pnode &root, pnode &t) {
    if(root == NULL) {
        root = t;
        return;
    }
    if(root->prior > t->prior) {
        if(t->key < root->key)
            insert(root->left, t);
        else
            insert(root->right, t);
        return;
    }
    pair<pnode, pnode> temp = split(root, t->key);
    root = t;
    root->left = temp.first;
    root->right = temp.second;
}

void addnode(pnode &root, int key) {
    pnode t = new node(key);
    insert(root, t);
}

/*
//���������� ���� ������� � ������ (����� ���� �������� �� ������ ���� ��������������)
void insert(pnode &root,  int  key) { // &root - �� ����� ��������
    pair<pnode, pnode> sp = split(root, key); //�������� ������ �� ����� ���� ���� ��������
    //����� sp.first �� ����� �� ����� �� key , � ����� ������ � sp.second
    //������� sp.first, ������ � ���� ���� �������, � sp.second
    root = merge(merge(sp.first, new node(key)), sp.second);
}
*/

//����� ��� �������� � ������� root, ��������� ������� ���'��� (���� �� �����)
void dispose(pnode root) {
    if(root == NULL) return;
    dispose(root->left); //������� ����
    dispose(root->right);
    delete(root); //���������� ���'��� ��� ������. (������������  root = new node()
}

void erase(pnode &root, int k) {
    if(root == NULL) return;
    else if(k < root->key) erase(root->left, k);
    else if(k > root->key) erase(root->right, k);
    else root = merge(root->left, root->right);
    update(root);
}

/*

//��������� �������� � ������ �
void erase(pnode &root, int k) {
    pair<pnode, pnode> sp1, sp2;
    sp1 = split(root, k); //������� ������ �� ����� �
    sp2 = split(sp1.second, k+1); //�������� ����� ������ �� ����� �+1

    dispose(sp2.first);//��������� ���'��� � ������ � ������� �

    root = merge(sp1.first, sp2.second); //����� ������ �� �� ������ ������ �

}
*/

int find_kth(pnode root, int k) {
    if(get_cnt(root) < k) return -INF;

    int inright = get_cnt(root->right);

    if(inright + 1 == k) return root->key;

    if(inright >= k) return find_kth(root->right, k);

    return find_kth(root->left, k - (inright + 1));

}

int order_of_key(pnode root, int k) {
    if(root == NULL) return 0;
    if(root->key == k) return get_cnt(root->left);
    else if(k < root->key) return order_of_key(root->left, k);
    else return get_cnt(root->left) + 1 + order_of_key(root->right, k);
}
///////////////////////////////////////////////////////

pnode T[4*DIM];

void buildtree(int v, int tl, int tr) {

    for(int i = tl ; i <= tr; i++) addnode(T[v], a[i]);

    if(tl == tr) {
        return;
    }

    int tm = (tl+tr) / 2;
    buildtree(2*v+1, tl, tm);
    buildtree(2*v+2, tm+1, tr);
}

void addpoint(int v, int tl, int tr, int pos, int val, int type) {
    if(pos < tl || tr < pos) return;

    if(type == 1) addnode(T[v], val);
    else erase(T[v], val);

    if(tl == tr) {
        return;
    }

    int tm = (tl+tr)/2;
    if(pos <= tm) addpoint(2*v+1, tl, tm, pos, val, type);
    else addpoint(2*v+2, tm+1, tr, pos, val, type);
}

int query(int v, int tl, int tr, int L, int R, int x, int type) {
    if(R < tl || tr < L) return 0;

    if(L <= tl && tr <= R) {
        if(type == 1)return order_of_key(T[v],x);
        else return (tr-tl+1) - order_of_key(T[v], x+1);
    }

    int tm = (tl+tr) / 2;
    return query(2*v+1, tl, tm, L, R, x, type) + query(2*v+2, tm+1, tr, L, R, x, type);
}

int changepoint(int pos, int newval) {
    int c = 0;
    c -= query(0, 1, n, 1, pos-1, a[pos], 2);
    c -= query(0, 1, n, pos+1, n, a[pos], 1);
    addpoint(0, 1, n, pos, a[pos], 2);
    a[pos] = newval;
    addpoint(0, 1, n, pos, newval, 1);
    c += query(0, 1, n, 1, pos-1, a[pos], 2);
    c += query(0, 1, n, pos+1, n, a[pos], 1);
    return c;
}

int makeswap(int L, int R) {
    if(L == R) return 0;
    int t1,t2,t3,t4,add;
    t1 = query(0, 1, n, L+1, R-1, a[L], 1);
    t2 = R-L-1 - t1;

    t4 = query(0, 1, n, L+1, R-1, a[R], 1);
    t3 = R-L-1 - t4;
    add = -t1 + t2 + - t3 + t4;
    if(a[L] < a[R]) add++;
    else add--;

    int oldl = a[L], oldr = a[R];

    addpoint(0, 1, n, L, a[L], 2);
    addpoint(0, 1, n, R, a[R], 2);
    addpoint(0, 1, n, L, a[R], 1);
    addpoint(0, 1, n, R, a[L], 1);

    swap(a[L], a[R]);

    return add;
}

int main() {
  //freopen("input.txt", "r", stdin);

  int xx, yy, m, oldxx, oldyy;
  long long res;

  scanf("%d %d", &n, &m);
  //n = 200000; m = 50000;
  for(int i = 1; i <= n; i++) a[i] = i;
  buildtree(0, 1, n);
//return 0;
  res = 0;

  for(int i = 1; i <= m; i++) {
    scanf("%d %d", &xx, &yy);
/*
    xx = rand() % 200000 + 1;
    yy = rand() % 200000 + 1;
*/
    if(xx > yy) swap(xx,yy);
    res += makeswap(xx,yy);
    printf("%lld\n", res);
  }

  return 0;
}


#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef pair<int,int> pii;

typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> my_super_set;

#define DIM 200007

#define MAXNUMBER 1000000003

int a[DIM];
//my_super_set T[4*DIM];
int n;

///////////////////////////////////////////////////////

const int INF = 1000000009;

//��������� "�������" ������
struct node{
    node *left, *right; //��������� �� ����
    int key, prior, cnt; //���� �� �������, ������� �������� � �������

    node(int newkey) { //����������� ����������� ���� ���������
        key = newkey; //�������� ��������� ����
        cnt = 1; //������� �������� � �������
        prior = rand(); //������� ������� ��������
        left = right = NULL; //��� � ����� ������
    }
};

typedef node* pnode; //��������� ��� ����� pnode - �������� �� node, ��� �� ������ ����� node*

//������� ������� ������ � ������� � ������� root
int get_cnt(pnode root) {
    if(root == NULL) return 0;
    else return root->cnt;
}

//������� �������� � ������ ���� ��������� ����
void update(pnode root) {
    if(root != NULL) root->cnt = 1 + get_cnt(root->left) + get_cnt(root->right);
}

//split ������ ����� ������ ��� ����� �������� � ���� �� ����� ��������. ������� ���� � ���� ��������� �����.
pair<pnode, pnode> split(pnode root, int key){
    if(root == NULL) return {NULL,NULL}; //���� ������  �����,  �� ��������� ��������� ������

    if(root->key < key) { //���� ���� ����� ����� ����� ������ - �� �������� ������� ����
        pair<pnode, pnode> sp = split(root->right, key); //�������� ����� ����������� �� �������� �����
        root->right = sp.first; //������ ��� ����� ������� ������� ������ ������
        update(root); //���������  �������� ���� ����������� ����
        return {root, sp.second}; //��������� ��������� ���������
    } else { //����������� ������� ��� ��� �������
        pair<pnode, pnode> sp = split(root->left, key);
        root->left = sp.second;
        update(root); //���������  �������� ���� ����������� ����
        return {sp.first,  root}; //��� ��� �� ������, �� ������� ���������� ��� �����������
    }
}

//����� ��� ���������� ������ L,R ����� �� �Ѳ ����� � L �� ����� �� ����� � R
pnode merge(pnode L, pnode R) {
    if(L == NULL) return R; //���� ��� ������ ����� - �� ��������� ������  ������� �������
    if(R == NULL) return L; //���������
    //����� ������� ���� ������ ������ �� ����. ������� �� ����� �� ������� � ��� ������ �������
    if(L->prior > R->prior) { //���� ������� ������ � ����� ������,
        L->right = merge(L->right, R); //�� ������� ������� ���� L � ������ �������
        update(L); //���������  �������� ���� ����������� ����
        return L; //��������� ����� ������� ������
    } else { //����������� ������
        R->left = merge(L, R->left);
        update(R); //���������  �������� ���� ����������� ����
        return R;
    }
}

//���������� ���� ������� � ������ (����� ���� �������� �� ������ ���� ��������������)
void insert(pnode &root,  int  key) { // &root - �� ����� ��������
    pair<pnode, pnode> sp = split(root, key); //�������� ������ �� ����� ���� ���� ��������
    //����� sp.first �� ����� �� ����� �� key , � ����� ������ � sp.second
    //������� sp.first, ������ � ���� ���� �������, � sp.second
    root = merge(merge(sp.first, new node(key)), sp.second);
}

//����� ��� �������� � ������� root, ��������� ������� ���'��� (���� �� �����)
void dispose(pnode root) {
    if(root == NULL) return;
    dispose(root->left); //������� ����
    dispose(root->right);
    delete(root); //���������� ���'��� ��� ������. (������������  root = new node()
}

//��������� �������� � ������ �
void erase(pnode &root, int k) {
    pair<pnode, pnode> sp1, sp2;
    sp1 = split(root, k); //������� ������ �� ����� �
    sp2 = split(sp1.second, k+1); //�������� ����� ������ �� ����� �+1

    dispose(sp2.first);//��������� ���'��� � ������ � ������� �

    root = merge(sp1.first, sp2.second); //����� ������ �� �� ������ ������ �
}

int find_kth(pnode root, int k) {
    if(get_cnt(root) < k) return -INF;

    int inright = get_cnt(root->right);

    if(inright + 1 == k) return root->key;

    if(inright >= k) return find_kth(root->right, k);

    return find_kth(root->left, k - (inright + 1));

}

int order_of_key(pnode root, int k) {
    if(root == NULL) return 0;
    if(root->key == k) return get_cnt(root->left);
    else if(k < root->key) return order_of_key(root->left, k);
    else return get_cnt(root->left) + 1 + order_of_key(root->right, k);
}
///////////////////////////////////////////////////////

pnode T[4*DIM];

void buildtree(int v, int tl, int tr) {

    for(int i = tl ; i <= tr; i++) insert(T[v], a[i]);

    if(tl == tr) {
        return;
    }

    int tm = (tl+tr) / 2;
    buildtree(2*v+1, tl, tm);
    buildtree(2*v+2, tm+1, tr);
}

void addpoint(int v, int tl, int tr, int pos, int val, int type) {
    if(pos < tl || tr < pos) return;

    if(type == 1) insert(T[v], val);
    else erase(T[v], val);

    if(tl == tr) {
        return;
    }

    int tm = (tl+tr)/2;
    if(pos <= tm) addpoint(2*v+1, tl, tm, pos, val, type);
    else addpoint(2*v+2, tm+1, tr, pos, val, type);
}

int query(int v, int tl, int tr, int L, int R, int x, int type) {
    if(R < tl || tr < L) return 0;

    if(L <= tl && tr <= R) {
        if(type == 1)return order_of_key(T[v],x);
        else return (tr-tl+1) - order_of_key(T[v], x+1);
    }

    int tm = (tl+tr) / 2;
    return query(2*v+1, tl, tm, L, R, x, type) + query(2*v+2, tm+1, tr, L, R, x, type);
}

int changepoint(int pos, int newval) {
    int c = 0;
    c -= query(0, 1, n, 1, pos-1, a[pos], 2);
    c -= query(0, 1, n, pos+1, n, a[pos], 1);
    addpoint(0, 1, n, pos, a[pos], 2);
    a[pos] = newval;
    addpoint(0, 1, n, pos, newval, 1);
    c += query(0, 1, n, 1, pos-1, a[pos], 2);
    c += query(0, 1, n, pos+1, n, a[pos], 1);
    return c;
}

int makeswap(int L, int R) {
    if(L == R) return 0;
    int t1,t2,t3,t4,add;
    t1 = query(0, 1, n, L+1, R-1, a[L], 1);
    t2 = R-L-1 - t1;

    t4 = query(0, 1, n, L+1, R-1, a[R], 1);
    t3 = R-L-1 - t4;
    add = -t1 + t2 + - t3 + t4;
    if(a[L] < a[R]) add++;
    else add--;

    addpoint(0, 1, n, L, a[L], 2);
    addpoint(0, 1, n, L, a[R], 1);
    addpoint(0, 1, n, R, a[R], 2);
    addpoint(0, 1, n, R, a[L], 1);
    swap(a[L], a[R]);

    return add;
}

int main() {
  //freopen("input.txt", "r", stdin);
/*
  int t1,t2,t3;

  t2 = clock();
  for(int i = 1; i <= 2000000; i++) insert(T[0], i);

  t3 = clock();

  cout<<t2-t1<<endl<<t3-t2<<endl;

  return 0;
*/
  int xx, yy, m, res, oldxx, oldyy;

  scanf("%d %d", &n, &m);
  //n = 200000; m = 50000;
  for(int i = 1; i <= n; i++) a[i] = i;
  buildtree(0, 1, n);
//return 0;
  res = 0;

  for(int i = 1; i <= m; i++) {
    scanf("%d %d", &xx, &yy);
/*
    xx = rand() % 200000 + 1;
    yy = rand() % 200000 + 1;
*/
    if(xx > yy) swap(xx,yy);
    res += makeswap(xx,yy);
    /*
    oldxx = a[xx]; oldyy = a[yy];
    res += changepoint(xx,oldyy);
    res += changepoint(yy, oldxx);
    */
    printf("%d\n", res);
  }

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define DIM 200007
#define MAXNUMBER 200003

#define NUMBLOCK 507

#define BSIZE 500

using namespace std;

int a[DIM];
int n;
int F[NUMBLOCK][MAXNUMBER+7];
int bs[NUMBLOCK];

int sumprefix(int bnum, int y) {
    y++;
    int res = 0;
    for(int i = y; i > 0; i -= i & -i)
        res += F[bnum][i];
    return res;
}

void add(int bnum, int y, int delta) {
    y++;
    for(int i = y; i <= MAXNUMBER; i += i & -i)
        F[bnum][i] += delta;
}

void changevalue(int pos, int newval) {
    add(pos/BSIZE, a[pos], -1);
    add(pos/BSIZE, newval, 1);
    a[pos] = newval;
}

int query(int x1, int x2, int yy) {
    int b1,b2,res = 0;
    b1 = x1 / BSIZE;
    b2 = x2 / BSIZE;

    if(b1 == b2) {
        for(int j = x1; j <= x2; j++)
            if(a[j] < yy) res++;
    } else {
        for(int j = x1; j < (b1+1)*BSIZE; j++)
            if(a[j] < yy) res++;

        for(int j = b1+1; j <= b2-1; j++)
            res += sumprefix(j, yy-1);

        for(int j = (b2*BSIZE); j <= x2; j++)
            if(a[j] < yy) res++;

    }
    return res;
}

int makeswap(int L, int R) {
    if(L == R) return 0;
    int t1,t2,t3,t4,add;
    t1 = query(L+1, R-1, a[L]);
    t2 = R-L-1 - t1;

    t4 = query(L+1, R-1, a[R]);
    t3 = R-L-1 - t4;
    add = -t1 + t2 + - t3 + t4;
    if(a[L] < a[R]) add++;
    else add--;

    int oldl = a[L], oldr = a[R];

    changevalue(L, oldr);
    changevalue(R, oldl);
    //swap(a[L], a[R]);

    return add;
}

int main() {
  //freopen("input.txt", "r", stdin);

  int xx, yy, m, oldxx, oldyy;
  long long res;

  scanf("%d %d", &n, &m);
  //n = 200000; m = 50000;
  for(int i = 1; i <= n; i++) a[i] = i;
  for(int i = 1; i <= n; i++) {
    add(i / BSIZE, a[i], 1);
    bs[i/BSIZE]++;
  }

  res = 0;

  for(int i = 1; i <= m; i++) {
    scanf("%d %d", &xx, &yy);
/*
    xx = rand() % 200000 + 1;
    yy = rand() % 200000 + 1;
*/
    if(xx > yy) swap(xx,yy);
    res += makeswap(xx,yy);
    printf("%lld\n", res);
  }

  return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>

using namespace std;

typedef long long ll;

struct Tree {
    int key;
    int priority;
    int size;
    Tree *left, *right;
    Tree(int key) :
            key(key),
            priority(rand()),
            size(1),
            left(nullptr),
            right(nullptr) { }
};

void inorderBad(Tree *tree) {
    if (!tree) {
        return;
    }
    inorderBad(tree->left);
    cout << tree->key << " ";
    inorderBad(tree->right);
}

void inorder(Tree *tree) {
    inorderBad(tree);
    cout << "\n";
}

inline int getSize(Tree *tree) {
    if (!tree) {
        return 0;
    }
    return tree->size;
}

inline void recalc(Tree *tree) {
    if (!tree) {
        return;
    }
    tree->size = getSize(tree->left) + 1 + getSize(tree->right);
}

pair<Tree *, Tree *> split(Tree *tree, int key) {
    if (!tree) {
        return {nullptr, nullptr};
    }
    if (tree->key > key) {
        auto p = split(tree->left, key);
        tree->left = p.second;
        recalc(tree);
        return {p.first, tree};
    } else {
        auto p = split(tree->right, key);
        tree->right = p.first;
        recalc(tree);
        return {tree, p.second};
    }
};

Tree *merge(Tree *ta, Tree *tb) {
    if (!ta) {
        return tb;
    }
    if (!tb) {
        return ta;
    }
    if (ta->priority < tb->priority) {
        ta->right = merge(ta->right, tb);
        recalc(ta);
        return ta;
    } else {
        tb->left = merge(ta, tb->left);
        recalc(tb);
        return tb;
    }
}

inline Tree *insert(Tree *tree, int key) {
    Tree *ntree = new Tree(key);
    auto p = split(tree, key);
    return merge(merge(p.first, ntree), p.second);
}

inline Tree *erase(Tree *tree, int key) {
    auto p = split(tree, key);
    auto p1 = split(p.first, key - 1);
    return merge(p1.first, p.second);
}

inline Tree *numLess(Tree *tree, int key, int &res) {
    auto p = split(tree, key - 1);
    res = getSize(p.first);
    return merge(p.first, p.second);
}

int n, sqn;

inline int ind(int i) {
    return i / sqn;
}

vector<int> a;
vector<Tree *> blocks;

ll ans = 0;

inline void update(int l, int r) {
    if (l == r) {
        return;
    }
    int st = ind(l), fn = ind(r);
    ll numMoreL = 0, numLessR = 0;
    for (int i = st; i <= fn; ++i) {
        if (l < i * sqn && (i + 1) * sqn <= r) {
            int res = 0;
            if (a[r] == n) {
                res = getSize(blocks[i]);
            } else if (a[r] == 1) {
                res = 0;
            } else {
                blocks[i] = numLess(blocks[i], a[r], res);
            }
            numLessR += res;
            if (a[l] == 1) {
                res = getSize(blocks[i]);
            } else if (a[l] == n) {
                res = 0;
            } else {
                blocks[i] = numLess(blocks[i], a[l], res);
            }
            numMoreL += getSize(blocks[i]) - res;
            continue;
        }
        for (int j = max(l + 1, i * sqn); j < min(r, (i + 1) * sqn); ++j) {
            numLessR += a[j] < a[r];
            numMoreL += a[j] > a[l];
        }
    }
    ans += numLessR - (r - l - 1 - numLessR);
    ans += numMoreL - (r - l - 1 - numMoreL);
    ans += 1 - 2 * (a[l] > a[r]);
    blocks[st] = erase(blocks[st], a[l]);
    blocks[fn] = erase(blocks[fn], a[r]);
    blocks[st] = insert(blocks[st], a[r]);
    blocks[fn] = insert(blocks[fn], a[l]);
    swap(a[l], a[r]);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    int q;
    cin >> n >> q;
    sqn = int(sqrt(n));
    a.resize((unsigned) n);
    for (int i = 0; i < n; ++i) {
        if (i % sqn == 0) {
            blocks.push_back(nullptr);
        }
        a[i] = i + 1;
        blocks.back() = insert(blocks.back(), i + 1);
    }
    for (int i = 0; i < q; ++i) {
        int l, r;
        cin >> l >> r;
        --l, --r;
        if (l > r) {
            swap(l, r);
        }
        update(l, r);
        cout << ans << "\n";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>

using namespace std;

typedef long long ll;

struct Tree {
    int key;
    int priority;
    int size;
    Tree *left, *right;
    Tree(int key) :
            key(key),
            priority(rand()),
            size(1),
            left(nullptr),
            right(nullptr) { }
};

void inorderBad(Tree *tree) {
    if (!tree) {
        return;
    }
    inorderBad(tree->left);
    cout << tree->key << " ";
    inorderBad(tree->right);
}

void inorder(Tree *tree) {
    inorderBad(tree);
    cout << "\n";
}

inline int getSize(Tree *tree) {
    if (!tree) {
        return 0;
    }
    return tree->size;
}

inline void recalc(Tree *tree) {
    if (!tree) {
        return;
    }
    tree->size = getSize(tree->left) + 1 + getSize(tree->right);
}

pair<Tree *, Tree *> split(Tree *tree, int key) {
    if (!tree) {
        return {nullptr, nullptr};
    }
    if (tree->key > key) {
        auto p = split(tree->left, key);
        tree->left = p.second;
        recalc(tree);
        return {p.first, tree};
    } else {
        auto p = split(tree->right, key);
        tree->right = p.first;
        recalc(tree);
        return {tree, p.second};
    }
};

Tree *merge(Tree *ta, Tree *tb) {
    if (!ta) {
        return tb;
    }
    if (!tb) {
        return ta;
    }
    if (ta->priority < tb->priority) {
        ta->right = merge(ta->right, tb);
        recalc(ta);
        return ta;
    } else {
        tb->left = merge(ta, tb->left);
        recalc(tb);
        return tb;
    }
}

inline Tree *insert(Tree *tree, int key) {
    Tree *ntree = new Tree(key);
    auto p = split(tree, key);
    return merge(merge(p.first, ntree), p.second);
}

inline Tree *erase(Tree *tree, int key) {
    auto p = split(tree, key);
    auto p1 = split(p.first, key - 1);
    return merge(p1.first, p.second);
}

inline Tree *numLess(Tree *tree, int key, int &res) {
    auto p = split(tree, key - 1);
    res = getSize(p.first);
    return merge(p.first, p.second);
}

int n, sqn;

inline int ind(int i) {
    return i / sqn;
}

vector<int> a;
vector<Tree *> blocks;

ll ans = 0;

inline void update(int l, int r) {
    if (l == r) {
        return;
    }
    int st = ind(l), fn = ind(r);
    ll numMoreL = 0, numLessR = 0;
    for (int i = st; i <= fn; ++i) {
        if (l < i * sqn && (i + 1) * sqn <= r) {
            int res = 0;
            if (a[r] == n) {
                res = getSize(blocks[i]);
            } else if (a[r] == 1) {
                res = 0;
            } else {
                blocks[i] = numLess(blocks[i], a[r], res);
            }
            numLessR += res;
            if (a[l] == 1) {
                res = 0;
            } else if (a[l] == n) {
                res = getSize(blocks[i]);
            } else {
                blocks[i] = numLess(blocks[i], a[l], res);
            }
            numMoreL += getSize(blocks[i]) - res;
            continue;
        }
        for (int j = max(l + 1, i * sqn); j < min(r, (i + 1) * sqn); ++j) {
            numLessR += a[j] < a[r];
            numMoreL += a[j] > a[l];
        }
    }
    ans += numLessR - (r - l - 1 - numLessR);
    ans += numMoreL - (r - l - 1 - numMoreL);
    ans += 1 - 2 * (a[l] > a[r]);
    blocks[st] = erase(blocks[st], a[l]);
    blocks[fn] = erase(blocks[fn], a[r]);
    blocks[st] = insert(blocks[st], a[r]);
    blocks[fn] = insert(blocks[fn], a[l]);
    swap(a[l], a[r]);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    int q;
    cin >> n >> q;
    sqn = int(sqrt(n));
    a.resize((unsigned) n);
    for (int i = 0; i < n; ++i) {
        if (i % sqn == 0) {
            blocks.push_back(nullptr);
        }
        a[i] = i + 1;
        blocks.back() = insert(blocks.back(), i + 1);
    }
    for (int i = 0; i < q; ++i) {
        int l, r;
        cin >> l >> r;
        --l, --r;
        if (l > r) {
            swap(l, r);
        }
        update(l, r);
        cout << ans << "\n";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>

using namespace std;

typedef long long ll;

int n, sqn;

inline int ind(int i) {
    return i / sqn;
}

vector<int> a;
vector<vector<int>> blocks;

int binSerach(int i, int key) {
    int l = -1, r = blocks[i].size();
    while (l < r - 1) {
        int m = (l + r) / 2;
        if (blocks[i][m] < key) {
            l = m;
        } else {
            r = m;
        }
    }
    return r;
}

ll ans = 0;

inline void update(int l, int r) {
    if (l == r) {
        return;
    }
    int st = ind(l), fn = ind(r);
    ll numMoreL = 0, numLessR = 0;
    for (int i = st; i <= fn; ++i) {
        if (l < i * sqn && (i + 1) * sqn <= r) {
            numLessR += binSerach(i, a[r]);
            numMoreL += blocks[i].size() - binSerach(i, a[l]);
            continue;
        }
        for (int j = max(l + 1, i * sqn); j < min(r, (i + 1) * sqn); ++j) {
            numLessR += a[j] < a[r];
            numMoreL += a[j] > a[l];
        }
    }
    ans += numLessR - (r - l - 1 - numLessR);
    ans += numMoreL - (r - l - 1 - numMoreL);
    ans += 1 - 2 * (a[l] > a[r]);
    blocks[st].erase(blocks[st].begin() + binSerach(st, a[l]));
    blocks[fn].erase(blocks[fn].begin() + binSerach(fn, a[r]));
    blocks[st].insert(blocks[st].begin() + binSerach(st, a[r]), a[r]);
    blocks[fn].insert(blocks[fn].begin() + binSerach(fn, a[l]), a[l]);
    swap(a[l], a[r]);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    int q;
    cin >> n >> q;
    sqn = int(sqrt(n));
    a.resize((unsigned) n);
    for (int i = 0; i < n; ++i) {
        if (i % sqn == 0) {
            blocks.push_back({});
        }
        a[i] = i + 1;
        blocks.back().push_back(i + 1);
    }
    for (int i = 0; i < q; ++i) {
        int l, r;
        cin >> l >> r;
        --l, --r;
        if (l > r) {
            swap(l, r);
        }
        update(l, r);
        cout << ans << "\n";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>

using namespace std;

typedef long long ll;

int n, sqn;

inline int ind(int i) {
    return i / sqn;
}

vector<int> a;
vector<vector<int>> blocks;

int binSerach(int i, int key) {
    int l = -1, r = blocks[i].size();
    while (l < r - 1) {
        int m = (l + r) / 2;
        if (blocks[i][m] < key) {
            l = m;
        } else {
            r = m;
        }
    }
    return r;
}

ll ans = 0;

inline void update(int l, int r) {
    if (l == r) {
        return;
    }
    int st = ind(l), fn = ind(r);
    ll numMoreL = 0, numLessR = 0;
    for (int i = st; i <= fn; ++i) {
        if (l < i * sqn && (i + 1) * sqn <= r) {
            numLessR += binSerach(i, a[r]);
            numMoreL += blocks[i].size() - binSerach(i, a[l]);
            continue;
        }
        for (int j = max(l + 1, i * sqn); j < min(r, (i + 1) * sqn); ++j) {
            numLessR += a[j] < a[r];
            numMoreL += a[j] > a[l];
        }
    }
    ans += numLessR - (r - l - 1 - numLessR);
    ans += numMoreL - (r - l - 1 - numMoreL);
    ans += 1 - 2 * (a[l] > a[r]);
    blocks[st].erase(blocks[st].begin() + binSerach(st, a[l]) - 1);
    blocks[fn].erase(blocks[fn].begin() + binSerach(fn, a[r]) - 1);
    blocks[st].insert(blocks[st].begin() + binSerach(st, a[r]), a[r]);
    blocks[fn].insert(blocks[fn].begin() + binSerach(fn, a[l]), a[l]);
    swap(a[l], a[r]);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    int q;
    cin >> n >> q;
    sqn = int(sqrt(n));
    a.resize((unsigned) n);
    for (int i = 0; i < n; ++i) {
        if (i % sqn == 0) {
            blocks.push_back({});
        }
        a[i] = i + 1;
        blocks.back().push_back(i + 1);
    }
    for (int i = 0; i < q; ++i) {
        int l, r;
        cin >> l >> r;
        --l, --r;
        if (l > r) {
            swap(l, r);
        }
        update(l, r);
        cout << ans << "\n";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>

using namespace std;

typedef long long ll;

struct Tree {
    int key;
    int priority;
    int size;
    Tree *left, *right;
    Tree(int key) :
            key(key),
            priority(rand()),
            size(1),
            left(nullptr),
            right(nullptr) { }
};

void inorderBad(Tree *tree) {
    if (!tree) {
        return;
    }
    inorderBad(tree->left);
    cout << tree->key << " ";
    inorderBad(tree->right);
}

void inorder(Tree *tree) {
    inorderBad(tree);
    cout << "\n";
}

inline int getSize(Tree *tree) {
    if (!tree) {
        return 0;
    }
    return tree->size;
}

inline void recalc(Tree *tree) {
    if (!tree) {
        return;
    }
    tree->size = getSize(tree->left) + 1 + getSize(tree->right);
}

pair<Tree *, Tree *> split(Tree *tree, int key) {
    if (!tree) {
        return {nullptr, nullptr};
    }
    if (tree->key > key) {
        auto p = split(tree->left, key);
        tree->left = p.second;
        recalc(tree);
        return {p.first, tree};
    } else {
        auto p = split(tree->right, key);
        tree->right = p.first;
        recalc(tree);
        return {tree, p.second};
    }
};

Tree *merge(Tree *ta, Tree *tb) {
    if (!ta) {
        return tb;
    }
    if (!tb) {
        return ta;
    }
    if (ta->priority < tb->priority) {
        ta->right = merge(ta->right, tb);
        recalc(ta);
        return ta;
    } else {
        tb->left = merge(ta, tb->left);
        recalc(tb);
        return tb;
    }
}

inline Tree *insert(Tree *tree, int key) {
    Tree *ntree = new Tree(key);
    auto p = split(tree, key);
    return merge(merge(p.first, ntree), p.second);
}

inline Tree *erase(Tree *tree, int key) {
    auto p = split(tree, key);
    auto p1 = split(p.first, key - 1);
    return merge(p1.first, p.second);
}

inline Tree *numLess(Tree *tree, int key, int &res) {
    auto p = split(tree, key - 1);
    res = getSize(p.first);
    return merge(p.first, p.second);
}

int n, sqn;

inline int ind(int i) {
    return i / sqn;
}

vector<int> a;
vector<Tree *> blocks;

ll ans = 0;

inline void update(int l, int r) {
    if (l == r) {
        return;
    }
    int st = ind(l), fn = ind(r);
    ll numMoreL = 0, numLessR = 0;
    for (int i = st; i <= fn; ++i) {
        if (l < i * sqn && (i + 1) * sqn <= r) {
            int res = 0;
            blocks[i] = numLess(blocks[i], a[r], res);
            numLessR += res;
            blocks[i] = numLess(blocks[i], a[l], res);
            numMoreL += getSize(blocks[i]) - res;
            continue;
        }
        for (int j = max(l + 1, i * sqn); j < min(r, (i + 1) * sqn); ++j) {
            numLessR += a[j] < a[r];
            numMoreL += a[j] > a[l];
        }
    }
    ans += numLessR - (r - l - 1 - numLessR);
    ans += numMoreL - (r - l - 1 - numMoreL);
    ans += 1 - 2 * (a[l] > a[r]);
    blocks[st] = erase(blocks[st], a[l]);
    blocks[fn] = erase(blocks[fn], a[r]);
    blocks[st] = insert(blocks[st], a[r]);
    blocks[fn] = insert(blocks[fn], a[l]);
    swap(a[l], a[r]);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    int q;
    cin >> n >> q;
    sqn = int(sqrt(n));
    a.resize((unsigned) n);
    for (int i = 0; i < n; ++i) {
        if (i % sqn == 0) {
            blocks.push_back(nullptr);
        }
        a[i] = i + 1;
        blocks.back() = insert(blocks.back(), i + 1);
    }
    for (int i = 0; i < q; ++i) {
        int l, r;
        cin >> l >> r;
        --l, --r;
        if (l > r) {
            swap(l, r);
        }
        update(l, r);
        cout << ans << "\n";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>

using namespace std;

typedef long long ll;

struct Tree {
    int key;
    int priority;
    int size;
    Tree *left, *right;
    Tree(int key) :
            key(key),
            priority(rand()),
            size(1),
            left(nullptr),
            right(nullptr) { }
};

void inorderBad(Tree *tree) {
    if (!tree) {
        return;
    }
    inorderBad(tree->left);
    cout << tree->key << " ";
    inorderBad(tree->right);
}

void inorder(Tree *tree) {
    inorderBad(tree);
    cout << "\n";
}

int getSize(Tree *tree) {
    if (!tree) {
        return 0;
    }
    return tree->size;
}

void recalc(Tree *tree) {
    if (!tree) {
        return;
    }
    tree->size = getSize(tree->left) + 1 + getSize(tree->right);
}

pair<Tree *, Tree *> split(Tree *tree, int key) {
    if (!tree) {
        return {nullptr, nullptr};
    }
    if (tree->key > key) {
        auto p = split(tree->left, key);
        tree->left = p.second;
        recalc(tree);
        return {p.first, tree};
    } else {
        auto p = split(tree->right, key);
        tree->right = p.first;
        recalc(tree);
        return {tree, p.second};
    }
};

Tree *merge(Tree *ta, Tree *tb) {
    if (!ta) {
        return tb;
    }
    if (!tb) {
        return ta;
    }
    if (ta->priority < tb->priority) {
        ta->right = merge(ta->right, tb);
        recalc(ta);
        return ta;
    } else {
        tb->left = merge(ta, tb->left);
        recalc(tb);
        return tb;
    }
}

Tree *insert(Tree *tree, int key) {
    Tree *ntree = new Tree(key);
    auto p = split(tree, key);
    return merge(merge(p.first, ntree), p.second);
}

Tree *erase(Tree *tree, int key) {
    auto p = split(tree, key);
    auto p1 = split(p.first, key - 1);
    return merge(p1.first, p.second);
}

Tree *numLess(Tree *tree, int key, int &res) {
    auto p = split(tree, key - 1);
    res = getSize(p.first);
    return merge(p.first, p.second);
}

int n, sqn;

int ind(int i) {
    return i / sqn;
}

vector<int> a;
vector<Tree *> blocks;

ll ans = 0;

void update(int l, int r) {
    if (l == r) {
        return;
    }
    int st = ind(l), fn = ind(r);
    ll numMoreL = 0, numLessR = 0;
    for (int i = st; i <= fn; ++i) {
//        inorder(blocks[i]);
        if (l < i * sqn && (i + 1) * sqn <= r) {
            int res = 0;
            blocks[i] = numLess(blocks[i], a[r], res);
            numLessR += res;
//            cout << res << " ";
            blocks[i] = numLess(blocks[i], a[l], res);
            numMoreL += getSize(blocks[i]) - res;
//            cout << getSize(blocks[i]) - res << "\n";
            continue;
        }
        for (int j = max(l + 1, i * sqn); j < min(r, (i + 1) * sqn); ++j) {
            numLessR += a[j] < a[r];
            numMoreL += a[j] > a[l];
        }
    }
//    cout << numLessR << " " << numMoreL << "\n";
    ans += numLessR - (r - l - 1 - numLessR);
    ans += numMoreL - (r - l - 1 - numMoreL);
    ans += 1 - 2 * (a[l] > a[r]);
    blocks[st] = erase(blocks[st], a[l]);
    blocks[fn] = erase(blocks[fn], a[r]);
    blocks[st] = insert(blocks[st], a[r]);
    blocks[fn] = insert(blocks[fn], a[l]);
    swap(a[l], a[r]);
}

int main() {
    int q;
    cin >> n >> q;
    sqn = int(sqrt(n));
    a.resize((unsigned) n);
    for (int i = 0; i < n; ++i) {
        if (i % sqn == 0) {
            blocks.push_back(nullptr);
        }
        a[i] = i + 1;
        blocks.back() = insert(blocks.back(), i + 1);
    }
    for (int i = 0; i < q; ++i) {
        int l, r;
        cin >> l >> r;
        --l, --r;
        if (l > r) {
            swap(l, r);
        }
        update(l, r);
//        for (auto b : blocks) {
//            inorder(b);
//        }
//        for (int j = 0; j < n; ++j) {
//            cout << a[j] << " ";
//        }
//        cout << "\n";
        cout << ans << "\n";
    }
//    system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>

using namespace std;

typedef long long ll;

struct Tree {
    int key;
    int priority;
    int size;
    Tree *left, *right;
    Tree(int key) :
            key(key),
            priority(rand()),
            size(1),
            left(nullptr),
            right(nullptr) { }
};

void inorderBad(Tree *tree) {
    if (!tree) {
        return;
    }
    inorderBad(tree->left);
    cout << tree->key << " ";
    inorderBad(tree->right);
}

void inorder(Tree *tree) {
    inorderBad(tree);
    cout << "\n";
}

int getSize(Tree *tree) {
    if (!tree) {
        return 0;
    }
    return tree->size;
}

void recalc(Tree *tree) {
    if (!tree) {
        return;
    }
    tree->size = getSize(tree->left) + 1 + getSize(tree->right);
}

pair<Tree *, Tree *> split(Tree *tree, int key) {
    if (!tree) {
        return {nullptr, nullptr};
    }
    if (tree->key > key) {
        auto p = split(tree->left, key);
        tree->left = p.second;
        recalc(tree);
        return {p.first, tree};
    } else {
        auto p = split(tree->right, key);
        tree->right = p.first;
        recalc(tree);
        return {tree, p.second};
    }
};

Tree *merge(Tree *ta, Tree *tb) {
    if (!ta) {
        return tb;
    }
    if (!tb) {
        return ta;
    }
    if (ta->priority < tb->priority) {
        ta->right = merge(ta->right, tb);
        recalc(ta);
        return ta;
    } else {
        tb->left = merge(ta, tb->left);
        recalc(tb);
        return tb;
    }
}

Tree *insert(Tree *tree, int key) {
    Tree *ntree = new Tree(key);
    auto p = split(tree, key);
    return merge(merge(p.first, ntree), p.second);
}

Tree *erase(Tree *tree, int key) {
    auto p = split(tree, key);
    auto p1 = split(p.first, key - 1);
    return merge(p1.first, p.second);
}

Tree *numLess(Tree *tree, int key, int &res) {
    auto p = split(tree, key - 1);
    res = getSize(p.first);
    return merge(p.first, p.second);
}

int n, sqn;

int ind(int i) {
    return i / sqn;
}

vector<int> a;
vector<Tree *> blocks;

ll ans = 0;

void update(int l, int r) {
    if (l == r) {
        return;
    }
    int st = ind(l), fn = ind(r);
    ll numMoreL = 0, numLessR = 0;
    for (int i = st; i <= fn; ++i) {
        if (l < i * sqn && (i + 1) * sqn <= r) {
            int res = 0;
            blocks[i] = numLess(blocks[i], a[r], res);
            numLessR += res;
            blocks[i] = numLess(blocks[i], a[l], res);
            numMoreL += getSize(blocks[i]) - res;
            continue;
        }
        for (int j = max(l + 1, i * sqn); j < min(r, (i + 1) * sqn); ++j) {
            numLessR += a[j] < a[r];
            numMoreL += a[j] > a[l];
        }
    }
    ans += numLessR - (r - l - 1 - numLessR);
    ans += numMoreL - (r - l - 1 - numMoreL);
    ans += 1 - 2 * (a[l] > a[r]);
    blocks[st] = erase(blocks[st], a[l]);
    blocks[st] = insert(blocks[st], a[r]);
    blocks[fn] = erase(blocks[fn], a[r]);
    blocks[fn] = insert(blocks[fn], a[l]);
    swap(a[l], a[r]);
}

int main() {
    int q;
    cin >> n >> q;
    sqn = int(sqrt(n));
    a.resize((unsigned) n);
    for (int i = 0; i < n; ++i) {
        if (i % sqn == 0) {
            blocks.push_back(nullptr);
        }
        a[i] = i + 1;
        blocks.back() = insert(blocks.back(), i + 1);
    }
    for (int i = 0; i < q; ++i) {
        int l, r;
        cin >> l >> r;
        --l, --r;
        if (l > r) {
            swap(l, r);
        }
        update(l, r);
//        for (auto b : blocks) {
//            inorder(b);
//        }
        cout << ans << "\n";
    }
//    system("pause");
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>

using namespace std;

typedef long long ll;

struct Tree {
    int key;
    int priority;
    int size;
    Tree *left, *right;
    Tree(int key) :
            key(key),
            priority(rand()),
            size(1),
            left(nullptr),
            right(nullptr) { }
};

void inorderBad(Tree *tree) {
    if (!tree) {
        return;
    }
    inorderBad(tree->left);
    cout << tree->key << " ";
    inorderBad(tree->right);
}

void inorder(Tree *tree) {
    inorderBad(tree);
    cout << "\n";
}

inline int getSize(Tree *tree) {
    if (!tree) {
        return 0;
    }
    return tree->size;
}

inline void recalc(Tree *tree) {
    if (!tree) {
        return;
    }
    tree->size = getSize(tree->left) + 1 + getSize(tree->right);
}

pair<Tree *, Tree *> split(Tree *tree, int key) {
    if (!tree) {
        return {nullptr, nullptr};
    }
    if (tree->key > key) {
        auto p = split(tree->left, key);
        tree->left = p.second;
        recalc(tree);
        return {p.first, tree};
    } else {
        auto p = split(tree->right, key);
        tree->right = p.first;
        recalc(tree);
        return {tree, p.second};
    }
};

Tree *merge(Tree *ta, Tree *tb) {
    if (!ta) {
        return tb;
    }
    if (!tb) {
        return ta;
    }
    if (ta->priority < tb->priority) {
        ta->right = merge(ta->right, tb);
        recalc(ta);
        return ta;
    } else {
        tb->left = merge(ta, tb->left);
        recalc(tb);
        return tb;
    }
}

inline Tree *insert(Tree *tree, int key) {
    Tree *ntree = new Tree(key);
    auto p = split(tree, key);
    return merge(merge(p.first, ntree), p.second);
}

inline Tree *erase(Tree *tree, int key) {
    auto p = split(tree, key);
    auto p1 = split(p.first, key - 1);
    return merge(p1.first, p.second);
}

inline Tree *numLess(Tree *tree, int key, int &res) {
    auto p = split(tree, key - 1);
    res = getSize(p.first);
    return merge(p.first, p.second);
}

int n, sqn;

inline int ind(int i) {
    return i / sqn;
}

vector<int> a;
vector<Tree *> blocks;

ll ans = 0;

inline void update(int l, int r) {
    if (l == r) {
        return;
    }
    int st = ind(l), fn = ind(r);
    ll numMoreL = 0, numLessR = 0;
    for (int i = st; i <= fn; ++i) {
        if (l < i * sqn && (i + 1) * sqn <= r) {
            int res = 0;
            if (a[r] == n) {
                res = getSize(blocks[i]);
            } else if (a[r] == 1) {
                res = 0;
            } else {
                blocks[i] = numLess(blocks[i], a[r], res);
            }
            numLessR += res;
            if (a[l] == 1) {
                numMoreL = getSize(blocks[i]);
            } else if (a[l] == n) {
                numMoreL = 0;
            } else {
                blocks[i] = numLess(blocks[i], a[l], res);
            }
            numMoreL += getSize(blocks[i]) - res;
            continue;
        }
        for (int j = max(l + 1, i * sqn); j < min(r, (i + 1) * sqn); ++j) {
            numLessR += a[j] < a[r];
            numMoreL += a[j] > a[l];
        }
    }
    ans += numLessR - (r - l - 1 - numLessR);
    ans += numMoreL - (r - l - 1 - numMoreL);
    ans += 1 - 2 * (a[l] > a[r]);
    blocks[st] = erase(blocks[st], a[l]);
    blocks[fn] = erase(blocks[fn], a[r]);
    blocks[st] = insert(blocks[st], a[r]);
    blocks[fn] = insert(blocks[fn], a[l]);
    swap(a[l], a[r]);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    int q;
    cin >> n >> q;
    sqn = int(sqrt(n));
    a.resize((unsigned) n);
    for (int i = 0; i < n; ++i) {
        if (i % sqn == 0) {
            blocks.push_back(nullptr);
        }
        a[i] = i + 1;
        blocks.back() = insert(blocks.back(), i + 1);
    }
    for (int i = 0; i < q; ++i) {
        int l, r;
        cin >> l >> r;
        --l, --r;
        if (l > r) {
            swap(l, r);
        }
        update(l, r);
        cout << ans << "\n";
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>

using namespace std;

typedef long long ll;

struct Tree {
    int key;
    int priority;
    int size;
    Tree *left, *right;
    Tree(int key) :
            key(key),
            priority(rand()),
            size(1),
            left(nullptr),
            right(nullptr) { }
};

void inorderBad(Tree *tree) {
    if (!tree) {
        return;
    }
    inorderBad(tree->left);
    cout << tree->key << " ";
    inorderBad(tree->right);
}

void inorder(Tree *tree) {
    inorderBad(tree);
    cout << "\n";
}

int getSize(Tree *tree) {
    if (!tree) {
        return 0;
    }
    return tree->size;
}

void recalc(Tree *tree) {
    if (!tree) {
        return;
    }
    tree->size = getSize(tree->left) + 1 + getSize(tree->right);
}

pair<Tree *, Tree *> split(Tree *tree, int key) {
    if (!tree) {
        return {nullptr, nullptr};
    }
    if (tree->key > key) {
        auto p = split(tree->left, key);
        tree->left = p.second;
        recalc(tree);
        return {p.first, tree};
    } else {
        auto p = split(tree->right, key);
        tree->right = p.first;
        recalc(tree);
        return {tree, p.second};
    }
};

Tree *merge(Tree *ta, Tree *tb) {
    if (!ta) {
        return tb;
    }
    if (!tb) {
        return ta;
    }
    if (ta->priority < tb->priority) {
        ta->right = merge(ta->right, tb);
        recalc(ta);
        return ta;
    } else {
        tb->left = merge(ta, tb->left);
        recalc(tb);
        return tb;
    }
}

Tree *insert(Tree *tree, int key) {
    Tree *ntree = new Tree(key);
    auto p = split(tree, key);
    return merge(merge(p.first, ntree), p.second);
}

Tree *erase(Tree *tree, int key) {
    auto p = split(tree, key);
    auto p1 = split(p.first, key - 1);
    return merge(p1.first, p.second);
}

Tree *numLess(Tree *tree, int key, int &res) {
    auto p = split(tree, key - 1);
    res = getSize(p.first);
    return merge(p.first, p.second);
}

int n, sqn;

int ind(int i) {
    return i / sqn;
}

vector<int> a;
vector<Tree *> blocks;

ll ans = 0;

void update(int l, int r) {
    if (l == r) {
        return;
    }
    int st = ind(l), fn = ind(r);
    ll numMoreL = 0, numLessR = 0;
    for (int i = st; i <= fn; ++i) {
        if (l < i * sqn && (i + 1) * sqn <= r) {
            int res = 0;
            blocks[i] = numLess(blocks[i], a[r], res);
            numLessR += res;
            blocks[i] = numLess(blocks[i], a[l], res);
            numMoreL += getSize(blocks[i]) - res;
            continue;
        }
        for (int j = max(l + 1, i * sqn); j < min(r, (i + 1) * sqn); ++j) {
            numLessR += a[j] < a[r];
            numMoreL += a[j] > a[l];
        }
    }
    ans += numLessR - (r - l - 1 - numLessR);
    ans += numMoreL - (r - l - 1 - numMoreL);
    ans += 1 - 2 * (a[l] > a[r]);
    blocks[st] = erase(blocks[st], a[l]);
    blocks[fn] = erase(blocks[fn], a[r]);
    blocks[st] = insert(blocks[st], a[r]);
    blocks[fn] = insert(blocks[fn], a[l]);
    swap(a[l], a[r]);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    int q;
    cin >> n >> q;
    sqn = int(sqrt(n));
    a.resize((unsigned) n);
    for (int i = 0; i < n; ++i) {
        if (i % sqn == 0) {
            blocks.push_back(nullptr);
        }
        a[i] = i + 1;
        blocks.back() = insert(blocks.back(), i + 1);
    }
    for (int i = 0; i < q; ++i) {
        int l, r;
        cin >> l >> r;
        --l, --r;
        if (l > r) {
            swap(l, r);
        }
        update(l, r);
        cout << ans << "\n";
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
const int M = 2e5 + 10;
struct Block {
    int l , r , lazy;
}B[M];
int a[M] , b[M] , ans;
int belong[M] , n , unit , bcnt;
inline void reset(int x) {
    for(int i = B[x].l ; i <= B[x].r ; i++) b[i] = a[i];
    sort(b + B[x].l , b + B[x].r + 1);
}
void build() {
    unit = (int)sqrt(1.0 * n);
    bcnt = n / unit;
    if(n % unit) bcnt++;
    for(int i = 1 ; i <= bcnt ; i++) {
        B[i].l = (i - 1) * unit + 1;
        B[i].r =  i * unit;
        B[i].lazy = 0;
    }
    B[bcnt].r = n;
    for(int i = 1 ; i <= bcnt ; i++) reset(i);
    for(int i = 1 ; i <= n ; i++) belong[i] = (i - 1) / unit + 1;
}
int find_small(int l , int r , int num , int sta) {
    int mid = (l + r) >> 1;
    int ans = -1 , ed = r;
    while(l <= r) {
        mid = (l + r) >> 1;
        if(b[mid] <= num) {
            ans = mid;
            l = mid + 1;
        }
        else {
            r = mid - 1;
        }
    }
    if(ans == -1) return 0;
    if(ans == ed) ans++;
    return ans - sta;
}
int find_big(int l , int r , int num , int ed) {
    int mid = (l + r) >> 1;
    int ans = -1 , sta = l;
    while(l <= r) {
        mid = (l + r) >> 1;
        if(b[mid] >= num) {
            ans = mid;
            r = mid - 1;
        }
        else {
            l = mid + 1;
        }
    }
    if(ans == -1) return 0;
    if(ans == sta) ans--;
    return ed - ans;
}
void query(int l , int r) {
    int L = belong[l] , R = belong[r];
    //cout << "first: " << l << ' ' << r << ' '  << L << ' ' << R << endl;
    int sum = 0;
    if(L == R) {
        int x = a[l] , y = a[r];
        for(int i = l + 1 ; i <= r - 1 ; i++) {
            if(a[i] > x) sum++;
            if(a[i] < x) sum--;
            if(a[i] < y) sum++;
            if(a[i] > y) sum--;
        }
        a[l] = y , a[r] = x;
        if(x < y) sum++;
        else if(x > y) sum--;
        reset(L);
    }
    else {
        int x = a[l] , y = a[r];
        //cout << B[L + 1].l << ' ' << B[L + 1].r << endl;
        for(int i = l + 1 ; i <= B[L].r ; i++) {
            if(a[i] > x) sum++;
            if(a[i] < x) sum--;
            if(a[i] < y) sum++;
            if(a[i] > y) sum--;
        }
        for(int i = B[R].l ; i <= r - 1 ; i++) {
            if(a[i] > x) sum++;
            if(a[i] < x) sum--;
            if(a[i] < y) sum++;
            if(a[i] > y) sum--;
        }
        for(int i = L + 1 ; i < R ; i++) {
            //cout << sum << endl;
            sum += find_big(B[i].l , B[i].r , x , B[i].r);
            //cout << sum << endl;
            sum -= find_small(B[i].l , B[i].r , x , B[i].l);
            //cout << sum << endl;
            sum += find_small(B[i].l , B[i].r , y , B[i].l);
            //cout << sum << endl;
            sum -= find_big(B[i].l , B[i].r , y , B[i].r);
            //cout << sum << endl;
        }
        a[l] = y , a[r] = x;
        reset(L) , reset(R);
        if(x < y) sum++;
        else if(x > y) sum--;
    }
    ans += sum;
    //cout << ans << ' ' << sum << endl;
}
int main() {
    int q;
    ans = 0;
    scanf("%d%d" , &n , &q);
    for(int i = 1 ; i <= n ; i++) a[i] = i;
    build();
    while(q--) {
        int l , r;
        scanf("%d%d" , &l , &r);
        if(l > r) swap(l , r);
        query(l , r);
        printf("%d\n" , ans);
//        cout << "Case : ";
//        for(int i = 1 ; i <= n ; i++) cout << a[i] << ' ';
//        cout << endl;
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
const int M = 2e5 + 10;
struct Block {
    int l , r , lazy;
}B[M];
int a[M] , b[M] , ans;
int belong[M] , n , unit , bcnt;
inline void reset(int x) {
    for(int i = B[x].l ; i <= B[x].r ; i++) b[i] = a[i];
    sort(b + B[x].l , b + B[x].r + 1);
}
void build() {
    unit = (int)sqrt(1.0 * n);
    bcnt = n / unit;
    if(n % unit) bcnt++;
    for(int i = 1 ; i <= bcnt ; i++) {
        B[i].l = (i - 1) * unit + 1;
        B[i].r =  i * unit;
        B[i].lazy = 0;
    }
    B[bcnt].r = n;
    for(int i = 1 ; i <= bcnt ; i++) reset(i);
    for(int i = 1 ; i <= n ; i++) belong[i] = (i - 1) / unit + 1;
}
void push_down(int x) {
    if(B[x].lazy) {
        for(int i = B[x].l ; i <= B[x].r ; i++) a[i] += B[x].lazy;
        reset(x);
        B[x].lazy = 0;
    }
}
int find_small(int l , int r , int num , int sta) {
    int mid = (l + r) >> 1;
    int ans = -1 , ed = r;
    while(l <= r) {
        mid = (l + r) >> 1;
        if(b[mid] <= num) {
            ans = mid;
            l = mid + 1;
        }
        else {
            r = mid - 1;
        }
    }
    if(ans == -1) return 0;
    if(ans == ed) ans++;
    return ans - sta;
}
int find_big(int l , int r , int num , int ed) {
    int mid = (l + r) >> 1;
    int ans = -1 , sta = l;
    while(l <= r) {
        mid = (l + r) >> 1;
        if(b[mid] >= num) {
            ans = mid;
            r = mid - 1;
        }
        else {
            l = mid + 1;
        }
    }
    if(ans == -1) return 0;
    if(ans == sta) ans--;
    return ed - ans;
}
void query(int l , int r) {
    int L = belong[l] , R = belong[r];
    //cout << "first: " << l << ' ' << r << ' '  << L << ' ' << R << endl;
    int sum = 0;
    if(L == R) {
        push_down(L);
        int x = a[l] , y = a[r];
        for(int i = l + 1 ; i <= r - 1 ; i++) {
            if(a[i] > x) sum++;
            if(a[i] < x) sum--;
            if(a[i] < y) sum++;
            if(a[i] > y) sum--;
        }
        a[l] = y , a[r] = x;
        if(x < y) sum++;
        else if(x > y) sum--;
    }
    else {
        push_down(L) , push_down(R);
        int x = a[l] , y = a[r];
        //cout << B[L + 1].l << ' ' << B[L + 1].r << endl;
        for(int i = l + 1 ; i <= B[L].r ; i++) {
            if(a[i] > x) sum++;
            if(a[i] < x) sum--;
            if(a[i] < y) sum++;
            if(a[i] > y) sum--;
        }
        for(int i = B[R].l ; i <= r - 1 ; i++) {
            if(a[i] > x) sum++;
            if(a[i] < x) sum--;
            if(a[i] < y) sum++;
            if(a[i] > y) sum--;
        }
        for(int i = L + 1 ; i < R ; i++) {
            reset(i);
            //cout << sum << endl;
            sum += find_big(B[i].l , B[i].r , x , B[i].r);
            //cout << sum << endl;
            sum -= find_small(B[i].l , B[i].r , x , B[i].l);
            //cout << sum << endl;
            sum += find_small(B[i].l , B[i].r , y , B[i].l);
            //cout << sum << endl;
            sum -= find_big(B[i].l , B[i].r , y , B[i].r);
            //cout << sum << endl;
        }
        a[l] = y , a[r] = x;
        if(x < y) sum++;
        else if(x > y) sum--;
    }
    ans += sum;
    //cout << ans << ' ' << sum << endl;
}
int main() {
    int q;
    ans = 0;
    scanf("%d%d" , &n , &q);
    for(int i = 1 ; i <= n ; i++) a[i] = i;
    build();
    while(q--) {
        int l , r;
        scanf("%d%d" , &l , &r);
        if(l > r) swap(l , r);
        query(l , r);
        printf("%d\n" , ans);
//        cout << "Case : ";
//        for(int i = 1 ; i <= n ; i++) cout << a[i] << ' ';
//        cout << endl;
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int M = 2e5 + 10;
struct Block {
    int l , r , lazy;
}B[M];
int a[M] , b[M];
ll ans;
int belong[M] , n , unit , bcnt;
inline void reset(int x) {
    for(int i = B[x].l ; i <= B[x].r ; i++) b[i] = a[i];
    sort(b + B[x].l , b + B[x].r + 1);
}
void build() {
    unit = (int)sqrt(1.0 * n);
    bcnt = n / unit;
    if(n % unit) bcnt++;
    for(int i = 1 ; i <= bcnt ; i++) {
        B[i].l = (i - 1) * unit + 1;
        B[i].r =  i * unit;
        B[i].lazy = 0;
    }
    B[bcnt].r = n;
    for(int i = 1 ; i <= bcnt ; i++) reset(i);
    for(int i = 1 ; i <= n ; i++) belong[i] = (i - 1) / unit + 1;
}
int find_small(int l , int r , int num , int sta) {
    int mid = (l + r) >> 1;
    int ans = -1 , ed = r;
    while(l <= r) {
        mid = (l + r) >> 1;
        if(b[mid] <= num) {
            ans = mid;
            l = mid + 1;
        }
        else {
            r = mid - 1;
        }
    }
    if(ans == -1) return 0;
    if(ans == ed) ans++;
    return ans - sta;
}
int find_big(int l , int r , int num , int ed) {
    int mid = (l + r) >> 1;
    int ans = -1 , sta = l;
    while(l <= r) {
        mid = (l + r) >> 1;
        if(b[mid] >= num) {
            ans = mid;
            r = mid - 1;
        }
        else {
            l = mid + 1;
        }
    }
    if(ans == -1) return 0;
    if(ans == sta) ans--;
    return ed - ans;
}
void query(int l , int r) {
    int L = belong[l] , R = belong[r];
    //cout << "first: " << l << ' ' << r << ' '  << L << ' ' << R << endl;
    ll sum = 0;
    if(L == R) {
        int x = a[l] , y = a[r];
        for(int i = l + 1 ; i <= r - 1 ; i++) {
            if(a[i] > x) sum++;
            if(a[i] < x) sum--;
            if(a[i] < y) sum++;
            if(a[i] > y) sum--;
        }
        a[l] = y , a[r] = x;
        if(x < y) sum++;
        else if(x > y) sum--;
        reset(L);
    }
    else {
        int x = a[l] , y = a[r];
        //cout << B[L + 1].l << ' ' << B[L + 1].r << endl;
        for(int i = l + 1 ; i <= B[L].r ; i++) {
            if(a[i] > x) sum++;
            if(a[i] < x) sum--;
            if(a[i] < y) sum++;
            if(a[i] > y) sum--;
        }
        for(int i = B[R].l ; i <= r - 1 ; i++) {
            if(a[i] > x) sum++;
            if(a[i] < x) sum--;
            if(a[i] < y) sum++;
            if(a[i] > y) sum--;
        }
        for(int i = L + 1 ; i < R ; i++) {
            //cout << sum << endl;
            sum += (ll)find_big(B[i].l , B[i].r , x , B[i].r);
            //cout << sum << endl;
            sum -= (ll)find_small(B[i].l , B[i].r , x , B[i].l);
            //cout << sum << endl;
            sum += (ll)find_small(B[i].l , B[i].r , y , B[i].l);
            //cout << sum << endl;
            sum -= (ll)find_big(B[i].l , B[i].r , y , B[i].r);
            //cout << sum << endl;
        }
        a[l] = y , a[r] = x;
        reset(L) , reset(R);
        if(x < y) sum++;
        else if(x > y) sum--;
    }
    ans += sum;
    //cout << ans << ' ' << sum << endl;
}
int main() {
    int q;
    ans = 0;
    scanf("%d%d" , &n , &q);
    for(int i = 1 ; i <= n ; i++) a[i] = i;
    build();
    while(q--) {
        int l , r;
        scanf("%d%d" , &l , &r);
        if(l > r) swap(l , r);
        query(l , r);
        printf("%lld\n" , ans);
//        cout << "Case : ";
//        for(int i = 1 ; i <= n ; i++) cout << a[i] << ' ';
//        cout << endl;
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
const int M = 2e5 + 10;
struct Block {
    int l , r , lazy;
}B[M];
int a[M] , b[M] , ans;
int belong[M] , n , unit , bcnt;
inline void reset(int x) {
    for(int i = B[x].l ; i <= B[x].r ; i++) b[i] = a[i];
    sort(b + B[x].l , b + B[x].r + 1);
}
void build() {
    unit = (int)sqrt(1.0 * n);
    bcnt = n / unit;
    if(n % unit) bcnt++;
    for(int i = 1 ; i <= bcnt ; i++) {
        B[i].l = (i - 1) * unit + 1;
        B[i].r =  i * unit;
        B[i].lazy = 0;
    }
    B[bcnt].r = n;
    for(int i = 1 ; i <= bcnt ; i++) reset(i);
    for(int i = 1 ; i <= n ; i++) belong[i] = (i - 1) / unit + 1;
}
void push_down(int x) {
    if(B[x].lazy) {
        for(int i = B[x].l ; i <= B[x].r ; i++) a[i] += B[x].lazy;
        reset(x);
        B[x].lazy = 0;
    }
}
int find_small(int l , int r , int num , int sta) {
    int mid = (l + r) >> 1;
    int ans = -1 , len = (r - l + 1);
    while(l <= r) {
        mid = (l + r) >> 1;
        if(b[mid] <= num) {
            ans = mid;
            l = mid + 1;
        }
        else {
            r = mid - 1;
        }
    }
    if(ans == -1) return 0;
    return ans - sta;
}
int find_big(int l , int r , int num , int ed) {
    int mid = (l + r) >> 1;
    int ans = -1 , len = (r - l + 1);
    while(l <= r) {
        mid = (l + r) >> 1;
        if(b[mid] >= num) {
            ans = mid;
            r = mid - 1;
        }
        else {
            l = mid + 1;
        }
    }
    if(ans == -1) return 0;
    return ed - ans;
}
void query(int l , int r) {
    int L = belong[l] , R = belong[r];
    //cout << "first: " << l << ' ' << r << ' '  << L << ' ' << R << endl;
    int sum = 0;
    if(L == R) {
        push_down(L);
        int x = a[l] , y = a[r];
        for(int i = l + 1 ; i <= r - 1 ; i++) {
            if(a[i] > x) sum++;
            if(a[i] < x) sum--;
            if(a[i] < y) sum++;
            if(a[i] > y) sum--;
        }
        a[l] = y , a[r] = x;
        if(x < y) sum++;
        else if(x > y) sum--;
    }
    else {
        push_down(L) , push_down(R);
        int x = a[l] , y = a[r];
        //cout << B[L + 1].l << ' ' << B[L + 1].r << endl;
        for(int i = l + 1 ; i <= B[L].r ; i++) {
            if(a[i] > x) sum++;
            if(a[i] < x) sum--;
            if(a[i] < y) sum++;
            if(a[i] > y) sum--;
        }
        for(int i = B[R].l ; i <= r - 1 ; i++) {
            if(a[i] > x) sum++;
            if(a[i] < x) sum--;
            if(a[i] < y) sum++;
            if(a[i] > y) sum--;
        }
        for(int i = L + 1 ; i < R ; i++) {
            reset(i);
            //cout << sum << endl;
            sum += find_big(B[i].l , B[i].r , x , B[i].r);
            //cout << sum << endl;
            sum -= find_small(B[i].l , B[i].r , x , B[i].l);
            //cout << sum << endl;
            sum += find_small(B[i].l , B[i].r , y , B[i].l);
            //cout << sum << endl;
            sum -= find_big(B[i].l , B[i].r , y , B[i].r);
            //cout << sum << endl;
        }
        a[l] = y , a[r] = x;
        if(x < y) sum++;
        else if(x > y) sum--;
    }
    ans += sum;
    //cout << ans << ' ' << sum << endl;
}
int main() {
    int q;
    ans = 0;
    scanf("%d%d" , &n , &q);
    for(int i = 1 ; i <= n ; i++) a[i] = i;
    build();
    while(q--) {
        int l , r;
        scanf("%d%d" , &l , &r);
        if(l > r) swap(l , r);
        query(l , r);
        printf("%d\n" , ans);
//        cout << "Case : ";
//        for(int i = 1 ; i <= n ; i++) cout << a[i] << ' ';
//        cout << endl;
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
const int M = 2e5 + 10;
struct Block {
    int l , r , lazy;
}B[M];
int a[M] , b[M] , ans;
int belong[M] , n , unit , bcnt;
inline void reset(int x) {
    for(int i = B[x].l ; i <= B[x].r ; i++) b[i] = a[i];
    sort(b + B[x].l , b + B[x].r + 1);
}
void build() {
    unit = (int)sqrt(1.0 * n);
    bcnt = n / unit;
    if(n % unit) bcnt++;
    for(int i = 1 ; i <= bcnt ; i++) {
        B[i].l = (i - 1) * unit + 1;
        B[i].r =  i * unit;
        B[i].lazy = 0;
    }
    B[bcnt].r = n;
    for(int i = 1 ; i <= bcnt ; i++) reset(i);
    for(int i = 1 ; i <= n ; i++) belong[i] = (i - 1) / unit + 1;
}
void push_down(int x) {
    if(B[x].lazy) {
        for(int i = B[x].l ; i <= B[x].r ; i++) a[i] += B[x].lazy;
        reset(x);
        B[x].lazy = 0;
    }
}
int find_small(int l , int r , int num , int sta) {
    int mid = (l + r) >> 1;
    int ans = sta - 1;
    while(l <= r) {
        mid = (l + r) >> 1;
        if(b[mid] >= num) {
            ans = mid;
            r = mid - 1;
        }
        else {
            l = mid + 1;
        }
    }
    return ans - sta + 1;
}
int find_big(int l , int r , int num , int ed) {
    int mid = (l + r) >> 1;
    int ans = ed - 1;
    while(l <= r) {
        mid = (l + r) >> 1;
        if(b[mid] <= num) {
            ans = mid;
            l = mid + 1;
        }
        else {
            r = mid - 1;
        }
    }
    return ed - ans + 1;
}
void query(int l , int r) {
    int L = belong[l] , R = belong[r];
    //cout << "first: " << l << ' ' << r << ' '  << L << ' ' << R << endl;
    int sum = 0;
    if(L == R) {
        push_down(L);
        int x = a[l] , y = a[r];
        for(int i = l + 1 ; i <= r - 1 ; i++) {
            if(a[i] > x) sum++;
            if(a[i] < x) sum--;
            if(a[i] < y) sum++;
            if(a[i] > y) sum--;
        }
        a[l] = y , a[r] = x;
        if(x < y) sum++;
        else if(x > y) sum--;
    }
    else {
        push_down(L) , push_down(R);
        int x = a[l] , y = a[r];
//        if(l == 1 && r == 5) {
//            for(int i = l + 1 ; i <= r - 1 ; i++) cout << a[i] << ' ';
//            cout << endl;
//        }
        for(int i = l + 1 ; i <= B[L].r ; i++) {
            if(a[i] > x) sum++;
            if(a[i] < x) sum--;
            if(a[i] < y) sum++;
            if(a[i] > y) sum--;
        }
        //if(l == 1 && r == 5) cout << sum << endl;
        for(int i = B[R].l ; i <= r - 1 ; i++) {
            if(a[i] > x) sum++;
            if(a[i] < x) sum--;
            if(a[i] < y) sum++;
            if(a[i] > y) sum--;
        }
        //if(l == 1 && r == 5) cout << x << ' ' << y << endl;
        //if(l == 1 && r == 5) cout << B[L + 1].l << ' ' << B[L + 1].r << endl;
        for(int i = L + 1 ; i < R ; i++) {
            sum += find_big(B[i].l , B[i].r , x , B[i].r);
            //if(l == 1 && r == 5) cout << find_big(B[i].l , B[i].r , x , B[i].r) << endl;
            sum -= find_small(B[i].l , B[i].r , x , B[i].l);
            //if(l == 1 && r == 5) cout << find_small(B[i].l , B[i].r , x , B[i].l) << endl;
            sum += find_small(B[i].l , B[i].r , y , B[i].l);
            //if(l == 1 && r == 5) cout << find_small(B[i].l , B[i].r , y , B[i].l) << endl;
            sum -= find_big(B[i].l , B[i].r , y , B[i].r);
            //if(l == 1 && r == 5) cout << find_big(B[i].l , B[i].r , y , B[i].r) << endl;
        }
        a[l] = y , a[r] = x;
        if(x < y) sum++;
        else if(x > y) sum--;
    }
    ans += sum;
    //cout << ans << ' ' << sum << endl;
}
int main() {
    int q;
    ans = 0;
    scanf("%d%d" , &n , &q);
    for(int i = 1 ; i <= n ; i++) a[i] = i;
    build();
    while(q--) {
        int l , r;
        scanf("%d%d" , &l , &r);
        if(l > r) swap(l , r);
        query(l , r);
        printf("%d\n" , ans);
//        cout << "Case : ";
//        for(int i = 1 ; i <= n ; i++) cout << a[i] << ' ';
//        cout << endl;
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
const int M = 2e5 + 10;
struct Block {
    int l , r , lazy;
}B[M];
int a[M] , b[M] , ans;
int belong[M] , n , unit , bcnt;
inline void reset(int x) {
    for(int i = B[x].l ; i <= B[x].r ; i++) b[i] = a[i];
    sort(b + B[x].l , b + B[x].r + 1);
}
void build() {
    unit = (int)sqrt(1.0 * n);
    bcnt = n / unit;
    if(n % unit) bcnt++;
    for(int i = 1 ; i <= bcnt ; i++) {
        B[i].l = (i - 1) * unit + 1;
        B[i].r =  i * unit;
        B[i].lazy = 0;
    }
    B[bcnt].r = n;
    //for(int i = 1 ; i <= bcnt ; i++) reset(i);
    for(int i = 1 ; i <= n ; i++) belong[i] = (i - 1) / unit + 1;
}
int find_small(int l , int r , int num , int sta) {
    int mid = (l + r) >> 1;
    int ans = -1 , ed = r;
    while(l <= r) {
        mid = (l + r) >> 1;
        if(b[mid] <= num) {
            ans = mid;
            l = mid + 1;
        }
        else {
            r = mid - 1;
        }
    }
    if(ans == -1) return 0;
    if(ans == ed) ans++;
    return ans - sta;
}
int find_big(int l , int r , int num , int ed) {
    int mid = (l + r) >> 1;
    int ans = -1 , sta = l;
    while(l <= r) {
        mid = (l + r) >> 1;
        if(b[mid] >= num) {
            ans = mid;
            r = mid - 1;
        }
        else {
            l = mid + 1;
        }
    }
    if(ans == -1) return 0;
    if(ans == sta) ans--;
    return ed - ans;
}
void query(int l , int r) {
    int L = belong[l] , R = belong[r];
    //cout << "first: " << l << ' ' << r << ' '  << L << ' ' << R << endl;
    int sum = 0;
    if(L == R) {
        int x = a[l] , y = a[r];
        for(int i = l + 1 ; i <= r - 1 ; i++) {
            if(a[i] > x) sum++;
            if(a[i] < x) sum--;
            if(a[i] < y) sum++;
            if(a[i] > y) sum--;
        }
        a[l] = y , a[r] = x;
        if(x < y) sum++;
        else if(x > y) sum--;
        reset(L);
    }
    else {
        int x = a[l] , y = a[r];
        //cout << B[L + 1].l << ' ' << B[L + 1].r << endl;
        for(int i = l + 1 ; i <= B[L].r ; i++) {
            if(a[i] > x) sum++;
            if(a[i] < x) sum--;
            if(a[i] < y) sum++;
            if(a[i] > y) sum--;
        }
        for(int i = B[R].l ; i <= r - 1 ; i++) {
            if(a[i] > x) sum++;
            if(a[i] < x) sum--;
            if(a[i] < y) sum++;
            if(a[i] > y) sum--;
        }
        for(int i = L + 1 ; i < R ; i++) {
            //cout << sum << endl;
            sum += find_big(B[i].l , B[i].r , x , B[i].r);
            //cout << sum << endl;
            sum -= find_small(B[i].l , B[i].r , x , B[i].l);
            //cout << sum << endl;
            sum += find_small(B[i].l , B[i].r , y , B[i].l);
            //cout << sum << endl;
            sum -= find_big(B[i].l , B[i].r , y , B[i].r);
            //cout << sum << endl;
        }
        a[l] = y , a[r] = x;
        reset(L) , reset(R);
        if(x < y) sum++;
        else if(x > y) sum--;
    }
    ans += sum;
    //cout << ans << ' ' << sum << endl;
}
int main() {
    int q;
    ans = 0;
    scanf("%d%d" , &n , &q);
    for(int i = 1 ; i <= n ; i++) a[i] = i;
    build();
    while(q--) {
        int l , r;
        scanf("%d%d" , &l , &r);
        if(l > r) swap(l , r);
        query(l , r);
        printf("%d\n" , ans);
//        cout << "Case : ";
//        for(int i = 1 ; i <= n ; i++) cout << a[i] << ' ';
//        cout << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int MAX = 1 << 18;

struct seg_node1 {
	int val;
	seg_node1 *lch, *rch;
	seg_node1(int v = 0, seg_node1* l = nullptr, seg_node1* r = nullptr)
		: val(v), lch(l), rch(r) {}
	~seg_node1() {
		if (lch) delete lch;
		if (rch) delete rch;
	}
};

seg_node1* add(seg_node1 *t, int p, int val, int lb = 0, int ub = MAX) {
	if (p < lb || ub <= p) return t;
	if (!t) t = new seg_node1;
	t->val += val;
	if (t->val == 0) {
		delete t;
		return nullptr;
	}
	if (ub - lb == 1) {
		return t;
	}
	int m = (lb + ub) >> 1;
	t->lch = add(t->lch, p, val, lb, m);
	t->rch = add(t->rch, p, val, m, ub);
	return t;
}

int find(seg_node1 *t, int l, int r, int lb = 0, int ub = MAX) {
	if (!t || r <= lb || ub <= l) return 0;
	if (l <= lb && ub <= r) return t->val;
	int m = (lb + ub) >> 1;
	return find(t->lch, l, r, lb, m) + find(t->rch, l, r, m, ub);
}

seg_node1 *segs[MAX << 1];

void add(int x, int y, int val, int t = 1, int lb = 0, int ub = MAX) {
	if (x < lb || ub <= x) return;
	segs[t] = add(segs[t], y, val);
	if (ub - lb == 1) return;
	int m = (lb + ub) >> 1;
	add(x, y, val, t << 1, lb, m);
	add(x, y, val, (t << 1) | 1, m, ub);
}

int find(int li, int lj, int ri, int rj, int t = 1, int lb = 0, int ub = MAX) {
	if (ri <= lb || ub <= li) return 0;
	if (li <= lb && ub <= ri) return find(segs[t], lj, rj);
	int m = (lb + ub) >> 1;
	return find(li, lj, ri, rj, t << 1, lb, m) + find(li, lj, ri, rj, (t << 1) | 1, m, ub);
}

int main()
{
	ios::sync_with_stdio(false), cin.tie(0);
	int n, q;
	cin >> n >> q;
	vector<int> val(n);
	for (int i = 0; i < n; i++) {
		val[i] = i;
		add(i, i, 1);
	}
	ll res = 0;
	while (q--) {
		int l, r;
		cin >> l >> r; l--, r--;
		if (l == r) {
			printf("%lld\n", res);
			continue;
		}
		if (l > r) swap(l, r);
		int lv = val[l], rv = val[r];

		res += lv < rv ? 1 : -1;

		int cnt1 = find(l + 1, 0, r, lv);
		res -= cnt1;
		res += (r - l - 1) - cnt1;

		int cnt2 = find(l + 1, 0, r, rv);
		res += cnt2;
		res -= (r - l - 1) - cnt2;

		val[l] = rv, val[r] = lv;

		add(l, lv, -1);
		add(l, val[l], 1);
		add(r, rv, -1);
		add(r, val[r], 1);

		printf("%lld\n", res);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int MAX = 1 << 18;

struct seg_node1 {
	int val;
	seg_node1 *lch, *rch;
	seg_node1(int v = 0, seg_node1* l = nullptr, seg_node1* r = nullptr)
		: val(v), lch(l), rch(r) {}
	~seg_node1() {
		if (lch) delete lch;
		if (rch) delete rch;
	}
};

seg_node1* add(seg_node1 *t, int p, int val, int lb = 0, int ub = MAX) {
	if (p < lb || ub <= p) return t;
	if (!t) t = new seg_node1;
	if (ub - lb == 1) {
		t->val += val;
		if (t->val == 0) {
			delete t;
			return nullptr;
		}
		return t;
	}
	int m = (lb + ub) >> 1;
	t->lch = add(t->lch, p, val, lb, m);
	t->rch = add(t->rch, p, val, m, ub);
	t->val += val;
	if (!t->lch && !t->rch && t->val == 0) {
		delete t;
		return nullptr;
	}
	return t;
}

int find(seg_node1 *t, int l, int r, int lb = 0, int ub = MAX) {
	if (!t || r <= lb || ub <= l) return 0;
	if (l <= lb && ub <= r) return t->val;
	int m = (lb + ub) >> 1;
	return find(t->lch, l, r, lb, m) + find(t->rch, l, r, m, ub);
}

struct seg_node2 {
	seg_node1 *tree;
	seg_node2 *lch, *rch;
	seg_node2(seg_node1 *t = nullptr, seg_node2* l = nullptr, seg_node2* r = nullptr)
		: tree(t), lch(l), rch(r) {}
};

seg_node2* add(seg_node2 *t, int x, int y, int val, int lb = 0, int ub = MAX) {
	if (x < lb || ub <= x) return t;
	if (!t) t = new seg_node2;
	t->tree = add(t->tree, y, val);
	if (ub - lb == 1) return t;
	int m = (lb + ub) >> 1;
	t->lch = add(t->lch, x, y, val, lb, m);
	t->rch = add(t->rch, x, y, val, m, ub);
	return t;
}

int find(seg_node2 *t, int li, int lj, int ri, int rj, int lb = 0, int ub = MAX) {
	if (!t || ri <= lb || ub <= li) return 0;
	if (li <= lb && ub <= ri) return find(t->tree, lj, rj);
	int m = (lb + ub) >> 1;
	return find(t->lch, li, lj, ri, rj, lb, m) + find(t->rch, li, lj, ri, rj, m, ub);
}

int main()
{
	ios::sync_with_stdio(false), cin.tie(0);
	int n, q;
	cin >> n >> q;
	vector<int> val(n);
	seg_node2 *tree = nullptr;
	for (int i = 0; i < n; i++) {
		val[i] = i;
		tree = add(tree, i, i, 1);
	}
	ll res = 0;
	while (q--) {
		int l, r;
		cin >> l >> r; l--, r--;
		if (l == r) {
			printf("%lld\n", res);
			continue;
		}
		if (l > r) swap(l, r);
		int lv = val[l], rv = val[r];

		res += lv < rv ? 1 : -1;

		int cnt1 = find(tree, l + 1, 0, r, lv);
		res -= cnt1;
		res += (r - l - 1) - cnt1;

		int cnt2 = find(tree, l + 1, 0, r, rv);
		res += cnt2;
		res -= (r - l - 1) - cnt2;

		val[l] = rv, val[r] = lv;

		tree = add(tree, l, lv, -1);
		tree = add(tree, l, val[l], 1);
		tree = add(tree, r, rv, -1);
		tree = add(tree, r, val[r], 1);

		printf("%lld\n", res);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int PMAX = 4e6;

const int MAX = 2e5;

class fid {
	struct node {
		bool val;
		int all;
		node *ch[2];
		int dep, size;
		node() : node(false) {}
		node(bool v, node* l = nullptr, node* r = nullptr)
			: val(v), all(v), ch{ l, r }, dep(1), size(1) {}
		void *operator new(size_t) {
			static int p = 0;
			static node pool[PMAX];
			return pool + p++;
		}
	};
	int depth(node *t) { return t ? t->dep : 0; }
	int count(node *t) { return t ? t->size : 0; }
	int que(node *t) { return t ? t->all : 0; }
	node *update(node *t) {
		t->dep = max(depth(t->ch[0]), depth(t->ch[1])) + 1;
		t->size = count(t->ch[0]) + 1 + count(t->ch[1]);
		t->all = que(t->ch[0]) + t->val + que(t->ch[1]);
		return t;
	}
	node *rotate(node *t, int b) {
		node *s = t->ch[1 - b];
		t->ch[1 - b] = s->ch[b];
		s->ch[b] = t;
		update(t);
		return update(s);
	}
	node *fix(node *t) {
		if (!t) return t;
		if (depth(t->ch[0]) - depth(t->ch[1]) == 2) {
			if (depth(t->ch[0]->ch[1]) > depth(t->ch[0]->ch[0])) {
				t->ch[0] = rotate(t->ch[0], 0);
			}
			t = rotate(t, 1);
		}
		else if (depth(t->ch[0]) - depth(t->ch[1]) == -2) {
			if (depth(t->ch[1]->ch[0]) > depth(t->ch[1]->ch[1])) {
				t->ch[1] = rotate(t->ch[1], 1);
			}
			t = rotate(t, 0);
		}
		return t;
	}
	node *insert(node *t, int k, bool v) {
		if (!t) return new node(v);
		int c = count(t->ch[0]);
		if (k <= c)
			t->ch[0] = insert(t->ch[0], k, v);
		else
			t->ch[1] = insert(t->ch[1], k - (c + 1), v);
		return fix(update(t));
	}
	node *erase(node *t) {
		if (!t) return t;
		if (!t->ch[0] && !t->ch[1]) return nullptr;
		if (!t->ch[0] || !t->ch[1]) return t->ch[t->ch[0] == nullptr];
		return fix(update(new node(find(t->ch[1], 0)->val, t->ch[0], erase(t->ch[1], 0))));
	}
	node *erase(node *t, int k) {
		if (!t) return t;
		int c = count(t->ch[0]);
		if (k < c) {
			t->ch[0] = erase(t->ch[0], k);
			update(t);
		}
		else if (k > c) {
			t->ch[1] = erase(t->ch[1], k - (c + 1));
			update(t);
		}
		else {
			t = erase(t);
		}
		return fix(t);
	}
	node *find(node *t, int k) {
		if (!t) return t;
		int c = count(t->ch[0]);
		return k < c ? find(t->ch[0], k) : k == c ? t : find(t->ch[1], k - (c + 1));
	}
	int find(node *t, int l, int r) {
		if (!t || r <= 0 || t->size <= l) return 0;
		if (l <= 0 && t->size <= r) return t->all;
		int c = count(t->ch[0]);
		return find(t->ch[0], l, r) + (l <= c && c < r ? t->val : 0) + find(t->ch[1], l - (c + 1), r - (c + 1));
	}
	node *root;
public:

	fid() : root(nullptr) {}
	int size() {
		return count(root);
	}
	void insert(int k, bool b) {
		root = insert(root, k, b);
	}
	void erase(int k) {
		root = erase(root, k);
	}
	bool find(int k) {
		return find(root, k)->val;
	}
	int rank(int k, bool b) {
		return b ? find(root, 0, k) : k - find(root, 0, k);
	}
	int rank(int l, int r, bool b) {
		return b ? find(root, l, r) : r - l - find(root, l, r);
	}
	int select(int k, bool b) {
		int res = 0;
		node *x = root;
		while (true) {
			assert(x);
			int c = b ? que(x->ch[0]) : count(x->ch[0]) - que(x->ch[0]);
			if (k == c && x->val == b) return res + count(x->ch[0]);
			if (k < c) {
				x = x->ch[0];
			}
			else {
				k -= c + (x->val == b);
				res += count(x->ch[0]) + 1;
				x = x->ch[1];
			}
		}
	}
	int select(int l, int k, bool b) {
		return select(k + rank(l, b), b);
	}
};

template <typename T>
class avl_tree {
	struct node {
		T val;
		node *ch[2];
		int dep, size;
		node() : node(T()) {}
		node(T v, node* l = nullptr, node* r = nullptr)
			: val(v), ch{ l, r }, dep(1), size(1) {}
		void *operator new(size_t) {
			static int p = 0;
			static node pool[MAX];
			return pool + p++;
		}
	};
	int depth(node *t) { return t == nullptr ? 0 : t->dep; }
	int count(node *t) { return t == nullptr ? 0 : t->size; }
	node *update(node *t) {
		t->dep = max(depth(t->ch[0]), depth(t->ch[1])) + 1;
		t->size = count(t->ch[0]) + count(t->ch[1]) + 1;
		return t;
	}
	node *rotate(node *t, int b) {
		node *s = t->ch[1 - b];
		t->ch[1 - b] = s->ch[b];
		s->ch[b] = t;
		update(t);
		return update(s);
	}
	node *fix(node *t) {
		if (!t) return t;
		if (depth(t->ch[0]) - depth(t->ch[1]) == 2) {
			if (depth(t->ch[0]->ch[1]) > depth(t->ch[0]->ch[0])) {
				t->ch[0] = rotate(t->ch[0], 0);
			}
			t = rotate(t, 1);
		}
		else if (depth(t->ch[0]) - depth(t->ch[1]) == -2) {
			if (depth(t->ch[1]->ch[0]) > depth(t->ch[1]->ch[1])) {
				t->ch[1] = rotate(t->ch[1], 1);
			}
			t = rotate(t, 0);
		}
		return t;
	}
	node *insert(node *t, int k, T v) {
		if (!t) return new node(v);
		int c = count(t->ch[0]);
		if (k <= c)
			t->ch[0] = insert(t->ch[0], k, v);
		else
			t->ch[1] = insert(t->ch[1], k - (c + 1), v);
		return fix(update(t));
	}
	node *erase(node *t) {
		if (!t) return t;
		if (!t->ch[0] && !t->ch[1]) return nullptr;
		if (!t->ch[0] || !t->ch[1]) return t->ch[t->ch[0] == nullptr];
		return fix(update(new node(find(t->ch[1], 0)->val, t->ch[0], erase(t->ch[1], 0))));
	}
	node *erase(node *t, int k) {
		if (!t) return t;
		int c = count(t->ch[0]);
		if (k < c) {
			t->ch[0] = erase(t->ch[0], k);
			update(t);
		}
		else if (k > c) {
			t->ch[1] = erase(t->ch[1], k - (c + 1));
			update(t);
		}
		else {
			t = erase(t);
		}
		return fix(t);
	}
	node *find(node *t, int k) {
		if (!t) return t;
		int c = count(t->ch[0]);
		return k < c ? find(t->ch[0], k) : k == c ? t : find(t->ch[1], k - (c + 1));
	}
	node *root;
public:
	avl_tree() : root(nullptr) {}
	int size() {
		return count(root);
	}
	void insert(int k, T val) {
		root = insert(root, k, val);
	}
	void erase(int k) {
		root = erase(root, k);
	}
	T find(int k) {
		return find(root, k)->val;
	}
};

template <typename T>
class dynamic_wavelet_matrix {
	T h;
	avl_tree<T> all;
	vector<fid> data;
	vector<int> mid;
public:
	dynamic_wavelet_matrix(T h_ = 30) : h(h_), data(h), mid(h) {}
	void insert(int k, T val) {
		all.insert(k, val);
		for (T b = 0; b < h; b++) {
			bool d = (val >> (h - 1 - b)) & 1;
			data[b].insert(k, d);
			if (d) {
				k = data[b].rank(k, d) + mid[b];
			}
			else {
				k = data[b].rank(k, d);
				mid[b]++;
			}
		}
	}
	void erase(int k) {
		T val = all.find(k);
		all.erase(k);
		for (T b = 0; b < h; b++) {
			bool d = (val >> (h - 1 - b)) & 1;
			data[b].erase(k);
			if (d) {
				k = data[b].rank(k, d) + mid[b];
			}
			else {
				k = data[b].rank(k, d);
				mid[b]--;
			}
		}
	}
	T get(int k) {
		return all.find(k);
	}
	int rank(int p, T val) {
		return rank(0, p, val);
	}
	int rank(int l, int r, T val) {
		if (val >> h) return 0;
		for (T b = 0; b < h; b++) {
			if (val & ((T)1 << (h - 1 - b))) {
				l = data[b].rank(l, true) + mid[b];
				r = data[b].rank(r, true) + mid[b];
			}
			else {
				l = data[b].rank(l, false);
				r = data[b].rank(r, false);
			}
		}
		return r - l;
	}
	int rank_less_than(int l, int r, T ub) {
		if (ub >> h) return r - l;
		int res = 0;
		for (T b = 0; b < h; b++) {
			bool d = (ub >> (h - 1 - b)) & 1;
			int lcnt = data[b].rank(l, d);
			int rcnt = data[b].rank(r, d);
			if (d) res += (r - l) - (rcnt - lcnt);
			l = lcnt;
			r = rcnt;
			if (d) {
				l += mid[b];
				r += mid[b];
			}
		}
		return res;
	}
	int rank_range(int l, int r, T lb, T ub) {
		return rank_less_than(l, r, ub) - rank_less_than(l, r, lb);
	}
	int select(int x, T val) {
		static int left[h];
		int l = 0, r = data[0].size();
		for (T b = 0; b < h; b++) {
			left[b] = l;
			if (val & ((T)1 << (h - 1 - b))) {
				l = data[b].rank(l, true) + mid[b];
				r = data[b].rank(r, true) + mid[b];
			}
			else {
				l = data[b].rank(l, false);
				r = data[b].rank(r, false);
			}
		}
		for (int b = h - 1; b >= 0; b--) {
			x = data[b].select(left[b], x, (bool)((val >> (h - 1 - b)) & 1)) - left[b];
		}
		return x;
	}
	int select(int l, int r, int x, T val) {
		return select(x + rank(l, val), val);
	}
	T kth_element(int l, int r, int k) {
		T res = 0;
		for (T b = 0; b < h; b++) {
			int cnt = data[b].rank(l, r, false);
			res <<= 1;
			if (k >= cnt) {
				l = data[b].rank(l, true) + mid[b];
				r = data[b].rank(r, true) + mid[b];
				k -= cnt;
				res |= 1;
			}
			else {
				l = data[b].rank(l, false);
				r = data[b].rank(r, false);
			}
		}
		return res;
	}
};

int main()
{
	ios::sync_with_stdio(false), cin.tie(0);
	int n, q;
	cin >> n >> q;
	dynamic_wavelet_matrix<int> dwm(18);
	for (int i = 0; i < n; i++) {
		dwm.insert(i, i + 1);
	}
	ll res = 0;
	while (q--) {
		int l, r;
		cin >> l >> r; l--, r--;
		if (l == r) {
			printf("%lld\n", res);
			continue;
		}
		if (l > r) swap(l, r);
		int lv = dwm.get(l), rv = dwm.get(r);
		int lcnt = dwm.rank_less_than(l + 1, r, lv), rcnt = dwm.rank_less_than(l + 1, r, rv);
		res += rcnt * 2 - lcnt * 2 + (lv < rv ? 1 : -1);
		printf("%lld\n", res);
		dwm.erase(l);
		dwm.insert(l, rv);
		dwm.erase(r);
		dwm.insert(r, lv);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int MAX = 1 << 18;

struct seg_node {
	int pos, val, all;
	seg_node *lch, *rch;
};

const int pmax = 1e7;
int it = 0;
seg_node pool[pmax];

seg_node* new_node(int p, int v) {
	pool[it] = (seg_node) { p, v, v, nullptr, nullptr };
	return pool + it++;
}

seg_node* update(seg_node *t) {
	t->all = (t->lch ? t->lch->all : 0) + t->val + (t->rch ? t->rch->all : 0);
	return t;
}

seg_node* add(seg_node *t, int p, int val, int lb = 0, int ub = MAX) {
	if (!t) return new_node(p, val);
	if (t->pos == p) {
		t->val += val;
		return update(t);
	}
	int m = (lb + ub) >> 1;
	if (p < m) {
		if (t->pos < p) {
			swap(p, t->pos);
			swap(val, t->val);
		}
		t->lch = add(t->lch, p, val, lb, m);
	}
	else {
		if (t->pos > p) {
			swap(p, t->pos);
			swap(val, t->val);
		}
		t->rch = add(t->rch, p, val, m, ub);
	}
	return update(t);
}

int find(seg_node *t, int l, int r, int lb = 0, int ub = MAX) {
	if (!t || r <= lb || ub <= l) return 0;
	if (l <= lb && ub <= r) return t->all;
	int m = (lb + ub) >> 1;
	int val = l <= t->pos && t->pos < r ? t->val : 0;
	return find(t->lch, l, r, lb, m) + find(t->rch, l, r, m, ub) + val;
}

seg_node* segs[MAX << 1];

void add(int x, int y, int val, int t = 1, int lb = 0, int ub = MAX) {
	if (x < lb || ub <= x) return;
	segs[t] = add(segs[t], y, val);
	if (ub - lb == 1) return;
	int m = (lb + ub) >> 1;
	add(x, y, val, t << 1, lb, m);
	add(x, y, val, (t << 1) | 1, m, ub);
}

int find(int li, int lj, int ri, int rj, int t = 1, int lb = 0, int ub = MAX) {
	if (ri <= lb || ub <= li) return 0;
	if (li <= lb && ub <= ri) return find(segs[t], lj, rj);
	int m = (lb + ub) >> 1;
	return find(li, lj, ri, rj, t << 1, lb, m) + find(li, lj, ri, rj, (t << 1) | 1, m, ub);
}

int main()
{
	ios::sync_with_stdio(false), cin.tie(0);
	int n, q;
	cin >> n >> q;
	vector<int> val(n);
	for (int i = 0; i < n; i++) {
		val[i] = i;
		add(i, i, 1);
	}
	ll res = 0;
	while (q--) {
		int l, r;
		cin >> l >> r; l--, r--;
		if (l == r) {
			printf("%lld\n", res);
			continue;
		}
		if (l > r) swap(l, r);
		int lv = val[l], rv = val[r];

		res += lv < rv ? 1 : -1;

		int cnt1 = find(l + 1, 0, r, lv);
		res -= cnt1;
		res += (r - l - 1) - cnt1;

		int cnt2 = find(l + 1, 0, r, rv);
		res += cnt2;
		res -= (r - l - 1) - cnt2;

		val[l] = rv, val[r] = lv;

		add(l, lv, -1);
		add(l, val[l], 1);
		add(r, rv, -1);
		add(r, val[r], 1);

		printf("%lld\n", res);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int MAX = 1 << 18;

struct seg_node1 {
	int pos, val, all;
	seg_node1 *lch, *rch;
	seg_node1(int p, int v = 0)
		: pos(p), val(v), all(v), lch(nullptr), rch(nullptr) {}
};

seg_node1* update(seg_node1 *t) {
	t->all = (t->lch ? t->lch->all : 0) + t->val + (t->rch ? t->rch->all : 0);
	return t;
}

seg_node1* add(seg_node1 *t, int p, int val, int lb = 0, int ub = MAX) {
	if (!t) return new seg_node1(p, val);
	if (t->pos == p) {
		t->val += val;
		return update(t);
	}
	int m = (lb + ub) >> 1;
	if (p < m) {
		if (t->pos < p) {
			swap(p, t->pos);
			swap(val, t->val);
		}
		t->lch = add(t->lch, p, val, lb, m);
	}
	else {
		if (t->pos > p) {
			swap(p, t->pos);
			swap(val, t->val);
		}
		t->rch = add(t->rch, p, val, m, ub);
	}
	return update(t);
}

int find(seg_node1 *t, int l, int r, int lb = 0, int ub = MAX) {
	if (!t || r <= lb || ub <= l) return 0;
	if (l <= lb && ub <= r) return t->all;
	int m = (lb + ub) >> 1;
	int val = l <= t->pos && t->pos < r ? t->val : 0;
	return find(t->lch, l, r, lb, m) + find(t->rch, l, r, m, ub) + val;
}

seg_node1* segs[MAX << 1];

void add(int x, int y, int val, int t = 1, int lb = 0, int ub = MAX) {
	if (x < lb || ub <= x) return;
	segs[t] = add(segs[t], y, val);
	if (ub - lb == 1) return;
	int m = (lb + ub) >> 1;
	add(x, y, val, t << 1, lb, m);
	add(x, y, val, (t << 1) | 1, m, ub);
}

int find(int li, int lj, int ri, int rj, int t = 1, int lb = 0, int ub = MAX) {
	if (ri <= lb || ub <= li) return 0;
	if (li <= lb && ub <= ri) return find(segs[t], lj, rj);
	int m = (lb + ub) >> 1;
	return find(li, lj, ri, rj, t << 1, lb, m) + find(li, lj, ri, rj, (t << 1) | 1, m, ub);
}

int main()
{
	ios::sync_with_stdio(false), cin.tie(0);
	int n, q;
	cin >> n >> q;
	vector<int> val(n);
	for (int i = 0; i < n; i++) {
		val[i] = i;
		add(i, i, 1);
	}
	ll res = 0;
	while (q--) {
		int l, r;
		cin >> l >> r; l--, r--;
		if (l == r) {
			printf("%lld\n", res);
			continue;
		}
		if (l > r) swap(l, r);
		int lv = val[l], rv = val[r];

		res += lv < rv ? 1 : -1;

		int cnt1 = find(l + 1, 0, r, lv);
		res -= cnt1;
		res += (r - l - 1) - cnt1;

		int cnt2 = find(l + 1, 0, r, rv);
		res += cnt2;
		res -= (r - l - 1) - cnt2;

		val[l] = rv, val[r] = lv;

		add(l, lv, -1);
		add(l, val[l], 1);
		add(r, rv, -1);
		add(r, val[r], 1);

		printf("%lld\n", res);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;

using COL = bool;
const COL RED = false, BLACK = true;

class FID {

	struct node {
		COL color;
		int size;
		bool val;
		int all;
		node *p, *ch[2];
		node() : color(BLACK), size(0), val(0), all(0), p(nullptr), ch{ nullptr, nullptr } {}
		node(COL c, bool v, node *par, node *l, node *r) : color(c), size(1), val(v), all(v), p(par), ch{ l, r } {}
	} *NIL, *root;

	node* new_node(bool val) const {
		return new node(RED, val, NIL, NIL, NIL);
	}
	void update(node *x) {
		x->size = x->ch[0]->size + 1 + x->ch[1]->size;
		x->all = x->ch[0]->all + x->val + x->ch[1]->all;
	}
	void update_up(node *x) {
		while (x != NIL) update(x), x = x->p;
	}
	void rotate(node *x, int b) {
		node *y = x->ch[1 - b];
		x->ch[1 - b] = y->ch[b];
		if (y->ch[b] != NIL) {
			y->ch[b]->p = x;
		}
		y->p = x->p;
		if (x->p == NIL) {
			root = y;
		}
		else {
			x->p->ch[x != x->p->ch[0]] = y;
		}
		y->ch[b] = x;
		x->p = y;
		update(x);
		update(y);
	}
	void insert_fix(node *x) {
		while (x->p->color == RED) {
			int b = (x->p != x->p->p->ch[0]);
			node *y = x->p->p->ch[1 - b];
			if (y->color == RED) {
				x->p->color = BLACK;
				y->color = BLACK;
				x->p->p->color = RED;
				x = x->p->p;
				continue;
			}
			if (x == x->p->ch[1 - b]) {
				x = x->p;
				rotate(x, b);
			}
			x->p->color = BLACK;
			x->p->p->color = RED;
			rotate(x->p->p, 1 - b);
		}
		root->color = BLACK;
	}
	void transplant(node *u, node *v) {
		if (u->p == NIL) {
			root = v;
		}
		else {
			u->p->ch[u != u->p->ch[0]] = v;
		}
		v->p = u->p;
	}
	void erase_fix(node *x) {
		while (x != root && x->color == BLACK) {
			int b = (x != x->p->ch[0]);
			node *w = x->p->ch[1 - b];
			if (w->color == RED) {
				w->color = BLACK;
				x->p->color = RED;
				rotate(x->p, b);
				w = x->p->ch[1 - b];
			}
			if (w->ch[b]->color == BLACK && w->ch[1 - b]->color == BLACK) {
				w->color = RED;
				x = x->p;
				continue;
			}
			if (w->ch[1 - b]->color == BLACK) {
				w->ch[b]->color = BLACK;
				w->color = RED;
				rotate(w, 1 - b);
				w = x->p->ch[1 - b];
			}
			w->color = x->p->color;
			x->p->color = BLACK;
			w->ch[1 - b]->color = BLACK;
			rotate(x->p, b);
			x = root;
		}
		x->color = BLACK;
	}
	node* find_first(node *x) const {
		while (x->ch[0] != NIL) x = x->ch[0];
		return x;
	}
	node* find(node *t, int k) const {
		if (k < 0 || t->size <= k) return NIL;
		node *x = t;
		while (x->ch[0]->size != k) {
			if (k < x->ch[0]->size) {
				x = x->ch[0];
			}
			else {
				k -= x->ch[0]->size + 1;
				x = x->ch[1];
			}
		}
		return x;
	}
	int find(node *t, int l, int r) const {
		if (t == NIL || r <= 0 || t->size <= l) return 0;
		if (l <= 0 && t->size <= r) return t->all;
		int c = t->ch[0]->size;
		return find(t->ch[0], l, r) + (l <= c && c < r ? t->val : 0) + find(t->ch[1], l - (c + 1), r - (c + 1));
	}

public:

	FID() : NIL(new node()), root(NIL) {}
	int size() const {
		return root->size;
	}
	void insert(int k, bool b) {
		node *y = NIL, *v = new_node(b);
		if (root == NIL) {
			root = v;
		}
		else if (k == 0) {
			y = find_first(root);
			y->ch[0] = v;
		}
		else {
			y = find(root, k - 1);
			if (y->ch[1] == NIL) {
				y->ch[1] = v;
			}
			else {
				y = find_first(y->ch[1]);
				y->ch[0] = v;
			}
		}
		v->p = y;
		update_up(y);
		insert_fix(v);
	}
	void erase(int k) {
		node *x = find(root, k);
		node *y = x, *z;
		COL c = y->color;
		if (x->ch[0] == NIL) {
			z = x->ch[1];
			transplant(x, x->ch[1]);
		}
		else if (x->ch[1] == NIL) {
			z = x->ch[0];
			transplant(x, x->ch[0]);
		}
		else {
			y = find_first(x->ch[1]);
			c = y->color;
			z = y->ch[1];
			if (y->p == x) {
				z->p = y;
			}
			else {
				transplant(y, y->ch[1]);
				y->ch[1] = x->ch[1];
				y->ch[1]->p = y;
			}
			transplant(x, y);
			y->ch[0] = x->ch[0];
			y->ch[0]->p = y;
			y->color = x->color;
			update(y);
		}
		update_up(z->p);
		if (c == BLACK) erase_fix(z);
	}
	bool find(int k) const {
		return find(root, k)->val;
	}
	int rank(int k, bool b) const {
		return b ? find(root, 0, k) : k - find(root, 0, k);
	}
	int rank(int l, int r, bool b) const {
		return b ? find(root, l, r) : r - l - find(root, l, r);
	}
	int select(int k, bool b) const {
		int res = 0;
		node *x = root;
		while (true) {
			assert(x != NIL);
			int c = b ? x->ch[0]->all : x->ch[0]->size - x->ch[0]->all;
			if (k == c && x->val == b) return res + x->ch[0]->size;
			if (k < c) {
				x = x->ch[0];
			}
			else {
				k -= c + (x->val == b);
				res += x->ch[0]->size + 1;
				x = x->ch[1];
			}
		}
	}
	int select(int l, int k, bool b) const {
		return select(k + rank(l, b), b);
	}
};

template <typename T>
class red_black_tree {

	struct node {
		COL color;
		int size;
		T val;
		node *p, *ch[2];
		node() : color(BLACK), size(0), val(), p(nullptr), ch{ nullptr, nullptr } {}
		node(COL c, T v, node *par, node *l, node *r) : color(c), size(1), val(v), p(par), ch{ l, r } {}
	} *NIL, *root;

	node* new_node(T val) const {
		return new node(RED, val, NIL, NIL, NIL);
	}
	void update(node *x) {
		x->size = x->ch[0]->size + 1 + x->ch[1]->size;
	}
	void update_up(node *x) {
		while (x != NIL) update(x), x = x->p;
	}
	void rotate(node *x, int b) {
		node *y = x->ch[1 - b];
		x->ch[1 - b] = y->ch[b];
		if (y->ch[b] != NIL) {
			y->ch[b]->p = x;
		}
		y->p = x->p;
		if (x->p == NIL) {
			root = y;
		}
		else {
			x->p->ch[x != x->p->ch[0]] = y;
		}
		y->ch[b] = x;
		x->p = y;
		update(x);
		update(y);
	}
	void insert_fix(node *x) {
		while (x->p->color == RED) {
			int b = (x->p != x->p->p->ch[0]);
			node *y = x->p->p->ch[1 - b];
			if (y->color == RED) {
				x->p->color = BLACK;
				y->color = BLACK;
				x->p->p->color = RED;
				x = x->p->p;
				continue;
			}
			if (x == x->p->ch[1 - b]) {
				x = x->p;
				rotate(x, b);
			}
			x->p->color = BLACK;
			x->p->p->color = RED;
			rotate(x->p->p, 1 - b);
		}
		root->color = BLACK;
	}
	void transplant(node *u, node *v) {
		if (u->p == NIL) {
			root = v;
		}
		else {
			u->p->ch[u != u->p->ch[0]] = v;
		}
		v->p = u->p;
	}
	void erase_fix(node *x) {
		while (x != root && x->color == BLACK) {
			int b = (x != x->p->ch[0]);
			node *w = x->p->ch[1 - b];
			if (w->color == RED) {
				w->color = BLACK;
				x->p->color = RED;
				rotate(x->p, b);
				w = x->p->ch[1 - b];
			}
			if (w->ch[b]->color == BLACK && w->ch[1 - b]->color == BLACK) {
				w->color = RED;
				x = x->p;
				continue;
			}
			if (w->ch[1 - b]->color == BLACK) {
				w->ch[b]->color = BLACK;
				w->color = RED;
				rotate(w, 1 - b);
				w = x->p->ch[1 - b];
			}
			w->color = x->p->color;
			x->p->color = BLACK;
			w->ch[1 - b]->color = BLACK;
			rotate(x->p, b);
			x = root;
		}
		x->color = BLACK;
	}
	node* find_first(node *x) const {
		if (x == NIL) return NIL;
		while (x->ch[0] != NIL) x = x->ch[0];
		return x;
	}
	node* find(node *t, int k) const {
		if (k < 0 || t->size <= k) return NIL;
		node *x = t;
		while (x->ch[0]->size != k) {
			if (k < x->ch[0]->size) {
				x = x->ch[0];
			}
			else {
				k -= x->ch[0]->size + 1;
				x = x->ch[1];
			}
		}
		return x;
	}

public:

	red_black_tree() : NIL(new node()), root(NIL) {}
	T find(int k) const {
		return find(root, k)->val;
	}
	void update(int k, T val) {
		node *t = find(root, k);
		t->val = val;
	}
	void insert(int k, T val) {
		node *y = NIL, *v = new_node(val);
		if (root == NIL) {
			root = v;
		}
		else if (k == 0) {
			y = find_first(root);
			y->ch[0] = v;
		}
		else {
			y = find(root, k - 1);
			if (y->ch[1] == NIL) {
				y->ch[1] = v;
			}
			else {
				y = find_first(y->ch[1]);
				y->ch[0] = v;
			}
		}
		v->p = y;
		update_up(y);
		insert_fix(v);
	}
	void erase(int k) {
		node *x = find(root, k);
		node *y = x, *z;
		COL c = y->color;
		if (x->ch[0] == NIL) {
			z = x->ch[1];
			transplant(x, x->ch[1]);
		}
		else if (x->ch[1] == NIL) {
			z = x->ch[0];
			transplant(x, x->ch[0]);
		}
		else {
			y = find_first(x->ch[1]);
			c = y->color;
			z = y->ch[1];
			if (y->p == x) {
				z->p = y;
			}
			else {
				transplant(y, y->ch[1]);
				y->ch[1] = x->ch[1];
				y->ch[1]->p = y;
			}
			transplant(x, y);
			y->ch[0] = x->ch[0];
			y->ch[0]->p = y;
			y->color = x->color;
			update(y);
		}
		update_up(z->p);
		if (c == BLACK) erase_fix(z);
	}

};

template <typename T>
class dynamic_wavelet_matrix {
	T h;
	red_black_tree<T> all;
	vector<FID> data;
	vector<int> mid;

public:
	dynamic_wavelet_matrix(T h_ = 30) : h(h_), data(h), mid(h) {}
	void insert(int k, T val) {
		all.insert(k, val);
		for (T b = 0; b < h; b++) {
			bool d = (val >> (h - 1 - b)) & 1;
			data[b].insert(k, d);
			if (d) {
				k = data[b].rank(k, d) + mid[b];
			}
			else {
				k = data[b].rank(k, d);
				mid[b]++;
			}
		}
	}
	void erase(int k) {
		T val = all.find(k);
		all.erase(k);
		for (T b = 0; b < h; b++) {
			bool d = (val >> (h - 1 - b)) & 1;
			data[b].erase(k);
			if (d) {
				k = data[b].rank(k, d) + mid[b];
			}
			else {
				k = data[b].rank(k, d);
				mid[b]--;
			}
		}
	}
	T get(int k) const {
		return all.find(k);
	}
	int rank(int p, T val) {
		return rank(0, p, val);
	}
	int rank(int l, int r, T val) {
		if (val >> h) return 0;
		for (T b = 0; b < h; b++) {
			if (val & ((T)1 << (h - 1 - b))) {
				l = data[b].rank(l, true) + mid[b];
				r = data[b].rank(r, true) + mid[b];
			}
			else {
				l = data[b].rank(l, false);
				r = data[b].rank(r, false);
			}
		}
		return r - l;
	}
	int rank_less_than(int l, int r, T ub) {
		if (ub >> h) return r - l;
		int res = 0;
		for (T b = 0; b < h; b++) {
			bool d = (ub >> (h - 1 - b)) & 1;
			int lcnt = data[b].rank(l, d);
			int rcnt = data[b].rank(r, d);
			if (d) res += (r - l) - (rcnt - lcnt);
			l = lcnt;
			r = rcnt;
			if (d) {
				l += mid[b];
				r += mid[b];
			}
		}
		return res;
	}
	int rank_range(int l, int r, T lb, T ub) {
		return rank_less_than(l, r, ub) - rank_less_than(l, r, lb);
	}
	int select(int x, T val) {
		static int left[h];
		int l = 0, r = data[0].size();
		for (T b = 0; b < h; b++) {
			left[b] = l;
			if (val & ((T)1 << (h - 1 - b))) {
				l = data[b].rank(l, true) + mid[b];
				r = data[b].rank(r, true) + mid[b];
			}
			else {
				l = data[b].rank(l, false);
				r = data[b].rank(r, false);
			}
		}
		for (int b = h - 1; b >= 0; b--) {
			x = data[b].select(left[b], x, (bool)((val >> (h - 1 - b)) & 1)) - left[b];
		}
		return x;
	}
	int select(int l, int r, int x, T val) {
		return select(x + rank(l, val), val);
	}
	T kth_element(int l, int r, int k) const {
		T res = 0;
		for (T b = 0; b < h; b++) {
			int cnt = data[b].rank(l, r, false);
			res <<= 1;
			if (k >= cnt) {
				l = data[b].rank(l, true) + mid[b];
				r = data[b].rank(r, true) + mid[b];
				k -= cnt;
				res |= 1;
			}
			else {
				l = data[b].rank(l, false);
				r = data[b].rank(r, false);
			}
		}
		return res;
	}
};

int main()
{
	ios::sync_with_stdio(false), cin.tie(0);
	int n, q;
	cin >> n >> q;
	dynamic_wavelet_matrix<int> dwm(18);
	for (int i = 0; i < n; i++) {
		dwm.insert(i, i + 1);
	}
	ll res = 0;
	while (q--) {
		int l, r;
		cin >> l >> r; l--, r--;
		if (l == r) {
			printf("%lld\n", res);
			continue;
		}
		if (l > r) swap(l, r);
		int lv = dwm.get(l), rv = dwm.get(r);
		int lcnt = dwm.rank_less_than(l + 1, r, lv), rcnt = dwm.rank_less_than(l + 1, r, rv);
		res += rcnt * 2 - lcnt * 2 + (lv < rv ? 1 : -1);
		printf("%lld\n", res);
		dwm.erase(l);
		dwm.insert(l, rv);
		dwm.erase(r);
		dwm.insert(r, lv);
	}
	return 0;
}