题目链接 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(离散化+区间并集)的更多相关文章

  1. CodeChef:Chef and Problems(分块)

    CodeChef:Chef and Problems 题目大意 有一个长度为n的序列$a_1,a_2,……,a_n$,每次给出一个区间[l,r],求在区间内两个相等的数的最远距离($max(j-i,满 ...

  2. 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 ...

  3. CODECHEF Chef and Churus 解题报告

    [CODECHEF]Chef and Churus Description 有一个长度为\(n\)的数组\(A\),有\(n\)个函数,第\(i\)个函数的值为\(\sum_{j=l_i}^{r_i} ...

  4. POJ 2528 - Mayor's posters - [离散化+区间修改线段树]

    题目链接:http://poj.org/problem?id=2528 Time Limit: 1000MS Memory Limit: 65536K Description The citizens ...

  5. Mayor's posters(线段树+离散化+区间染色)

    题目链接:http://poj.org/problem?id=2528 题目: 题意:将n个区间进行染色(对于同一个区间,后一次染色会覆盖上一次的染色),问最后可见的颜色有多少种. 思路:由于区间长度 ...

  6. Codeforces - 915E 离散化区间覆盖

    我一直以来都错认为离散化就是换个映射,其实还需要在离散值两端加上相差为1的值才能真正离散 不然看一下test3就知道 不过这个离散姿势太暴力,以至于我1000ms时限跑出998ms(其实是太懒没有删重 ...

  7. HDU2883 kebab(最大流判断满流 + 离散化 + 区间化点)

    [题意]: 有一个烤箱,烤箱在一个时刻最多考M个肉串,N个顾客,每个顾客有属性s,n,e,t s是来的时间,n是想要的肉串数量,e是最晚离开的时间,t是烤的时间(几分熟). 顾客的烤肉可以分开烤,比如 ...

  8. 2019牛客暑期多校训练营(第七场)-E Find the median (线段树+离散化 区间为点)

    题目链接:https://ac.nowcoder.com/acm/contest/887/E 题意:给出L[i],R[i],每次添加L[i]...R[i],求出此时的中位数. 思路:因为添加的数范围为 ...

  9. codeforces 295E Yaroslav and Points (离线操作+离散化+区间合并)

    参考链接:http://blog.csdn.net/dyx404514/article/details/8817717 写的很详细,这里就不再赘述,附上我的代码. #include <iostr ...

随机推荐

  1. poj 3259 时光穿梭问题 bellman_ford算法

    题意:有n个空地,有m条双向大路,w条时光隧道单向路.问能否回到过去? 思路:判断是否有负环存在,如果有负环存在那么就可以一直小就可以回到过去了 创建源顶点 V到其他顶点的距离d 初始为INF d[1 ...

  2. Git add命令

    git add -A和 git add .   git add -u在功能上看似很相近,但还是存在一点差别 git add . :他会监控工作区的状态树,使用它会把工作时的所有变化提交到暂存区,包括文 ...

  3. 01-Flutter移动电商实战-项目学习记录

    一直想系统性的学习一下 Flutter,正好看到该课程<Flutter移动电商实战>的百度云资源,共 69 课时,由于怕自己坚持不下去(经常学着学着就不学了),故采用博客监督以记之. 1. ...

  4. loj2074 「JSOI2016」灯塔

    loj 题面错的--去bzoj上看吧qwq 观察到 \(\sqrt{|i-j|}\) 的取值只有 \(\sqrt{n}\) 级别个,然后就很显然了,rmq. #include <iostream ...

  5. 【Distinct Subsequences】cpp

    题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...

  6. 菜鸟之路——机器学习之KNN算法个人理解及Python实现

    KNN(K Nearest Neighbor) 还是先记几个关键公式 距离:一般用Euclidean distance   E(x,y)√∑(xi-yi)2 .名字这么高大上,就是初中学的两点间的距离 ...

  7. md5 加密算法和升级

    在这里插一小节加密的吧,使用openssl库进行加密. 使用MD5加密 我们以一个字符串为例,新建一个文件filename.txt,在文件内写入hello ,然后在Linux下可以使用命令md5sum ...

  8. 四则运算UI组结对作业报告

    Github提交地址: 小组成员:陈兆庭,陈昶金: 一.编程阶段 清明节开始接触和调研关于UI设计的方法.由于两人的各方面知识储备均不足,在各种东西C#.MFC.Qt中进行调查和讨论,最终因为网上说Q ...

  9. Unity 脚本<2>

    UnityEngine; using System.Collections; public class PlayerControl : MonoBehaviour { [HideInInspector ...

  10. PHP中create_function的用法总结

    在php中,函数create_function主要用来创建匿名函数,有时候匿名函数可以发挥它的作用. 1.测试一 测试一主要用来循环替换数组中多个值的<与>,我们用array_map加上c ...