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 ...
随机推荐
- 记忆化搜索:POJ1579-Function Run Fun(最基础的记忆化搜索)
Function Run Fun Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14815 Accepted: 7659 Des ...
- 【PyTorch深度学习】学习笔记之PyTorch与深度学习
第1章 PyTorch与深度学习 深度学习的应用 接近人类水平的图像分类 接近人类水平的语音识别 机器翻译 自动驾驶汽车 Siri.Google语音和Alexa在最近几年更加准确 日本农民的黄瓜智能分 ...
- python使用@property @x.setter @x.deleter
@property可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式,但是有时候setter/deleter也是需要的. 1>只有@property表示只读. 2>同时 ...
- error LNK2001: unresolved external symbol __imp___time64
Q: vs2005 generate a static lib(libva.lib), used in vc++6.0, error LNK2001: unresolved external symb ...
- 【数据结构与算法】Fibonacci Sequence
学计算机的对 Fibonacci 都并不陌生,在课堂上一讲到递归几乎都会提到 Fibonacci 数列.不久前,我对 Fibonacci 产生了一些兴趣,就在这里把自己的想法给记录下来. 递推公式: ...
- Leetcode 546.移除盒子
移除盒子 给出一些不同颜色的盒子,盒子的颜色由数字表示,即不同的数字表示不同的颜色.你将经过若干轮操作去去掉盒子,直到所有的盒子都去掉为止.每一轮你可以移除具有相同颜色的连续 k 个盒子(k > ...
- C++ 编程笔记
图片有点大,请耐心下载!
- Python 字典值相加
#字典值相加 def union_dic(*objs): _keys = set(sum([obj.keys() for obj in objs],[])) _total = {} for _key ...
- ZigBee学习三 UART通信
ZigBee学习三 UART通信 在使用串口时,只需掌握ZigBee协议栈提供的串口操作相关的三个函数即可. uint8 HalUARTOpen(uint8 port,halUARTCfg_t *co ...
- [NOI2012][bzoj2879] 美食节 [费用流+动态加边]
题面 传送门 思路 先看看这道题 修车 仔细理解一下,这两道题是不是一样的? 这道题的不同之处 但是有一个区别:本题中每一种车有多个需求,但是这个好办,连边的时候容量涨成$p\lbrack i\rbr ...