BZOJ3295 [Cqoi2011]动态逆序对
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
Description
Input
Output
Sample Input
1
5
3
4
2
5
1
4
2
Sample Output
2
2
1
样例解释
(1,5,3,4,2)(1,3,4,2)(3,4,2)(3,2)(3)。
HINT
N<=100000 M<=50000
//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int inf = (<<);
const int MAXN = ;
int n,m,c[MAXN],match[MAXN],ans[MAXN];
LL Ans;
struct node{
int x,y,t;
int flag;
}a[MAXN],b[MAXN];
inline bool cmpx(node q,node qq){ if(q.x==qq.x) return q.y<qq.y; return q.x<qq.x; }
inline bool cmpt(node q,node qq){ return q.t<qq.t; }
inline void add(int x,int val){ while(x<=n) c[x]+=val,x+=x&(-x); }
inline int query(int x){int tot=; while(x>) tot+=c[x],x-=x&(-x); return tot; }
inline int getint()
{
int w=,q=; char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} inline void CDQ(int l,int r){
if(l>=r) return ; int mid=(l+r)>>,size=r-l+,cnt=;
for(int i=l;i<=mid;i++) b[++cnt]=a[i],b[cnt].flag=; for(int i=mid+;i<=r;i++) b[++cnt]=a[i],b[cnt].flag=;
sort(b+,b+cnt+,cmpx); //for(int i=1;i<=n;i++) c[i]=0;
for(int i=;i<=size;i++) {
if(b[i].flag==) add(b[i].y,);
else ans[b[i].t]+=query(n)-query(b[i].y);
}
for(int i=;i<=size;i++) if(b[i].flag==) add(b[i].y,-);
for(int i=size;i>=;i--) {
if(b[i].flag==) add(b[i].y,);
else ans[b[i].t]+=query(b[i].y);
}
for(int i=;i<=size;i++) if(b[i].flag==) add(b[i].y,-);
CDQ(l,mid); if(mid<r) CDQ(mid+,r);
} inline void work(){
n=getint(); m=getint(); for(int i=;i<=n;i++) { a[i].x=i; a[i].y=getint(); match[a[i].y]=i; } int cc=n,x;
for(int i=;i<=m;i++) { x=getint(); a[match[x]].t=cc--; } for(int i=;i<=n;i++) if(a[i].t==) a[i].t=cc--;
sort(a+,a+n+,cmpt); CDQ(,n);
for(int i=;i<=n;i++) Ans+=ans[i];
for(int i=n;i>n-m;i--) {
printf("%lld\n",Ans);
Ans-=ans[i];
}
} int main()
{
work();
return ;
}
BZOJ3295 [Cqoi2011]动态逆序对的更多相关文章
- bzoj3295 [Cqoi2011]动态逆序对 cdq+树状数组
[bzoj3295][Cqoi2011]动态逆序对 2014年6月17日4,7954 Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数. ...
- bzoj3295[Cqoi2011]动态逆序对 树套树
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5987 Solved: 2080[Submit][Sta ...
- 2018.07.01 BZOJ3295: [Cqoi2011]动态逆序对(带修主席树)
3295: [Cqoi2011]动态逆序对 **Time Limit: 10 Sec Memory Limit: 128 MB Description 对于序列A,它的逆序对数定义为满足i<j& ...
- [BZOJ3295][Cqoi2011]动态逆序对 CDQ分治&树套树
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MB Description 对于序列A,它的逆序对数定义为满足i<j,且 ...
- bzoj千题计划146:bzoj3295: [Cqoi2011]动态逆序对
http://www.lydsy.com/JudgeOnline/problem.php?id=3295 正着删除看做倒着添加 对答案有贡献的数对满足以下3个条件: 出现时间:i<=j 权值大小 ...
- BZOJ3295: [Cqoi2011]动态逆序对(树状数组套主席树)
3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 7465 Solved: 2662[Submit][Sta ...
- BZOJ3295 [Cqoi2011]动态逆序对 —— CDQ分治
题目链接:https://vjudge.net/problem/HYSBZ-3295 3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 1 ...
- bzoj3295: [Cqoi2011]动态逆序对(cdq分治+树状数组)
3295: [Cqoi2011]动态逆序对 题目:传送门 题解: 刚学完cdq分治,想起来之前有一道是树套树的题目可以用cdq分治来做...尝试一波 还是太弱了...想到了要做两次cdq...然后伏地 ...
- [bzoj3295][Cqoi2011]动态逆序对_主席树
动态逆序对 bzoj-3295 Cqoi-2011 题目大意:题目链接. 注释:略. 想法:直接建立主席树. 由于是一个一个删除,所以我们先拿建立好的root[n]的权值线段树先把总逆序对求出来,接着 ...
- bzoj3295: [Cqoi2011]动态逆序对(树套树)
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
随机推荐
- java:利用xpath删除xml中的空节点
原始xml内容: <data> <a> </a> <b>b1</b> <awb> <awbpre>123</a ...
- Spring Security授权 AccessDecisionManager
Spring Security授权 AccessDecisionManager 博客分类: Security Spring 在前面那篇博客有一段配置: <http auto-config=& ...
- 【语言基础】c++ 备忘录
1. C++ 整数类型范围 可以参照头文件limits.h定义的宏 #define INT_MAX 2147483647(32bit, 最大10位十进制) #define UINT_MAX ...
- MATLAB调用C程序、调试和LDPC译码
MATLAB是一个很好用的工具.利用MATLAB脚本进行科学计算也特别方便快捷.但是代码存在较多循环时,MATLAB运行速度极慢.如果不想放弃MATLAB中大量方便使用的库,又希望代码能迅速快捷的运行 ...
- Display: table-cell实现img、文字垂直居中
在文章开头先说明一下此方法的兼容性,IE8+以及其他现代浏览器都支持此属性. 直接献上一个demo吧 <!DOCTYPE html> <html> <head> & ...
- LiveSDK初始化/登录时失败的解决办法
环境描述 Windows 8.1+VS 2013 Update3+Live SDK 5.6 Metro风格的程序,集成LIVE认证 问题描述 如下图,提示Null Reference的异常. 解决办法 ...
- SQL2008R2 不支持用该后端版本设计数据库关系图或表
向下不兼容. 要么安装SQL2012,要么把SQL2012数据库通过脚本转成2008
- 使用Topshelf创建Windows服务
概述 Topshelf是创建Windows服务的另一种方法,老外的一篇文章Create a .NET Windows Service in 5 steps with Topshelf通过5个步骤详细的 ...
- Ubuntu Terminal Shortcut
Not all of the shortcuts are useful.Only remeber the most useful. 移动类Ctrl + a - Jump to the start o ...
- mysql性能优化-慢查询分析、优化索引和配置
一.优化概述 二.查询与索引优化分析 1性能瓶颈定位 Show命令 慢查询日志 explain分析查询 profiling分析查询 2索引及查询优化 三.配置优化 1) max_connec ...