Codeforces 526F Pudding Monsters
先把题目抽象一下:
有一个静态的数组,求有多少个区间[i,j]满足:j-i==max{ai,...,aj}-min{ai,...,aj}
也就是要求max-min+i-j==0的区间数
所以肿么做呢?
首先枚举i(这里倒着做,比较好理解),维护以i为开头的所有区间
相当于每次要在一坨区间的最前面同时加一个元素(并且增加一个仅含有ai的区间[i,i])
然后很惊喜的发现实际上对于这一坨区间的权值(max-min+i-j)只有以下几个操作:
1.同时-1,因为i减小了1
2.改max(这个一定只有有限段进行区间加减的操作)
3.改min(同理)
维护最大最小两个单调队列,每个元素(注意不是每次)出队的同时把它本来“管辖”的区域区间修改一下
这样就只需要一个兹磁区间修改兹磁查区间最小值及其出现次数的线段树即可
果断标记永久化(好写好想常数还小)
#include <bits/stdc++.h>
#define mid (l+r>>1)
using namespace std;
long long dep,ret,n,p,debug;
int Min[],flag[],num[];
int posa[],posb[],a[],b[],s[];
void add(int now,int l,int r,int x,int y,long long z)
{
if(l==x && r==y)
{
Min[now]+=z;
flag[now]+=z;
return;
}
if(x<=mid) add(now<<,l,mid,x,min(y,mid),z);
if(y>mid) add(now<<|,mid+,r,max(x,mid+),y,z);
if(Min[now<<]==Min[now<<|])
Min[now]=Min[now<<]+flag[now],num[now]=num[now<<]+num[now<<|];
else
{
int t;
if(Min[now<<]<Min[now<<|]) t=now<<;
else t=now<<|;
Min[now]=Min[t]+flag[now];
num[now]=num[t];
}
}
void query(int now,int l,int r,int x,int y)
{
if(l==x && r==y)
{
if(Min[now]+dep==)
ret+=num[now];
return;
}
dep+=flag[now];
if(x<=mid) query(now<<,l,mid,x,min(y,mid));
if(y>mid) query(now<<|,mid+,r,max(x,mid+),y);
}
void build(int now,int l,int r)
{
if(l==r)
{
Min[now]=;
num[now]=;
return;
}
build(now<<,l,mid);
build(now<<|,mid+,r);
Min[now]=;
num[now]=r-l+;
}
int main()
{
scanf("%d",&n);
build(,,n);
for(int i=;i<=n;i++)
scanf("%d",&p),scanf("%d",&s[p]);
int la=,lb=;
posa[]=n+;posb[]=n+;
for(int i=n;i>=;i--)
{
while(la && a[la]<=s[i])
{
add(,,n,posa[la],posa[la-]-,s[i]-a[la]);
--la;
}
while(lb && b[lb]>=s[i])
{
add(,,n,posb[lb],posb[lb-]-,b[lb]-s[i]);
--lb;
}
++la;++lb;
a[la]=s[i];posa[la]=i;
b[lb]=s[i];posb[lb]=i;
dep=;
query(,,n,i,n);
add(,,n,i,n,-);
}
printf("%lld\n",ret);
return ;
}
Codeforces 526F Pudding Monsters的更多相关文章
- Codeforces 526F Pudding Monsters - CDQ分治 - 桶排序
In this problem you will meet the simplified model of game Pudding Monsters. An important process in ...
- 奇袭 CodeForces 526F Pudding Monsters 题解
考场上没有认真审题,没有看到该题目的特殊之处: 保证每一行和每一列都恰有一只军队,即每一个Xi和每一个Yi都是不一样 的. 于是无论如何也想不到复杂度小于$O(n^3)$的算法, 只好打一个二维前缀和 ...
- Codeforces 436D - Pudding Monsters(dp)
Codeforces 题目传送门 & 洛谷题目传送门 u1s1 这题数据范围有点迷惑啊--乍一看 \(\mathcal O(nm)\) 过不去,还以为是正解是 \(\mathcal O(n+m ...
- Codeforces 436D Pudding Monsters
题意简述 开始有无限长的一段格子,有n个格子种有布丁怪兽,一开始连续的布丁怪兽算一个布丁怪兽. 每回合你可以将一个布丁怪兽向左或右移动,他会在碰到第一个布丁怪兽时停下,并与其合并. 有m个特殊格子,询 ...
- 【CF526F】Pudding Monsters cdq分治
[CF526F]Pudding Monsters 题意:给你一个排列$p_i$,问你有对少个区间的值域段是连续的. $n\le 3\times 10^5$ 题解:bzoj3745 Norma 的弱化版 ...
- [Codeforces526F]Pudding Monsters 分治
F. Pudding Monsters time limit per test 2 seconds memory limit per test 256 megabytes In this proble ...
- CodeForces526F:Pudding Monsters (分治)
In this problem you will meet the simplified model of game Pudding Monsters. An important process in ...
- CF526F Pudding Monsters
CF526F Pudding Monsters 题目大意:给出一个\(n* n\)的棋盘,其中有\(n\)个格子包含棋子. 每行每列恰有一个棋子. 求\(k*k\)的恰好包含\(k\)枚棋子的子矩形个 ...
- 「CF526F」 Pudding Monsters
CF526F Pudding Monsters 传送门 模型转换:对于一个 \(n\times n\) 的棋盘,若每行每列仅有一个棋子,令 \(a_x=y\),则 \(a\) 为一个排列. 转换成排列 ...
随机推荐
- ImageIO 操作图片
/** * 读取本地图片到另一个本地文件夹 * @throws IOException */ public void copeImageToOtherFolder() throws IOExcepti ...
- 深入浅出剖析C语言函数指针与回调函数(一)【转】
本文转载自:http://blog.csdn.net/morixinguan/article/details/65494239 关于静态库和动态库的使用和制作方法. http://blog.csdn. ...
- 万亿级日志与行为数据存储查询技术剖析——Hbase系预聚合方案、Dremel系parquet列存储、预聚合系、Lucene系
转自:http://www.infoq.com/cn/articles/trillion-log-and-data-storage-query-techniques?utm_source=infoq& ...
- C#继承与多态
继承:在程序中,如果一个类A:类B,这种机制就是继承. 子类可以继承父类的所有内容(成员)吗? 解析: 1.私有成员(属性和方法) 2.构造函数 3.final修饰过的方法,子类不能进行重写 //SE ...
- SDOI2017 Round1 Day1 题解
不知道有几个AK的,除了出题人SB搬了个BZOJ3779以外,应该没什么因素阻碍AK吧.要是SCOI考这套题多好. BZOJ4816 数字表格 SB反演,推出答案为$\prod_{i=1}^nf^{\ ...
- DBCPTool
dbcp读取配置文件的方式: 1. 导入3个包:commons-dbcp-... .jar(数据源) commons-collections-.....jar(集合) commons-pool... ...
- IIS 部署 SSAS
转自:http://blog.csdn.net/jinjazz/article/details/4058368 1.首先到分析服务器的SQLServer安装目录中找到如下目录和文件 2.然后为IIS建 ...
- 最新sublimetext3080注册
----- BEGIN LICENSE -----K-20Single User LicenseEA7E-9401293A099EC1 C0B5C7C5 33EBF0CF BE82FE3BEAC216 ...
- 3-C++程序的结构1.4
共享数据的保护 对于既需要共享.又需要防止改变的数据应该声明为常量. 1.常引用 如果在声明引用时用const修饰,被声明的引用就是常引用.常引用所引用的对象不能被更新.声明形式如下: const 类 ...
- CodeForces - 767C Garland 树的遍历
C. Garland time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...