Anton and Permutation
4 seconds
512 megabytes
standard input
standard output
Anton likes permutations, especially he likes to permute their elements. Note that a permutation of n elements is a sequence of numbers{a1, a2, ..., an}, in which every number from 1 to n appears exactly once.
One day Anton got a new permutation and started to play with it. He does the following operation q times: he takes two elements of the permutation and swaps these elements. After each operation he asks his friend Vanya, how many inversions there are in the new permutation. The number of inversions in a permutation is the number of distinct pairs (i, j) such that 1 ≤ i < j ≤ n and ai > aj.
Vanya is tired of answering Anton's silly questions. So he asked you to write a program that would answer these questions instead of him.
Initially Anton's permutation was {1, 2, ..., n}, that is ai = i for all i such that 1 ≤ i ≤ n.
The first line of the input contains two integers n and q (1 ≤ n ≤ 200 000, 1 ≤ q ≤ 50 000) — the length of the permutation and the number of operations that Anton does.
Each of the following q lines of the input contains two integers li and ri (1 ≤ li, ri ≤ n) — the indices of elements that Anton swaps during the i-th operation. Note that indices of elements that Anton swaps during the i-th operation can coincide. Elements in the permutation are numbered starting with one.
Output q lines. The i-th line of the output is the number of inversions in the Anton's permutation after the i-th operation.
5 4
4 5
2 4
2 5
2 2
1
4
3
3
2 1
2 1
1
6 7
1 4
3 5
2 3
3 3
3 6
2 1
5 1
5
6
7
7
10
11
8
Consider the first sample.
After the first Anton's operation the permutation will be {1, 2, 3, 5, 4}. There is only one inversion in it: (4, 5).
After the second Anton's operation the permutation will be {1, 5, 3, 2, 4}. There are four inversions: (2, 3), (2, 4), (2, 5) and (3, 4).
After the third Anton's operation the permutation will be {1, 4, 3, 2, 5}. There are three inversions: (2, 3), (2, 4) and (3, 4).
After the fourth Anton's operation the permutation doesn't change, so there are still three inversions.
分析:主席树套树状数组,注意开始静态建树,防止爆内存;
分块法待学,orz~
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=5e4+;
const int N=2e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p%mod;p=p*p%mod;q>>=;}return f;}
int n,m,k,t,s[maxn**+N*],ls[maxn**+N*],rs[maxn**+N*],root[N*],lx[],rx[],rt[maxn<<],pos[maxn<<],sz;
ll ret;
void insert(int l,int r,int x,int &y,int z,int k,int t)
{
if(!t)y=++sz;
else if(t&&!y)y=++sz;
s[y]=s[x]+k;
if(l==r)return;
int mid=l+r>>;
ls[y]=ls[x],rs[y]=rs[x];
if(z<=mid)insert(l,mid,ls[x],ls[y],z,k,t);
else insert(mid+,r,rs[x],rs[y],z,k,t);
}
void add(int x,int y,int z)
{
while(y<=n)
{
insert(,n,rt[y],rt[y],x,z,);
y+=y&(-y);
}
}
int ask_more(int x,int y,int z)
{
int l=,r=n,ret=,i;
while(l!=r)
{
int mid=l+r>>;
if(x<=mid)
{
rep(i,,lx[])ret-=s[rs[lx[i]]],lx[i]=ls[lx[i]];
rep(i,,rx[])ret+=s[rs[rx[i]]],rx[i]=ls[rx[i]];
ret-=s[rs[y]],y=ls[y];
ret+=s[rs[z]],z=ls[z];
r=mid;
}
else
{
rep(i,,lx[])lx[i]=rs[lx[i]];
rep(i,,rx[])rx[i]=rs[rx[i]];
y=rs[y],z=rs[z];
l=mid+;
}
}
return ret;
}
int gao(int x,int l,int r)
{
int ret=,_l=l,_r=r;
lx[]=rx[]=;
while(l)lx[++lx[]]=rt[l],l-=l&(-l);
while(r)rx[++rx[]]=rt[r],r-=r&(-r);
ret+=ask_more(x,root[_l],root[_r]);
return ret;
}
int main()
{
int i,j;
scanf("%d%d",&n,&m);
rep(i,,n)pos[i]=i,insert(,n,root[i-],root[i],i,,);
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
if(a==b)
{
printf("%lld\n",ret);
continue;
}
if(pos[a]>pos[b])swap(a,b);
int x=gao(a,pos[a]-,pos[b]),y=gao(b,pos[a]-,pos[b]);
ret+=x-(pos[b]-pos[a]-x);
ret-=y-(pos[b]-pos[a]-y);
if(a<b)ret--;
else ret++;
printf("%lld\n",ret);
add(a,pos[a],-);
add(b,pos[b],-);
add(a,pos[b],);
add(b,pos[a],);
swap(pos[a],pos[b]);
}
return ;
}
Anton and Permutation的更多相关文章
- Codeforces785E - Anton and Permutation
Portal Description 对一个长度为\(n(n\leq2\times10^5)\)的数列\(a\)进行\(m(m\leq5\times10^4)\)次操作,数列初始时为\(\{1,2,. ...
- Codeforces Round #404 (Div. 2) E. Anton and Permutation(树状数组套主席树 求出指定数的排名)
E. Anton and Permutation time limit per test 4 seconds memory limit per test 512 megabytes input sta ...
- Codeforces 785 E. Anton and Permutation(分块,树状数组)
Codeforces 785 E. Anton and Permutation 题目大意:给出n,q.n代表有一个元素从1到n的数组(对应索引1~n),q表示有q个查询.每次查询给出两个数l,r,要求 ...
- Codeforces 785E. Anton and Permutation
题目链接:http://codeforces.com/problemset/problem/785/E 其实可以CDQ分治... 我们只要用一个数据结构支持单点修改,区间查询比一个数大(小)的数字有多 ...
- [CF785E]Anton and Permutation
题目大意:有一串数为$1\sim n(n\leqslant2\times10^5)$,$m(m\leqslant5\times10^4)$次询问,每次问交换位置为$l,r$的两个数后数列中逆序对的个数 ...
- Codeforces 785E Anton and Permutation(分块)
[题目链接] http://codeforces.com/contest/785/problem/E [题目大意] 一个1到n顺序排列的数列,每次选择两个位置的数进行交换,求交换后的数列的逆序对数 [ ...
- CodeForces 785E Anton and Permutation 分块
题意: 有一个\(1 \sim n\)的排列\(A\),有\(q\)个询问: 交换任意两个元素的位置,求交换之后排列的逆序数 分析: 像这种不太容易用线段树,树状数组维护的可以考虑分块 每\(\sqr ...
- 【codeforces 785E】Anton and Permutation
[题目链接]:http://codeforces.com/problemset/problem/785/E [题意] 给你一个初始序列1..n顺序 然后每次让你交换任意两个位置上面的数字; 让你实时输 ...
- 题解 CF785E 【Anton and Permutation】
考虑用分块解决这个题,一次交换对当前逆序对个数的影响是,加上两倍的在区间\([l+1,r-1]\)中比\(a_r\)小的元素个数,减去两倍的在区间\([l+1,r-1]\)中比\(a_l\)小的元素个 ...
随机推荐
- MAC地址 初识
MAC地址 即物理地址/硬件地址 地址长度为48位,6字节. 格式为:00-23-5A-15-99-42 一个网卡对应一个MAC地址(比如笔记本,有线网卡有一个MAC地址,无线网卡也有一个MAC地址) ...
- E20170623-ts
filter n. 滤波器; 滤光器; 滤色镜; [化] 过滤器; mass n. 大量,大多; 块,堆,团; [物理学] 质量; 弥撒曲; assignment n. 分给,分配; 任务, ...
- hdu1814Peaceful Commission(2-SAT)
Peaceful Commission Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- [Swift通天遁地]二、表格表单-(2)创建右侧带有索引的UITableView(表单视图)
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [Swift通天遁地]七、数据与安全-(7)创建文件浏览器:以可视化的方式浏览沙箱文件
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 【学习笔记】线段树—扫描线补充 (IC_QQQ)
[学习笔记]线段树-扫描线补充 (IC_QQQ) (感谢 \(IC\)_\(QQQ\) 大佬授以本内容的著作权.此人超然于世外,仅有 \(Luogu\) 账号 尚可膜拜) [学习笔记]线段树详解(全) ...
- Spring-Security-OAuth2微信网页授权
@Controller public class Controller1 { @Autowired private OAuth2ClientContext context; @Bean @Scope( ...
- 更新换代之requests库
好久不更新博客了... 之前的博文都是通过urllib2进行http访问,接下来我要说一个利器啊!requests模块,无法用语言对他进行赞扬了,需要的,有兴趣的,可以去了解下,移步官方中文文档: R ...
- String字符串的完美度
题目详情: 我们要给每个字母配一个1-26之间的整数,具体怎么分配由你决定,但不同字母的完美度不同, 而一个字符串的完美度等于它里面所有字母的完美度之和,且不在乎字母大小写,也就是说字母F和f的完美度 ...
- 1、Xamarin 环境准备
一.安装环境 1.安装VS2017 2.安装Xamarin扩展包 3.安装NDK 工具栏中,一个为AVD(模拟器管理)一个为NDK(管理Andoid SDK) 3.1 先打开NDK,在Tools\Op ...