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. jQuery方法输出有几个checkbox框被选中

    每选中一个多选框,输出有多少个选中 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...

  2. 怎样通过js 取消input域的hidden属性使其变的可见

    document.getElementById(ID).setAttribute("hidden",false);厉害了 我的哥!

  3. kappa系数在评测中的应用

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/7091315.html 前言 最近打算把翻译质量的人工评测好 ...

  4. httpd常用配置

    author:JevonWei 版权声明:原创作品 检查配置文件时,如下提示,则因为没有server的服务名称导致,故设置网站的服务server名称,若没有设置web服务名,主默认解析系统主机名(添加 ...

  5. poj3463 最短路和比最短路长1的路径数

    这题需要很好的理解Dij. 在Dij的基础上,每个点多一个次短路的长度和数量进行控制. 那么在队列中,最短路控制时出现n次,次短路控制出现n次.注意松弛条件中val值和最短路.次短路的关系. 这题需要 ...

  6. Linux安装简介

    一.基本简介 Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统. Linux能运行主要的UNIX工具软件.应用程序 ...

  7. setAttribute设置无效

    我发现ie浏览器中动态用setAttribute设置style属性值始终不能设置,经过一番查找发现了这篇文字 http://webcenter.hit.edu.cn/articles/2009/05- ...

  8. ASP.NET 控制器

    1.继承Controller public class TestController : Controller2.编写控制器方法    // URL  :   test/Edit/1        [ ...

  9. Tomca软件介绍和安装

    Web开发入门 软件的结构: C/S (Client - Server  客户端-服务器端) 典型应用:QQ软件 ,飞秋,红蜘蛛. 特点: 1)必须下载特定的客户端程序. 2)服务器端升级,客户端升级 ...

  10. 个人作业3——(Alpha阶段)

    一.alpha 过程总结 1.这学期的软件工程作业,由于各种事情冲突和时间安排问题,前期并没有太多时间去好好应对,中途有很多次都想放弃.但最后还是咬咬牙在同学的帮助下完成了一些基本任务,在这门课程开始 ...