Dynamic Inversions

Time Limit: 30000/15000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
SubmitStatus

Problem Description

给出N个数a[1],a[2] ... a[N],有M个操作,每个操作给出x,y两个数,你将a[x],a[y]交换,然后求交换后数组的逆序对个数。
逆序对的意思是1 <= i < j <= N 且a[i] > a[j].

Input

多组数据,每组数据:

一个N,接下来一行有N个数a[1]... a[N]

再下去一行是M,最后M行每行两个数x,y

1 <= N,M <= 10^5, 1 <= x,y <= N,1 <= a[i] <= 50

Output

对于每组数据,输出M + 1行,第一行是开始时的逆序对数目,接下去M行每行一个数,表示这次修改后的逆序对数目

Sample Input

2
1 2
1
2 1
3
1 2 3
3
1 2
2 3
3 1

Sample Output

0
1
0
1
2
1

Hint

第二个样例,一开始1 2 3,逆序对数为0,交换第1,2个元素,变为2 1 3,答案为1,交换第2和3个元素,变为2 3 1,逆序对数为2,交换第3和1个元素,逆序对数为1,此时序列变成1 3  2

二维树状数组做法:

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define ll long long
int b[][],a[],n,c[];
int lowbit(int x)
{
return x&(-x);
}
void update1(int x)
{
while(x<)
{
c[x]++;
x+=lowbit(x);
}
}
int query1(int x)
{
int sum=;
while(x>)
{
sum+=c[x];
x-=lowbit(x);
}
return sum;
}
void update(int x,int y,int z)
{
for(int i=x; i<=; i+=lowbit(i))
for(int j=y; j<=n; j+=lowbit(j))
b[i][j]+=z;
}
int query(int x,int y,int x1,int y1)
{
int ans=,i,j;
for(i=y;i>;i-=lowbit(i))
for(j=y1;j>;j-=lowbit(j))
ans+=b[i][j]; for(i=x-;i>;i-=lowbit(i))
for(j=y1;j>;j-=lowbit(j))
ans-=b[i][j]; for(i=y;i>;i-=lowbit(i))
for(j=x1-;j>;j-=lowbit(j))
ans-=b[i][j]; for(i=x-;i>;i-=lowbit(i))
for(j=x1-;j>;j-=lowbit(j))
ans+=b[i][j];
return ans;
}
int main()
{
int i,m,x,y;
ll ans;
while(~scanf("%d",&n))
{
ans=;
memset(b,,sizeof(b));
memset(c,,sizeof(c));
for(i=; i<=n; i++)
{
scanf("%d",&a[i]);
update1(a[i]);
ans+=i-query1(a[i]);
update(a[i],i,);
}
printf("%lld\n",ans);
scanf("%d",&m);
for(i=; i<m; i++)
{
scanf("%d%d",&x,&y);
if(x>y)swap(x,y);
ans-=query(a[y]+,,x+,y);
ans+=query(,a[y]-,x+,y);
ans+=query(a[x]+,,x,y-);
ans-=query(,a[x]-,x,y-);
if(a[x]>a[y])ans--;
else if(a[x]<a[y])ans++;
update(a[x],x,-);
update(a[x],y,);
update(a[y],y,-);
update(a[y],x,);
swap(a[x],a[y]);
printf("%lld\n",ans);
}
}
}

50个一维的做法:

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define ll long long
int a[][],b[],n,c[];
int lowbit(int x)
{
return x&(-x);
}
int update(int x)
{
while(x<)
{
b[x]++;
x+=lowbit(x);
}
}
int update1(int i,int x,int y)
{
while(x<=n)
{
a[i][x]+=y;
x+=lowbit(x);
}
}
int query(int x)
{
int sum=;
while(x>)
{
sum+=b[x];
x-=lowbit(x);
}
return sum;
}
int query1(int i,int x)
{
int sum=;
while(x>)
{
sum+=a[i][x];
x-=lowbit(x);
}
return sum;
}
int main()
{
int i,j,m,x,y,z;
ll ans,temp,temp1;
while(~scanf("%d",&n))
{
memset(b,,sizeof(b));
memset(a,,sizeof(a));
memset(c,,sizeof(c));
ans=;
for(i=; i<=n; i++)
{
scanf("%d",&a[][i]);
c[a[][i]]++;
ans+=i-query(a[][i])-;
update(a[][i]);
update1(a[][i],i,);
}
printf("%lld\n",ans);
scanf("%d",&m);
for(i=; i<m; i++)
{
scanf("%d%d",&x,&y);
if(x>y)swap(x,y);
z=y-x;
temp=;
for(j=; j<a[][y]; j++)
{
temp+=query1(j,y-)-query1(j,x-);
}
temp1=query1(j,y-)-query1(j,x-);
ans+=temp-(z-temp1-temp);
temp=;
for(j=; j<a[][x]; j++)
{
temp+=query1(j,y-)-query1(j,x-);
}
temp1=query1(j,y-)-query1(j,x-);
ans+=(z-temp1-temp)-temp;
update1(a[][x],x,-);
update1(a[][x],y,);
update1(a[][y],y,-);
update1(a[][y],x,);
swap(a[][y],a[][x]);
printf("%lld\n",ans);
}
}
}

