In this problem you will meet the simplified model of game Pudding Monsters.

An important process in developing any game is creating levels. A game field in Pudding Monsters is an n × n rectangular grid, n of its cells contain monsters and some other cells contain game objects. The gameplay is about moving the monsters around the field. When two monsters are touching each other, they glue together into a single big one (as they are from pudding, remember?).

Statistics showed that the most interesting maps appear if initially each row and each column contains exactly one monster and the rest of map specifics is set up by the correct positioning of the other game objects.

A technique that's widely used to make the development process more efficient is reusing the available resources. For example, if there is a large n × n map, you can choose in it a smaller k × k square part, containing exactly k monsters and suggest it as a simplified version of the original map.

You wonder how many ways there are to choose in the initial map a k × k (1 ≤ k ≤ n) square fragment, containing exactly k pudding monsters. Calculate this number.

Input

The first line contains a single integer n (1 ≤ n ≤ 3 × 105) — the size of the initial field.

Next n lines contain the coordinates of the cells initially containing monsters. The i-th of the next lines contains two numbers ri, ci (1 ≤ ri, ci ≤ n) — the row number and the column number of the cell that initially contains the i-th monster.

It is guaranteed that all ri are distinct numbers and all ci are distinct numbers.

Output

Print the number of distinct square fragments of the original field that can form a new map.

Examples

Input
5
1 1
4 3
3 2
2 4
5 5
Output
10

题意:给定N*N的棋盘,以及N个棋子放置情况,保证每一行,每一列只有一个棋子。问现在有多少个子正方形,满足每一行每一列都有一个棋子。

思路:以x坐标为第一关键字,转化为一维数组,如样例(1,4,2,3,5),然后问题就成了有多少个练习子区间[i,j],满足i-j=max-min,棋子max和min是区间最大和最小值。 显然分治可以搞,问题转化为多个子问题:求跨过Mid的区间,满足i-j=max-min。

如何线性地解决子问题:由于max和min都是单调的,我们枚举i或者j,然后验证另外一个是否在当前子区间区间里,同时满足max和min的关系,累加答案。

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn=3e5+;
int mx[maxn],mn[maxn],sum[maxn<<];
int a[maxn],N; ll ans;
void solve(int L,int R)
{
if(L==R){ ans++; return ;}
int Mid=(L+R)/;
solve(L,Mid); solve(Mid+,R);
mx[Mid]=mn[Mid]=a[Mid]; mx[Mid+]=mn[Mid+]=a[Mid+];
for(int i=Mid-;i>=L;i--) mx[i]=max(mx[i+],a[i]);//预处理
for(int i=Mid-;i>=L;i--) mn[i]=min(mn[i+],a[i]);
for(int i=Mid+;i<=R;i++) mx[i]=max(mx[i-],a[i]);
for(int i=Mid+;i<=R;i++) mn[i]=min(mn[i-],a[i]); for(int i=Mid;i>=L;i--){ //都在左测
int j=mx[i]-mn[i]+i;
if(j<=R&&j>Mid&&mx[j]<mx[i]&&mn[j]>mn[i]) ans++;
}
for(int i=Mid+;i<=R;i++){ //都在右侧
int j=i-mx[i]+mn[i];
if(j<=Mid&&j>=L&&mx[j]<mx[i]&&mn[j]>mn[i]) ans++;
}
int j=Mid+,k=Mid+;
for(int i=Mid;i>=L;i--){ //左小右大
while(j<=R&&mn[j]>mn[i]) sum[mx[j]-j+N]++,j++;
while(k< j&&mx[k]<mx[i]) sum[mx[k]-k+N]--,k++;
ans+=(ll)sum[mn[i]-i+N];
}
while(k<j) sum[mx[k]-k+N]--,k++;
j=Mid,k=Mid;
for(int i=Mid+;i<=R;i++){ //左大右小
while(j>=L&&mn[j]>mn[i]) sum[mx[j]+j]++,j--;
while(k> j&&mx[k]<mx[i]) sum[mx[k]+k]--,k--;
ans+=(ll)sum[mn[i]+i];
}
while(k>j) sum[mx[k]+k]--,k--;
}
int main()
{
int x,y,i,j;
scanf("%d",&N);
for(i=;i<=N;i++) scanf("%d%d",&x,&y) ,a[x]=y;
solve(,N);
printf("%lld\n",ans);
return ;
}

