BZOJ_3289_Mato的文件管理_莫队+树状数组
BZOJ_3289_Mato的文件管理_莫队+树状数组
Description
Input
Output
q行,每行一个正整数,表示Mato这天需要交换的次数。
Sample Input
1 4 2 3
2
1 2
2 4
Sample Output
2
//样例解释:第一天,Mato不需要交换
第二天,Mato可以把2号交换2次移到最后。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define N 50050
#define LL long long
int c[N], n, q, v[N], pos[N];
struct A {
int num,d,id;
}b[N];
bool cmp3(const A &x,const A &y) {return x.num<y.num; }
bool cmp4(const A &x,const A &y) {return x.id < y.id ; }
struct B {
int s,t,id;
LL ans;
}a[N];
bool cmp1(const B &x,const B &y) {
if(pos[y.s] == pos[x.s]) return x.t < y.t;
return pos[x.s] < pos[y.s];
}
bool cmp2(const B &x,const B &y) {
return x.id < y.id;
}
LL now;
int main() {
scanf("%d",&n);
int i, j, block = sqrt(n), l, r = 0;
for(i = 1;i <= block; ++ i) {
l = r + 1;
r = i * block;
for(j = l;j <= r; ++ j) pos[j] = i;
}
if(r != n) {
++ block;
l = r + 1;
r = n;
for(i = l;i <= r; ++ i) pos[i] = block;
}
for(i = 1;i <= n; ++ i) scanf("%d", &b[i].num), b[i].id = i;
sort(b + 1, b + n + 1, cmp3); b[0].num = -84234820;
for(i = 1, j = 0;i <= n; ++ i) {if(b[i].num != b[i - 1].num)j ++; b[i].d = j; }
sort(b + 1, b + n + 1, cmp4);
for(i = 1;i <= n; ++ i) v[i] = b[i].d;
scanf("%d", &q);
for(i = 1;i <= q; ++ i) scanf("%d%d", &a[i].s, &a[i].t),a[i].id = i;
sort(a + 1, a + q + 1, cmp1);
for(l = 1, r = 0, i = 1;i <= q; ++ i) {
while(l < a[i].s) {
int t = v[l] - 1, re = 0;
for(; t ;t -= t & (-t)) re += c[t];
now -= re;
t = v[l];
for(;t <= n;t += t & (-t)) c[t]--;
l ++;
}
while(l > a[i].s) {
int t = v[l - 1] - 1, re = 0;
for(; t ;t -= t & (-t)) re += c[t];
now += re;
t = v[l - 1];
for(;t <= n;t += t & (-t)) c[t]++;
l --;
}
while(r > a[i].t) {
int t = v[r], re = 0;
for(; t ;t -= t & (-t)) re += c[t];
re = r - l + 1- re;
now -= re;
t = v[r];
for(;t <= n;t += t & (-t)) c[t]--;
r --;
}
while(r < a[i].t) {
int t = v[r + 1], re = 0;
for(; t ;t -= t & (-t)) re += c[t];
re = r - l + 1 - re;
now += re;
t = v[r + 1];
for(;t <= n;t += t & (-t)) c[t]++;
r ++;
}
a[i].ans = now;
}
sort(a + 1, a + q + 1, cmp2);
for(i = 1;i <= q; ++ i) printf("%lld\n",a[i].ans);
}
BZOJ_3289_Mato的文件管理_莫队+树状数组的更多相关文章
- BZOJ 3289: Mato的文件管理 【莫队 + 树状数组】
任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=3289 3289: Mato的文件管理 Time Limit: 40 Sec Memory ...
- BZOJ3289 Mato的文件管理 【莫队 + 树状数组】
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MB Submit: 3964 Solved: 1613 [Submit][Status] ...
- BZOJ 3289 Mato的文件管理(莫队+树状数组)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3289 [题目大意] 求静态区间逆序对. [题解] 我们对查询进行莫队操作,对于区间的删 ...
- BZOJ3289 Mato的文件管理(莫队+树状数组)
这个做法非常显然. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib& ...
- bzoj 3289: Mato的文件管理 莫队+树状数组
3289: Mato的文件管理 Time Limit: 40 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description Mato同学 ...
- bzoj3236 作业 莫队+树状数组
莫队+树状数组 #include<cstdio> #include<cstring> #include<iostream> #include<algorith ...
- 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 ...
- COGS.1822.[AHOI2013]作业(莫队 树状数组/分块)
题目链接: COGS.BZOJ3236 Upd: 树状数组实现的是单点加 区间求和,采用值域分块可以\(O(1)\)修改\(O(sqrt(n))\)查询.同BZOJ3809. 莫队为\(O(n^{1. ...
- 51nod 1290 Counting Diff Pairs | 莫队 树状数组
51nod 1290 Counting Diff Pairs | 莫队 树状数组 题面 一个长度为N的正整数数组A,给出一个数K以及Q个查询,每个查询包含2个数l和r,对于每个查询输出从A[i]到A[ ...
随机推荐
- 有关Java 5.0+ 并发包的探讨-2 section
有关Java 5.0+ 并发包的探讨-2 section 博客分类: Core Java JavathreadAccessF# 前面文章用直接使用Thread类的start方法来新启动一个线程,看起 ...
- Android之动画
Android的动画可以分为三种,View动画.帧动画.属性动画.View动画通过对场景里的对象不断做图像变化(平移.缩放.旋转.透明度)从而产生动画效果,它是一种渐进式动画,而且View动画支持自定 ...
- android传值
需求 OneActivity向TwoActivity传值name=hzs,然后TwoActivity向OneActivity传值sex=Y 第一步:OneActivity向TwoActivity传值n ...
- 关于前端本地压缩图片,兼容IOS/Android/PC且自动按需加载文件之lrz.bundle.js
一.介绍说明主要特点: ①在前端压缩好要上传的图片可以更快的发送给后端,因此也特别适合在移动设备上使用. ②兼容IOS/Android,修复了IOS/Android某些版本已知的BUG. ③按需加载文 ...
- JS中sessionstorage的getItem/setItem/removeItem/clear
function people(){ } var animal = { name: "cc", age:16, say:function(m1,m2){ alert("他 ...
- C++的find函数使用小技巧
一个小问题:原始字符串如CRYPT,FUNCTION,我要确定里面是否存在CRYPT,于是调用C++的find()函数,结果判断是不存在,怎么回事? 我的判断代码if(strUseFlagsTmp.F ...
- SOFA 源码分析 — 负载均衡和一致性 Hash
前言 SOFA 内置负载均衡,支持 5 种负载均衡算法,随机(默认算法),本地优先,轮询算法,一致性 hash,按权重负载轮询(不推荐,已被标注废弃). 一起看看他们的实现(重点还是一致性 hash) ...
- cxf webservice生成客户端代码及调用服务端遇到的问题
1. 从网上下载cxf开发的工具 apache-cxf-3.1.4.zip, 解压文件,找到apache-cxf-3.1.4\bin目录,里面包含一个wsdl2java文件 2. 设置环境变量 1. ...
- cocos2d-x高级学习
弱联网开发技术: libcurl 添加lib文件:libcurl_imp.lib pthreadVCE2.lib 添加头文件:#include"curl/curl.h" curl ...
- Nordic官网/Infocenter/Devzone/Github简介
1. Nordic官网及资料下载 Nordic官网主页:https://www.nordicsemi.com/,进入官网后,一般点击"Products"标签页,即进入Nordic产 ...