Make Rounddog Happy(2019年杭电多校第十场1011+HDU6701+启发式分治)
题目链接
题意
求有多少个子区间满足\(a_l,a_{l+1},\dots,a_r\)均不相同且\(max(a_l,a_{l+1},\dots,a_r)-(r-l+1)\leq K\)。
思路
听说是启发式分治然后就去学了下如何套板子,赛场上写搓了本地过不了样例,赛后改过来了。
启发式分治在本题的思路貌似就是在处理\([l,r]\)时找到区间最大值的位置\(mid\),然后看左半部分区间长度短还是右半部分短,然后暴力统计短的那部分的贡献。
首先预处理出以\(i\)为左端点,区间内没有相同数的右端点\(R[i]\)和以\(i\)为右端点,区间内没有相同数的左端点\(L[i]\),再用\(st\)表处理出区间最大值的位置。
在分治时由于我们已知最大值位置在哪,然后算贡献时(拿左半部分为例子)枚举不等式中的\(l\),然后移项计算出满足题意的最近右端点\(r\geq a[mid]-K+l-1\),然后用以\(l\)为左端点区间内没有相同数的右端点\(R[l]\)(满足题意的最远右端点)来减去这个值就是这个左端点的贡献,复杂度为\(O(nlog(n))\)。
代码
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define lson (rt<<1),L,mid
#define rson (rt<<1|1),mid + 1,R
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("/home/dillonh/CLionProjects/Dillonh/in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)
const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 300000 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
LL ans;
int t, n, K;
int a[maxn], L[maxn], R[maxn], vis[maxn], dp[maxn][20], pos[maxn][20];
void init() {
for(int j = 1; j < 20; ++j) {
if(1<<(j-1) > n) break;
for(int i = 1; i + (1<<j) - 1 <= n; ++i) {
if(dp[i][j-1] >= dp[i+(1<<(j-1))][j-1]) {
dp[i][j] = dp[i][j-1];
pos[i][j] = pos[i][j-1];
} else {
dp[i][j] = dp[i+(1<<(j-1))][j-1];
pos[i][j] = pos[i+(1<<j-1)][j-1];
}
}
}
}
int query(int l, int r) {
int k = log(r - l + 1) / log(2);
if(dp[l][k] >= dp[r-(1<<k)+1][k]) return pos[l][k];
else return pos[r-(1<<k)+1][k];
}
void solve(int l, int r) {
if(l > r) return;
int mid = query(l, r);
if(r - mid > mid - l) {
for(int i = l; i <= mid; ++i) {
int rs = a[mid] - K + i - 1;
rs = max(rs, mid);
int dd = min(r, R[i]);
if(rs > dd) continue;
ans += dd - rs + 1;
}
} else {
for(int i = mid; i <= r; ++i) {
int ls = K - a[mid] + i + 1;
ls = min(ls, mid);
int dd = max(l, L[i]);
if(ls < dd) continue;
ans += ls - dd + 1;
}
}
solve(l, mid - 1), solve(mid + 1, r);
}
int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif
scanf("%d", &t);
while(t--) {
scanf("%d%d", &n, &K);
for(int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
dp[i][0] = a[i];
pos[i][0] = i;
vis[i] = 0;
}
int r = 2;
vis[a[1]] = 1;
for(int i = 1; i <= n; ++i) {
while(r <= n && !vis[a[r]]) {
vis[a[r]] = 1;
++r;
}
vis[a[i]] = 0;
R[i] = r - 1;
}
vis[a[n]] = 1;
int l = n - 1;
for(int i = n; i >= 1; --i) {
while(l >= 1 && !vis[a[l]]) {
vis[a[l]] = 1;
--l;
}
vis[a[i]] = 0;
L[i] = l + 1;
}
init();
ans = 0;
solve(1, n);
printf("%lld\n", ans);
}
return 0;
}
版权声明:本文允许转载,转载时请注明原博客链接,谢谢~