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[ ...
随机推荐
- poi excel 常用操作
基本 Workbook wb= new HSSFWorkbook(); Sheet sheet = wb.createSheet("sheetName"); Row row = s ...
- Validate Binary Search Tree(一定掌握的方法)
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- access登录窗口校验代码一
Private Sub login_Click()If IsNull(Me.username) ThenMsgBox "请输入用户名!", vbExclamationElseIf ...
- Application "org.eclipse.ui.ide.workbench" could not be found in the registry.问题的解决
今天升级Eclipse,升级完Restart,碰到启动不了让看日志,日志里主要错误信息即是Application "org.eclipse.ui.ide.workbench" co ...
- java面试题,附个人理解答案
一,面向对象的特征:1.抽象 包括数据抽象跟行为抽象,将对象共同的特征取出形成一个类2.继承 被继承类为基类/超类,继承类为子类/派生类3.封装 多次使用道德数据或方法,封装成类,方便多次重复调用4. ...
- Ocelot中文文档-请求聚合
Ocelot允许您指定聚合多个普通ReRoutes的Aggregate ReRoutes(聚合路由),并将其响应映射到一个对象中.一般用于当您有一个客户端向服务器发出多个请求,而这些请求可以合并成一个 ...
- form表单序列化为Jquery对象
<form id="DailyFinancial" > @*class="form-inline"*@ <div class="fo ...
- 关于react router 4 的小实践
详细代码栗子:https://github.com/wayaha/react-dom-CY clone然后 npm install npm start 分割线 1.这个项目使用create-react ...
- ASP.NET MVC中的路由IRouteConstraint方法应用实例
在如下代码的写法中: public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { ro ...
- hadoop is running beyond virtual memory limits问题解决
单机搭建了2.6.5的伪分布式集群,写了一个tf-idf计算程序,分词用的是结巴分词,使用standalone模式运行没有任何问题,切换到伪分布式模式运行一直报错: hadoop is running ...