hdu-6701 Make Rounddog Happy
题目链接
Problem Description
Rounddog always has an array a1,a2,⋯,an in his right pocket, satisfying 1≤ai≤n.
A subarray is a non-empty subsegment of the original array. Rounddog defines a good subarray as a subsegment al,al+1,⋯,ar that all elements in it are different and max(al,al+1,…,ar)−(r−l+1)≤k.
Rounddog is not happy today. As his best friend, you want to find all good subarrays of a to make him happy. In this case, please calculate the total number of good subarrays of a.
Input
The input contains several test cases, and the first line contains a single integer T (1≤T≤20), the number of test cases.
The first line of each test case contains two integers n (1≤n≤300000) and k (1≤k≤300000).
The second line contains n integers, the i-th of which is ai (1≤ai≤n).
It is guaranteed that the sum of n over all test cases never exceeds 1000000.
Output
One integer for each test case, representing the number of subarrays Rounddog likes.
Sample Input
2
5 3
2 3 2 2 5
10 4
1 5 4 3 6 2 10 8 4 5
Sample Output
7
31
题意
给出一个数组a和k,问有多少对\(l,r\)满足\(max(al,al+1,…,ar)−(r−l+1)≤k\)
题解
用启发式分治的方法遍历每个最大值掌控的区间,记区间为\([l,r]\),最大值在mid位置上,如果左区间更小就遍历左区间,计算以左区间每个点为左端点的方案数,否则就遍历右区间,预处理一个数组pre[i]表示以i向右最多延伸到哪里,使i到pre[i]数字不重复,suf[i]表示i向左最多延伸到哪里,使得suf[i]到i数字不重复,这样就能O(1)计算以每个点为左端点的方案数了。总体复杂度\(O(n\log n)\)
代码
#include <bits/stdc++.h>
using namespace std;
const int mx = 3e5+5;
int a[mx], pre[mx], suf[mx];
int n, k;
bool vis[mx];
struct Node {
int v, pos;
}tree[mx<<2];
void pushUp(int rt) {
tree[rt].v = max(tree[rt<<1].v, tree[rt<<1|1].v);
tree[rt].pos = (tree[rt<<1].v > tree[rt<<1|1].v ? tree[rt<<1].pos : tree[rt<<1|1].pos);
}
void build(int l, int r, int rt) {
if (l >= r) {
tree[rt].v = a[r];
tree[rt].pos = r;
return;
}
int mid = (l + r) / 2;
build(l, mid, rt<<1);
build(mid+1, r, rt<<1|1);
pushUp(rt);
}
int query(int L, int R, int l, int r, int rt) {
if (L <= l && r <= R) return tree[rt].pos;
int mid = (l + r) / 2;
int pos1 = -1, pos2 = -1;
if (L <= mid) pos1 = query(L, R, l, mid, rt<<1);
if (mid < R) pos2 = query(L, R, mid+1, r, rt<<1|1);
if (pos1 == -1) return pos2;
else if (pos2 == -1) return pos1;
else return a[pos1] > a[pos2] ? pos1 : pos2;
}
void dfs(int l, int r, long long &ans) {
if (l > r) return;
int mid = query(l, r, 1, n, 1);
int len = max(1, a[mid]-k);
if (mid-l <= r-mid) {
for (int i = l; i <= mid; i++) {
int L = max(mid, i+len-1);
int R = min(pre[i], r);
ans += max(0, R-L+1);
}
} else {
for (int i = mid; i <= r; i++) {
int R = min(mid, i-len+1);
int L = max(suf[i], l);
ans += max(0, R-L+1);
}
}
dfs(l, mid-1, ans);
dfs(mid+1, r, ans);
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
build(1, n, 1);
int pos = 0;
for (int i = 1; i <= n; i++) {
while (pos < n && !vis[a[pos+1]]) {
pos++;
vis[a[pos]] = true;
}
pre[i] = pos;
vis[a[i]] = false;
}
pos = n+1;
for (int i = n; i >= 1; i--) {
while (pos > 1 && !vis[a[pos-1]]) {
pos--;
vis[a[pos]] = true;
}
suf[i] = pos;
vis[a[i]] = false;
}
long long ans = 0;
dfs(1, n, ans);
printf("%lld\n", ans);
}
return 0;
}
hdu-6701 Make Rounddog Happy的更多相关文章
- [2019杭电多校第十场][hdu6701]Make Rounddog Happy
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6701 题目大意为求满足 $max(a_{l},a_{l+1}\cdot \cdot \cdot a_{ ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
- hdu 4329
problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟 a. p(r)= R'/i rel(r)=(1||0) R ...
随机推荐
- Mac Android 配置环境变量
进入终端,输入以下命令: cd ~ touch .bash_profile //没有该文件的话新建一个 vi .bash_profile //vim 形式打开 输入内容jdk变量配置内容: expor ...
- linux下pip的安装
---恢复内容开始--- 1 输入apt-cache search wxpython 如果有返回信息 则输入 sudo apt-get install python-tools 2 否则 1.添加软件 ...
- KD-tree 专题「Hide and Seek · 巧克力王国」
Lockey的瞎理解 抄了一遍板子又水了俩题,感觉对KD-tree 稍稍理解了一点儿,唠叨一下(二维的KD-tree),如有错误请指出(Lockey 洗脸恭听) 普通平衡树维护的是一维的序列,但对于二 ...
- ASP.NET Core Web API 跨域(CORS) Cookie问题
身为一个Web API,处理来自跨域不同源的请求,是一件十分合理的事情. 先上已有的文章,快速复制粘贴,启用CORS: Microsoft:启用 ASP.NET Core 中的跨域请求 (CORS) ...
- ASP.NET Core MVC 之视图组件(View Component)
1.视图组件介绍 视图组件是 ASP.NET Core MVC 的新特性,类似于局部视图,但它更强大.视图组件不使用模型绑定,并且仅依赖于调用它时所提供的数据. 视图组件特点: 呈块状,而不是整个响应 ...
- spark学习(10)-RDD的介绍和常用算子
RDD(弹性分布式数据集,里面并不存储真正要计算的数据,你对RDD的操作,他会在Driver端转换成Task,下发到Executor计算分散在多台集群上的数据) RDD是一个代理,你对代理进行操作,他 ...
- Vsftp服务器配置文件详解
vsftp软件是我们常见的FTP服务器搭建软件,所有的配置都是基于vsftpd.conf这个配置文件的.vsftpd.conf里面主要包括安全配置,传输,用户还有权限等相关的选项.现在我们讲解下关于V ...
- SVN服务器更改ip地址客户端怎么设置
SVN 服务器 IP 地址修改后,客户端对服务器的连接可以采用以下的方法重定位: 1. 如果客户端工具是TortoiseSVN,直接在工作副本上右键,选择TortoiseSVN->relocat ...
- Spring1
一.Spring是什么?有什么用? Spring的适用环境是这样的,假设现在有一个类port,它将提供一个返回消息的功能,代码如下: public class port { private weibo ...
- 什么是HTML,HTML的简介,HTML结构
html:超文本标记语言(Hyper Text Markup Language) ==============基本结构================= <html><!--最外层为 ...