Codechef Chef and Triangles(离散化+区间并集)
题目链接 Chef and Triangles
先排序,然后得到$m - 1$个区间:
$(a[2] - a[1], a[2] + a[1])$
$(a[3] - a[2], a[3] + a[2])$
$……$
$(a[n] - a[n - 1], a[n] + a[n - 1])$
对这些区间求交集 再和$[L, R]$求并集,最后的元素个数就是答案。
#include <bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i(a); i <= (b); ++i)
#define LL long long
#define INF 1 << 30 const int N = + ; struct node{ LL x, y;} c[N], q[N]; struct Node{
LL x; int y;
friend bool operator < (const Node &a, const Node &b){
return (a.x == b.x) ? a.y < b.y : a.x < b.x;
}
} p[N]; map <LL, int> mp;
LL a[N], ori[N], fp[N], l, r, x, y, ans;
int n, cnt, et, nx, ny, now; int f[N], h[N], d[N], ret, cnt_status, _min, _max; int main(){ scanf("%d%lld%lld", &n, &l, &r);
rep(i, , n) scanf("%lld", a + i);
sort(a + , a + n + ); cnt = ; et = ;
rep(i, , n - ){
c[++cnt].x = a[i + ] - a[i] + ;
p[++et].x = c[cnt].x; p[et].y = et;
c[cnt].y = a[i + ] + a[i] - ;
p[++et].x = c[cnt].y; p[et].y = et;
} rep(i, , et) ori[i] = p[i].x;
sort(p + , p + et + ); f[p[].y] = ; rep(i, , et) f[p[i].y] = p[i].x == p[i - ].x ? f[p[i - ].y] : f[p[i - ].y] + ; rep(i, , et) f[i] *= ; rep(i, , et){
mp[ori[i]] = f[i];
fp[f[i]] = ori[i];
} _min = INF;
_max = -_min; memset(h, , sizeof h);
rep(i, , cnt){
x = c[i].x, y = c[i].y;
nx = mp[x], ny = mp[y]; _min = min(_min, nx);
_max = max(_max, ny + ); ++h[nx], --h[ny + ];
}
now = ;
rep(i, _min, _max){
now += h[i];
d[i] = now;
} cnt_status = ;
ret = ;
rep(i, _min, _max){
if (cnt_status == && d[i]){
++ret;
q[ret].x = fp[i];
cnt_status = ;
} else
if (cnt_status == && d[i] == ){
q[ret].y = fp[i - ];
cnt_status = ;
}
} ans = ;
rep(i, , ret){
x = max(l, q[i].x), y = min(r, q[i].y);
if (x <= y) ans += y - x + ;
} printf("%lld\n", ans); return ; }
Codechef Chef and Triangles(离散化+区间并集)的更多相关文章
- CodeChef:Chef and Problems(分块)
CodeChef:Chef and Problems 题目大意 有一个长度为n的序列$a_1,a_2,……,a_n$,每次给出一个区间[l,r],求在区间内两个相等的数的最远距离($max(j-i,满 ...
- codechef Chef and The Right Triangles 题解
Chef and The Right Triangles The Chef is given a list of N triangles. Each triangle is identfied by ...
- CODECHEF Chef and Churus 解题报告
[CODECHEF]Chef and Churus Description 有一个长度为\(n\)的数组\(A\),有\(n\)个函数,第\(i\)个函数的值为\(\sum_{j=l_i}^{r_i} ...
- POJ 2528 - Mayor's posters - [离散化+区间修改线段树]
题目链接:http://poj.org/problem?id=2528 Time Limit: 1000MS Memory Limit: 65536K Description The citizens ...
- Mayor's posters(线段树+离散化+区间染色)
题目链接:http://poj.org/problem?id=2528 题目: 题意:将n个区间进行染色(对于同一个区间,后一次染色会覆盖上一次的染色),问最后可见的颜色有多少种. 思路:由于区间长度 ...
- Codeforces - 915E 离散化区间覆盖
我一直以来都错认为离散化就是换个映射,其实还需要在离散值两端加上相差为1的值才能真正离散 不然看一下test3就知道 不过这个离散姿势太暴力,以至于我1000ms时限跑出998ms(其实是太懒没有删重 ...
- HDU2883 kebab(最大流判断满流 + 离散化 + 区间化点)
[题意]: 有一个烤箱,烤箱在一个时刻最多考M个肉串,N个顾客,每个顾客有属性s,n,e,t s是来的时间,n是想要的肉串数量,e是最晚离开的时间,t是烤的时间(几分熟). 顾客的烤肉可以分开烤,比如 ...
- 2019牛客暑期多校训练营(第七场)-E Find the median (线段树+离散化 区间为点)
题目链接:https://ac.nowcoder.com/acm/contest/887/E 题意:给出L[i],R[i],每次添加L[i]...R[i],求出此时的中位数. 思路:因为添加的数范围为 ...
- codeforces 295E Yaroslav and Points (离线操作+离散化+区间合并)
参考链接:http://blog.csdn.net/dyx404514/article/details/8817717 写的很详细,这里就不再赘述,附上我的代码. #include <iostr ...
随机推荐
- 读《深入理解jvm虚拟机》之长期存活对象进入老年代,有感!!!!
关于这一段代码 有几个不是让人很理解的地方,我一一说来. 1.Desired survivor size 524288 bytes 关于这个512KB空间是怎么来的,JVM有这样一个参数: -XX:T ...
- 10,Scrapy简单入门及实例讲解
Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的, 也可以 ...
- 03014_EL技术
1.EL表达式概述 EL(Express Language)表达式可以嵌入在jsp页面内部,减少jsp脚本的编写,EL出现的目的是要替代jsp页面中脚本的编写. 2.EL从域中取出数据(EL最重要的作 ...
- spring整合mybatis详解
在上篇螃蟹已经说明spring注解的最经典配置,接下来开始整合mybatis,这样整个项目就相对完整了. 有关本实例的源码可以到 <spring MVC注解实例及说明文档> 下载. 如需转 ...
- IOS开发学习笔记037-九宫格代码实现
九宫格布局,用手机输入法时经常见到.先按3行3列写. 代码的实现主要是计算插入图片的位置. 每一张图片的位置和所在的行列密切相关.分析过程如下: 界面: 代码实现 1.把需要的图片资源添加进来 然后给 ...
- 引用其他头文件时出现这种错误,莫名其妙,error C2065: “ColorMatrix”: 未声明的标识符
今天做项目时,直接拷贝了另一个工程里的头文件和源文件,然后运行时就出现这种问题,莫名其妙,在原程序里运行一点问题就没有,但是在新工程里就是error. >e:\c++\button_fly2\b ...
- android 内存说明
MemoryInfo的Field如下 dalvikPrivateDirty: The private dirty pages used by dalvik. dalvikPss :The propor ...
- Python3 HTMLTestRunner自动化测试报告美化
# FileName : MyHTMLTestRunner.py # Author : wangyinghao # DateTime : 2019/1/9 21:04 # SoftWare : PyC ...
- 基于 <tx> 和 <aop> 命名空间的声明式事务管理
环境配置 项目使用SSH架构,现在要添加Spring事务管理功能,针对当前环境,只需要添加Spring 2.0 AOP类库即可.添加方法: 点击项目右键->Build Path->Add ...
- poj2488 A Knight's Journey裸dfs
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35868 Accepted: 12 ...