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 ...
随机推荐
- POJ:2185-Milking Grid(KMP找矩阵循环节)
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Description Every morning when they are milked, ...
- 团体程序设计天梯赛-练习集 L2-001 紧急救援 (25 分)
作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上.当其他城市有紧急求 ...
- 11、python中的函数(基础)
一.什么是函数? 在数学中,x2+2x2+3=10这样的叫方程. 而ax2+bx2+c=d这样的才叫函数.数学的函数中,abcd等待输入的未知量叫自变量,它需要我们自己去输入,而x这种待求得未知量叫因 ...
- 笔记-HTTP代理
笔记-HTTP代理 1. 基本原理 所谓代理,一个重要的目的是身份伪装,让被访问端无法正辨识访问者的身份. 那么下一个问题是服务器从哪些信息上来识别访问端的身份. 以php http服务器为 ...
- Android拨打电话不弹出系统拨号界面总结
我在网上搜了一下,解决这个问题,有两种方式: 1.反射调用系统底层方法,并获取系统权限 反射调用的代码如下: Class phoneFactoryClass = Class.forName(" ...
- 图学java基础篇之集合工具
两个工具类 java.utils下又两个集合相关_(准确来说其中一个是数组的)_的工具类:Arrays和Collections,其中提供了很多针对集合的操作,其中涵盖了一下几个方面: 拷贝.填充.反转 ...
- loj2046 「CQOI2016」路由表
大傻逼trie树,更傻逼的是我这都没独立想出来,以后要少看题解,多多思考 #include <algorithm> #include <iostream> #include & ...
- leetcode 【 Remove Duplicates from Sorted Array 】python 实现
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- Windows API 学习记录1
这里面的好多函数不能死记硬背,知道有这个函数就行,等用到了,再回来细看 一.窗口管理函数包括以下几类: 按钮函数(Button) Microsoft提供了对话框和控制来支持应用程序与用户之间的交互通讯 ...
- dib build ipa image Injection password
针对dib制作的deploy image,注入密码有两种方式: devuser/dynamic-login .对应 dib 添加密码,是通过 dynamic-login element 来完成的. 首 ...