求逆序对个数,莫队套树状数组

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define N 50005
using namespace std;
int n,m,nn,a[N],c[5000005],be[N],maxn;
struct Query{
int l,r,id,ans;
}qr[N];
bool cmp1(Query a,Query b){
if(be[a.l]==be[b.l])
return a.r<b.r;
return be[a.l]<be[b.l];
}
bool cmp2(Query a,Query b){
return a.id<b.id;
}
int lowbit(int x){
return x&(-x);
}
void add(int x,int y){
while(x<=maxn)
{c[x]+=y;x+=lowbit(x);}
}
int query(int x){
int ans=0;
while(x)
{ans+=c[x];x-=lowbit(x);}
return ans;
}
void work(){
int l=1,r=0,tot=0;
for(int i=1;i<=m;i++){
while(l<qr[i].l){
tot-=query(a[l]-1);
add(a[l++],-1);
}
while(l>qr[i].l){
add(a[--l],1);
tot+=query(a[l]-1);
}
while(r<qr[i].r){
add(a[++r],1);
tot+=r-l+1-query(a[r]);
//printf("%d %d\n",query(a[r]),tot);
}
while(r>qr[i].r){
tot-=r-l+1-query(a[r]);
add(a[r--],-1);
}
qr[i].ans=tot;
}
}
int main()
{
scanf("%d",&n); nn=sqrt(n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
maxn=max(maxn,a[i]);
be[i]=(i-1)/nn+1;
}
scanf("%d",&m);
int l,r;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&l,&r);
qr[i].l=l; qr[i].r=r;
qr[i].id=i;
}
sort(qr+1,qr+m+1,cmp1);
work();
sort(qr+1,qr+m+1,cmp2);
for(int i=1;i<=m;i++)
printf("%d\n",qr[i].ans);
return 0;
}

bzoj3289 Mato的文件管理 莫队+树状数组的更多相关文章

  1. bzoj 3289: Mato的文件管理 莫队+树状数组

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description Mato同学 ...

  2. BZOJ3289[JZYZOJP2018]: Mato的文件管理 莫队+树状数组+离散化

            描述 Description     Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号.为了防止他人偷拷,这些资料都是加密过的, ...

  3. 【BZOJ3289】Mato的文件管理 莫队+树状数组

    题目大意:给定一个长度为 N 的序列,M 个询问,每次询问区间逆序对的个数. 题解:用树状数组加速答案转移. 代码如下 #include <bits/stdc++.h> #define f ...

  4. Bzoj 3289: Mato的文件管理 莫队,树状数组,逆序对,离散化,分块

    3289: Mato的文件管理 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 1539  Solved: 665[Submit][Status][Di ...

  5. bzoj 3289 : Mato的文件管理 (莫队+树状数组)

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3289 思路: 求区间最小交换的次数将区间变成一个不降序列其实就是求区间逆序对的数量,这 ...

  6. BZOJ_3289_Mato的文件管理_莫队+树状数组

    BZOJ_3289_Mato的文件管理_莫队+树状数组 Description Mato同学从各路神犇以各种方式(你们懂的)收集了许多资料,这些资料一共有n份,每份有一个大小和一个编号 .为了防止他人 ...

  7. bzoj3236 作业 莫队+树状数组

    莫队+树状数组 #include<cstdio> #include<cstring> #include<iostream> #include<algorith ...

  8. BZOJ3236[Ahoi2013]作业——莫队+树状数组/莫队+分块

    题目描述 输入 输出 样例输入 3 4 1 2 2 1 2 1 3 1 2 1 1 1 3 1 3 2 3 2 3 样例输出 2 2 1 1 3 2 2 1 提示 N=100000,M=1000000 ...

  9. COGS.1822.[AHOI2013]作业(莫队 树状数组/分块)

    题目链接: COGS.BZOJ3236 Upd: 树状数组实现的是单点加 区间求和,采用值域分块可以\(O(1)\)修改\(O(sqrt(n))\)查询.同BZOJ3809. 莫队为\(O(n^{1. ...

随机推荐

  1. same tree(判断两颗二叉树是否相等)

    Input: 1 1 / \ / \ 2 3 2 3 [1,2,3], [1,2,3] Output: true Example 2: Input: 1 1 / \ 2 2 [1,2], [1,nul ...

  2. 网站SEO优化问答精选

    1.百度每更新一次,网站的收录就减少很多,但是我每天都增加伪原创的内容啊? 这个问题大多数是因为网站权重导致百度不够重视你:另外就是文章质量度不高,没有可读性或是原创度太低,尽管百度会收录,但是经过一 ...

  3. 并发编程(五):CAS

    在atomic包中,大多数类都是借助unsafe类来实现的,如以下代码 public static AtomicInteger count = new AtomicInteger(0); privat ...

  4. apache压力测试工具的apache bench和JMeter的安装

    Apache压力测试工具的安装 1,apache bench的安装 apache  bench工具集成在http的软件包内,可以直接安装apache就可以. 当有些时候,我们不需要用到所有的软件包,我 ...

  5. gevent程序员指南

    gevent程序员指南 由Gevent社区编写 gevent是一个基于libev的并发库.它为各种并发和网络相关的任务提供了整洁的API.   介绍 本指南假定读者有中级Python水平,但不要求有其 ...

  6. java之MVC开发模式

    友情提醒:eclipse创建servlet不会帮你同步创建一个web.xml配置文件:所以建议使用MyEclipse! 1.View层(jsp) <form action="javaw ...

  7. C#更改操作系统时间

    using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServi ...

  8. 非正则表达式检验邮箱格式是否合法(Java代码实现)

    一.邮箱格式需满足以下要求 1. 有且只有一个@                        2. @不能放在开头,也不能放在结尾                        3. @之后必须有. ...

  9. robot framework之弹出窗口的处理关键字实战

    1.1  弹出窗口的处理关键字 5.8.1 Alert Should Be Present关键字 按F5 查看Alert Should Be Present关键字的说明,如下图

  10. Python_doc文件写入SQLite数据库

    #docx文档题库包含很多段,每段一个题目,格式为:问题.(答案) #数据库datase.db中tiku表包含kechengmingcheng.zhanngji.timu.daan四个字段 impor ...