Dynamic Inversions 50个树状数组的更多相关文章

  1. [bzoj1901][zoj2112][Dynamic Rankings] (整体二分+树状数组 or 动态开点线段树 or 主席树)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  2. HDU 6318 Swaps and Inversions(归并排序 || 树状数组)题解

    题意:一个逆序对罚钱x元,现在给你交换的机会,每交换任意相邻两个数花钱y,问你最少付多少钱 思路:最近在补之前还没过的题,发现了这道多校的题.显然,交换相邻两个数逆序对必然会变化+1或者-1,那我们肯 ...

  3. BZOJ1901: Zju2112 Dynamic Rankings(整体二分 树状数组)

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 9094  Solved: 3808[Submit][Status][Discuss] Descript ...

  4. BZOJ 1901 Dynamic Rankings (整体二分+树状数组)

    题目大意:略 洛谷传送门 这道题在洛谷上数据比较强 貌似这个题比较常见的写法是树状数组套主席树,动态修改 我写的是整体二分 一开始的序列全都视为插入 对于修改操作,把它拆分成插入和删除两个操作 像$C ...

  5. Swaps and Inversions HDU - 6318 树状数组+离散化

    #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> us ...

  6. Dynamic Rankings ZOJ - 2112(主席树+树状数组)

    The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with t ...

  7. ZOJ 2112 Dynamic Rankings (动态第k大,树状数组套主席树)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  8. ZOJ 2112 Dynamic Rankings(树状数组+主席树)

    The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with t ...

  9. Bzoj 1901: Zju2112 Dynamic Rankings 主席树,可持久,树状数组,离散化

    1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 6321  Solved: 2628[Su ...

随机推荐

  1. jmeter系列------参数关联

    接口请求中的一个变量需要用上一个接口道服务器返回响应的动态值(上个请求). 遇到这样的情况,我们就需要用到关联 例如用户A发表了一个一条微信朋友圈,用户B想对这条朋友圈进行评论,就需要先获取到这个朋友 ...

  2. jq获取图片的原始尺寸,自适应布局

    原理: each()遍历,width().height()获取宽高, load() 注意: 由于页面加载完了,但图片不一定加载完了,所以直接通过 $("img").width(), ...

  3. 读取Execl表 导入数据库

    不知不觉博客园园林都两年多了,我是今年毕业的应届生,最近公司项目需要改动,很多的数据需要导入,很多的实体类需要些.考虑到这些问题自己写了两个winform版的小工具,一个是读取Execl数据导入数据库 ...

  4. FTP的主动和被动模式详解

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp25 主动模式FTP与被动模式FTP该如何选择 一.主动模式的实现与特点. ...

  5. Spring中ApplicationContextAware的用法

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt379 一.这个接口有什么用? 当一个类实现了这个接口(Application ...

  6. 3_SQL Server通过代码的方式添加数据

    --通过代码添加数据 --第一种方式--insert into 表名(列名1,列名2,...)values (值1,值2,...)insert into Department(DepName, Dep ...

  7. 转:【Java集合源码剖析】Java集合框架

    转载轻注明出处:http://blog.csdn.net/ns_code/article/details/35564663   Java集合工具包位于Java.util包下,包含了很多常用的数据结构, ...

  8. SNS团队第五次站立会议(2017.04.26)

    一.当天站立式会议照片 本次会议主要内容:汇报工作进度,根据完成情况调整进度 二.每个人的工作 成员 今天已完成的工作 明天计划完成的工作 罗于婕 完善数据库文件 根据需求完善数据库文件 龚晓婷 编写 ...

  9. 201521123075 《Java程序设计》第3周学习总结

    1. 本周学习总结 2. 书面作业 1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; p ...

  10. 201521123014 《Java程序设计》第3周学习总结

    1. 本周学习总结 2. 书面作业 Q1. 代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; ...