CodeForces526F:Pudding Monsters (分治)的更多相关文章

  1. [Codeforces526F]Pudding Monsters 分治

    F. Pudding Monsters time limit per test 2 seconds memory limit per test 256 megabytes In this proble ...

  2. 【CF526F】Pudding Monsters cdq分治

    [CF526F]Pudding Monsters 题意:给你一个排列$p_i$,问你有对少个区间的值域段是连续的. $n\le 3\times 10^5$ 题解:bzoj3745 Norma 的弱化版 ...

  3. Codeforces 526F Pudding Monsters - CDQ分治 - 桶排序

    In this problem you will meet the simplified model of game Pudding Monsters. An important process in ...

  4. CF526F Pudding Monsters

    CF526F Pudding Monsters 题目大意:给出一个\(n* n\)的棋盘,其中有\(n\)个格子包含棋子. 每行每列恰有一个棋子. 求\(k*k\)的恰好包含\(k\)枚棋子的子矩形个 ...

  5. 「CF526F」 Pudding Monsters

    CF526F Pudding Monsters 传送门 模型转换:对于一个 \(n\times n\) 的棋盘,若每行每列仅有一个棋子,令 \(a_x=y\),则 \(a\) 为一个排列. 转换成排列 ...

  6. Pudding Monsters CodeForces - 526F (分治, 双指针)

    大意: n*n棋盘, n个点有怪兽, 求有多少边长为k的正方形内恰好有k只怪兽, 输出k=1,...,n时的答案和. 等价于给定n排列, 对于任意一个长为$k$的区间, 若最大值最小值的差恰好为k, ...

  7. [Codeforce526F]:Pudding Monsters(分治)

    题目传送门 题目描述 由于各种原因,桐人现在被困在Under World(以下简称UW)中,而UW马上要迎来最终的压力测试——魔界入侵.唯一一个神一般存在的Administrator被消灭了,靠原本的 ...

  8. 奇袭 CodeForces 526F Pudding Monsters 题解

    考场上没有认真审题,没有看到该题目的特殊之处: 保证每一行和每一列都恰有一只军队,即每一个Xi和每一个Yi都是不一样 的. 于是无论如何也想不到复杂度小于$O(n^3)$的算法, 只好打一个二维前缀和 ...

  9. 【CF526F】Pudding Monsters

    题意: 给你一个排列pi,问你有对少个区间的值域段是连续的. n≤3e5 题解: bzoj3745

随机推荐

  1. Android动画中Interpolator 详解和演示

    遇到一个项目需求,想让动画变得更活泼一点,于是想到了动画属性中的Interpolator,写了基本例子测试一下Android提供给我们现成的加速器的效果: 效果 代码中方法 xml中属性 越来越快 A ...

  2. Samp免流软件以及地铁跑酷的自校验分析

    [文章标题]:Samp免流软件以及地铁跑酷的自校验分析 [文章作者]: Ericky [作者博客]: http://blog.csdn.net/hk9259 [下载地址]: 自行百度 [保护方式]: ...

  3. 如何让你的服务屏蔽Shodan扫描

    1. 前言 在互联网中,充斥着各种各样的网络设备,shodan等搜索引擎提供给了我们一个接口,让我们可以在输入一些过滤条件就可以检索出网络中相关的设备. 对于我们的一些可能有脆弱性或者比较隐私的服务, ...

  4. 基于GPU加速的三维空间分析【转】

    基于GPU加速的三维空间分析 标签:supermap地理信息系统gisit 文:李凯 随着三维GIS 的快速发展和应用普及,三维空间分析技术以其应用中的实用性成为当前GIS技术研究的热点领域.面对日益 ...

  5. myBatis-plus异常提示For input string: "{0=null}"

    异常信息 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.Per ...

  6. 安装惠普M1136打印机一直处于“新设备已连接”状态怎么办?

    百度的答案是从控制面板的添加打印机入手,我试了遇到找不到设备的问题. 其实问题的原因是在安装驱动时一直把打印机到电脑的USB插着.我的解决方案是: 1.点击M1130MFP_M1210MFP开始安装, ...

  7. js动态函数

    最近项目中使用百度模板引擎baiduTemplate.js,使用动态函数解析模板中代码. 通过new Function([arg1,arg2,...,argN,]functionBody)方式实现动态 ...

  8. 关于ASP.NET MVC中Response.Redirect和RedirectToAction的BUG (跳转后继续执行后面代码而不结束进程)以及处理方法

    关于ASP.NET MVC中Response.Redirect和RedirectToAction的BUG (跳转后继续执行后面代码而不结束进程)以及处理方法   在传统的ASP.NET中,使用Resp ...

  9. [LeetCode]Insert Interval 考虑多种情况

    写太复杂了. 思想:确定带插入区间的每一个边界位于给定区间中的哪个位置,共同拥有5种情况 -1 |(0)_1_(2)|  (3) 当中.0,1,2这三种情况是一样的. 确定每一个带插入区间的两个边界分 ...

  10. leetCode(51):Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...