1810 连续区间
基准时间限制:1.5 秒 空间限制:131072 KB 分值: 80
 
 
区间内所有元素排序后,任意相邻两个元素值差为1的区间称为“连续区间”

如:3,1,2是连续区间,但3,1,4不是连续区间
给出一个1~n的排列,求出有多少个连续区间
Input
一个数n(n<=1,000,000)
第二行n个数,表示一个1~n的排列
Output
一个数,表示有多少个连续区间
Input示例
5
2 1 5 3 4
Output示例
9
样例解释:
区间[1,1][2,2][3,3][4,4][5,5][1,2][4,5][3,4][1,5]为连续区间//[l,r]表示从第l个数到第r个数构成的区间
 
题解:
  分治计数
  先将序列划分为左右两边,只考虑左端点在左边部分,右端点在右边部分的答案
  也就是左边部分对右边部分答案的贡献
  可知
      Max{max(i),max(j)} - Min{min(i),min(j)} = j - i;
  共有4中互不影响的情况,其实这里分类讨论下就可以O(1)求出
#include <bits/stdc++.h>
inline long long read(){long long x=,f=;char ch=getchar();while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}return x*f;}
using namespace std;
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define MP make_pair
typedef long long LL;
typedef unsigned long long ULL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 1e6 + , M = , inf = 1e9; LL ans,mx[N],mx1[N],mn[N],mn1[N],vis[N * ];
int a[N],n; void solve(int ll,int rr) {
if(ll == rr) return ;
int last1 = -,last2 = inf;
for(int i = mid; i >= ll; --i)
mx[i] = max(last1,a[i]),last1 = mx[i],
mn[i] = min(last2,a[i]),last2 = mn[i];
last1 = -,last2 = inf;
for(int i = mid+; i <= rr; ++i)
mx1[i] = max(last1,a[i]),last1 = mx1[i],
mn1[i] = min(last2,a[i]),last2 = mn1[i]; for(int i = ll; i <= mid; ++i) {
int tmp = i + mx[i] - mn[i];
if(tmp > mid && tmp <= rr
&& mx[i] > mx1[tmp] && mn[i] < mn1[tmp]) ans+=;
} for(int i = mid+; i <= rr; ++i) {
int tmp = i - mx1[i] + mn1[i];
if(tmp >= ll && tmp <= mid
&& mx1[i] > mx[tmp] && mn1[i] < mn[tmp]) ans+=;
}
int l = mid+,r = mid;
for(int i = mid; i >= ll; --i) {
while(r < rr && mx[i] > mx1[r+]) ++r, vis[r+mn1[r]+n]++;
while(l <= r && mn[i] < mn1[l]) vis[l+mn1[l]+n]--,++l;
ans += vis[i+mx[i]+n];
}
while(l <= r) vis[l+mn1[l]+n]--,l++; l = mid, r = mid+;
for(int i = mid+; i <= rr; ++i) {
while(r > ll && mx1[i] > mx[r-]) r--,vis[r-mn[r]+n]++;
while(l >= r && mn[l] > mn1[i]) vis[l-mn[l]+n]--,l--;
ans += vis[i-mx1[i]+n];
}
while(l >= r) vis[l-mn[l]+n]--,l--;
solve(ll,mid),solve(mid+,rr);
}
int main() {
scanf("%d",&n);
for(int i = ; i <= n; ++i) a[i] = read();
solve(,n);
printf("%lld\n",ans+n);
return ;
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

51NOD 1810 连续区间 分治 区间计数的更多相关文章

  1. 51Nod 1810 连续区间

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1810 题目给出一个1~n的排列,问有多少连续区间.连续区间的定义为区间 ...

  2. 51NOD 1962 区间计数 单调栈+二分 / 线段树+扫描线

     区间计数   基准时间限制:1.5 秒 空间限制:262144 KB 分值: 80   两个数列 {An} , {Bn} ,请求出Ans, Ans定义如下: Ans:=Σni=1Σnj=i[max{ ...

  3. UVa 1640 The Counting Problem (数学,区间计数)

    题意:给定两个数m, n,求从 m 到 n 中0-9数字各出现了多少次. 析:看起来挺简单的,其实并不好做,因为有容易想乱了.主要思路应该是这样的,分区间计数,先从个位进行计,一步一步的计算过来.都从 ...

  4. 51nod 1962区间计数(单调栈加二分)

    题目要求是求出两个序列中处于相同位置区间并且最大值相同的区间个数,我们最直观的感受就是求出每个区间的最大值,这个可以O(N)的求,利用单调栈求出每个数作为最大值能够覆盖的区间. 然后我们可以在进行单调 ...

  5. 51nod 1962 区间计数(单调栈+二分)

    维护两个单调递减的栈,当i加进栈,位置x的数弹出的时候,在另一个栈中找到和这个数一样大的数,计算贡献(x-靠右左端点)*(i-x). #include<iostream> #include ...

  6. 51nod 1021 石子归并 - 区间dp(经典)

    题目地址:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1021 经典区间dp,dp[i][j] 表示将从 i 到 j 堆 ...

  7. 51Nod 1522 上下序列 —— 区间DP

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1522 区间DP,从大往小加: 新加入一种数有3种加法:全加左边,全 ...

  8. 类似区间计数的种类并查集两题--HDU 3038 & POJ 1733

    1.POJ 1733 Parity game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5744   Accepted: ...

  9. 51nod 第K大区间2(二分+树状数组)

    题目链接: 第K大区间2 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 160 定义一个长度为奇数的区间的值为其所包含的的元素的中位数.中位数_百度百科 现给出n个数,求将所有长度为 ...

随机推荐

  1. 转 Python常见数据结构整理

    http://www.cnblogs.com/jeffwongishandsome/archive/2012/08/05/2623660.html Python常见数据结构整理 Python中常见的数 ...

  2. hdu 4932 BestCoder Round #4 1002

    这题真是丧心病狂,引来今天的hack狂潮~ Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  3. HYSBZ 1026: windy数(数位DP)

    类型:数位DP题意:不含前导零且相邻两个数字之差至少为2的正整数被称为windy数.问[A,B]之间windy数的个数.(1 <= A <= B <= 2000000000 ) 思路 ...

  4. mysql利用sql脚本插入数据中文乱码

    将其中的 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;/*!40101 SET @OLD_CHARACTER_SE ...

  5. Cryptography I 学习笔记 --- 流密码

    1. 对于一次性密码本(one time pad),没有唯密文攻击(cypher text only attack),也就是说如果攻击者只能拿到密文,他什么也做不了 2. 完美密码:密钥长度大于密文长 ...

  6. 洛谷——P2196 挖地雷

    题目背景 NOIp1996提高组第三题 题目描述 在一个地图上有N个地窖(N<=20),每个地窖中埋有一定数量的地雷.同时,给出地窖之间的连接路径.当地窖及其连接的数据给出之后,某人可以从任一处 ...

  7. Codechef FNCS Chef and Churu

    Disciption Chef has recently learnt Function and Addition. He is too exited to teach this to his fri ...

  8. Failed to check the status of the service报错解决

    报这个错误是因为我的application_context.service.xml 文件里的的dubbo声明暴露口时的ref属性写错了. <dubbo:service interface=&qu ...

  9. The Process class relies on proc_open, which is not available on your PHP installation

    [Symfony\Component\Process\Exception\RuntimeException] The Process class relies on proc_open, which ...

  10. 【原创】关于jquery实现格式化时间

    //js格式化时间,参数jsonDate可以是后台数据 function jsonDateFormat(jsonDate) { try { var date = new Date(parseInt(j ...