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 ...
随机推荐
- 笔记-python-standard library-17.1 threading
笔记-python-standard library-17.1 threading 1. threading source code: Lib/threading.py 本模块构建高级别的线 ...
- TCP/IP网络编程之多线程服务端的实现(二)
线程存在的问题和临界区 上一章TCP/IP网络编程之多线程服务端的实现(一)的thread4.c中,我们发现多线程对同一变量进行加减,最后的结果居然不是我们预料之内的.其实,如果多执行几次程序,会发现 ...
- easyui 判断密码是否输入一致
1.首先要扩展validatebox,添加验证两次密码功能 $.extend($.fn.validatebox.defaults.rules, { eqPassword:{ validator:fun ...
- wap html5播放器和直播开发小结
此文已由作者吴家联授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 去年年中的时候,借着产品改版的机会,将之前的h5播放器好好整理重构了一番.之前的h5播放器较为简陋,有几个大 ...
- RSA进阶之两个N的公约数
适用场景: 给你两个n,n1和n2. 两个数都很大,不好分解. 但是这数刚好有质数公因子(试试欧几里得辗转相除跑完之后,就是不断地相除就可以了,4000多位也是很快的),那不就相当于间接的分解出q或者 ...
- SQL 基础语法详解
SQL 命令一般分为 DQL.DML.DDL DQL:数据查询语句,基本就是 SELECT 查询命令,用于数据查询 DML:Data Manipulation Language 的简称,即数据操纵语言 ...
- HDU5726 GCD
Give you a sequence of N(N≤100,000)N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000)a1,...,an( ...
- Windows下Git多账号ssh-key(复制自己用)
Windows下Git多账号配置,同一电脑多个ssh-key的管理 这一篇文章是对上一篇文章<Git-TortoiseGit完整配置流程>的拓展,所以需要对上一篇文章有所了解,当然直接往下 ...
- ValueStack 源码
/* * Copyright 2002-2007,2009 The Apache Software Foundation. * * Licensed under the Apache License, ...
- linux 相关知识
在mac 终端中可以直接访问ssh 命令:ssh root@127.0.0.* 批量删除文件: 当前目录下所有 *.html文件,除了index.html [root@whr ...