You have a long fence which consists of nn sections. Unfortunately, it is not painted, so you decided to hire qq painters to paint it. ii-th painter will paint all sections xx such that li≤x≤rili≤x≤ri.

Unfortunately, you are on a tight budget, so you may hire only q−2q−2 painters. Obviously, only painters you hire will do their work.

You want to maximize the number of painted sections if you choose q−2q−2 painters optimally. A section is considered painted if at least one painter paints it.

Input

The first line contains two integers nn and qq (3≤n,q≤50003≤n,q≤5000) — the number of sections and the number of painters availible for hire, respectively.

Then qq lines follow, each describing one of the painters: ii-th line contains two integers lili and riri (1≤li≤ri≤n1≤li≤ri≤n).

Output

Print one integer — maximum number of painted sections if you hire q−2q−2painters.

Examples

Input
7 5
1 4
4 5
5 6
6 7
3 5
Output
7
Input
4 3
1 1
2 2
3 4
Output
2
Input
4 4
1 1
2 2
2 3
3 4
Output
3
解题过程:
遍历一下每个栅栏有多少人会在这里刷漆,对于在漆工范围内的栅栏存储总量。用前缀和数组记录刷一次和刷两次的位置。
暴力寻找哪两个漆工去掉后会让栅栏不能刷漆,取最小值。用总量-最小值。大牛只需要分三种情况,在下天资愚钝,分了6种情况。
第一种:
.........................
.........................
第二种:
.......................
..............................
第三种:
.........................
..............
第四种:
.........................
..................
第五种:
.........................
...........................
第六种:
.............
............
上代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<vector>
#include<iostream>
#include<cstring>
#define inf 0x3f3f3f3f
using namespace std;
#define ll long long struct node
{
int l;
int r;
int idx;
};
int x,y;
node a[];
int t[];
int one[];
int two[];///前缀和数组 bool cmp(node p1,node p2)
{
if(p1.l==p2.l)
return p1.r<p2.r;
else return p1.l<p2.l;
} int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(a,,sizeof(a));
memset(t,,sizeof(t));
for(int i=;i<m;i++)
{
scanf("%d%d",&a[i].l,&a[i].r);
for(int j=a[i].l;j<=a[i].r;j++)
t[j]++;
}
int ans=,minn=inf;
for(int i=;i<=n;i++)
{
if(t[i])
{
ans++;
}
one[i]=one[i-]+(t[i]==);///前缀和赋值不能放括号里面
two[i]=two[i-]+(t[i]==);///前缀和需要连续赋值,如果if不成功则不赋值,造成中断
}
sort(a,a+m,cmp);
for(int i=;i<m;i++)///hello
{
for(int j=i+;j<m;j++)///下标j在i后面,j的l始终大于等于i的l
{
if( a[i].l==a[j].l && a[i].r==a[j].r)///
minn=min(minn,( two[ a[i].r ]-two[ a[i].l- ] ) );
else if(a[i].l==a[j].l && a[i].r<a[j].r)///
minn=min(minn,( two[ a[i].r ]-two[ a[i].l- ] ) + ( one[ a[j].r ]-one[ a[i].r ] ) );
else if( a[i].r>a[j].r )///
minn=min(minn,( one[ a[j].l- ]-one[ a[i].l- ] ) + ( two[ a[j].r ]-two[ a[j].l- ] ) + ( one[ a[i].r ]-one[ a[j].r ]) );
else if( a[i].r==a[j].r )///
minn=min(minn,( one[ a[j].l- ]-one[ a[i].l- ] ) + ( two[ a[j].r ]-two[ a[j].l- ] ) );
else if( a[i].r<a[j].r && a[i].r>=a[j].l )///
minn=min(minn,( one[ a[j].l- ]-one[ a[i].l- ] ) + ( two[ a[i].r ]-two[ a[j].l- ] ) + ( one[ a[j].r ]-one[ a[i].r ]) );
else if( a[i].r<a[j].l )///
minn=min(minn,( one[ a[i].r ]-one[ a[i].l- ] ) + ( one[ a[j].r ]-one[ a[j].l- ] ) );
}
}
printf("%d\n",ans-minn); }
return ;
}
 

CodeForces-1132C-Painting the Fence-(前缀和)的更多相关文章

  1. Codeforces 1132C - Painting the Fence - [前缀和优化]

    题目链接:https://codeforces.com/contest/1132/problem/C 题意: 栅栏有 $n$ 个节,有 $q$ 个人可以雇佣来涂栅栏,第 $i$ 个人可以涂第 $l_i ...

  2. Educational Codeforces Round 61 (Rated for Div. 2)-C. Painting the Fence 前缀和优化

    题意就是给出多个区间,要求去掉两个区间,使得剩下的区间覆盖范围最大. 当然比赛的时候还是没能做出来,不得不佩服大佬的各种姿势. 当时我想的是用线段树维护区间和,然后用单点判0,维护区间间断个数.然后打 ...

  3. codeforces 349B Color the Fence 贪心,思维

    1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...

  4. Codeforces 484E Sign on Fence(是持久的段树+二分法)

    题目链接:Codeforces 484E Sign on Fence 题目大意:给定给一个序列,每一个位置有一个值,表示高度,如今有若干查询,每次查询l,r,w,表示在区间l,r中, 连续最长长度大于 ...

  5. [luogu P2205] [USACO13JAN]画栅栏Painting the Fence

    [luogu P2205] [USACO13JAN]画栅栏Painting the Fence 题目描述 Farmer John has devised a brilliant method to p ...

  6. 洛谷 画栅栏Painting the Fence 解题报告

    P2205 画栅栏Painting the Fence 题目描述 \(Farmer\) \(John\) 想出了一个给牛棚旁的长围墙涂色的好方法.(为了简单起见,我们把围墙看做一维的数轴,每一个单位长 ...

  7. Painting The Fence(贪心+优先队列)

    Painting The Fence(贪心+优先队列) 题目大意:给 m 种数字,一共 n 个,从前往后填,相同的数字最多 k 个在一起,输出构造方案,没有则输出"-1". 解题思 ...

  8. 【Codeforces 1132C】Painting the Fence

    Codeforces 1132 C 题意:给一些区间\([l_i,r_i]\),从中删掉两个,求剩下的区间最多能够覆盖的格子数量. 思路:首先枚举第一个删掉的区间,然后我们可以通过差分来求出每个格子被 ...

  9. [Codeforces 448C]Painting Fence

    Description Bizon the Champion isn't just attentive, he also is very hardworking. Bizon the Champion ...

  10. codeforces C. Painting Fence

    http://codeforces.com/contest/448/problem/C 题意:给你n宽度为1,高度为ai的木板,然后用刷子刷颜色,可以横着刷.刷着刷,问最少刷多少次可以全部刷上颜色. ...

随机推荐

  1. Django之Models进阶操作(字段属性)

    字段属性详细介绍 一.字段 AutoField(Field) - int自增列,必须填入参数 primary_key=True BigAutoField(AutoField) - bigint自增列, ...

  2. java根据GPS(经纬度)获取地理位置

    package cn.antiy.weiqing.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONAr ...

  3. PowerDesigner 物理数据模型(PDM) 说明

    ref: https://blog.csdn.net/tianlesoftware/article/details/6874067 一.     PDM 介绍 物理数据模型(Physical Data ...

  4. CentOS7开机时的菜单选项及时间的修改

    一.在CentOS更新后,并不会自动删除旧内核.所以在启动选项中会有多个内核选项,可以手动使用以下命令删除多余的内核:(正常下,第一个选项正常启动,第二个选项急救模式启动(系统出项问题不能正常启动时使 ...

  5. tornado-5.1版本

    server.py python server.py执行 import tornado.ioloop import tornado.options import tornado.web from to ...

  6. TIDB资料收集

    https://github.com/pingcap/docs-cn https://github.com/pingcap/docs-cn/blob/master/op-guide/binary-de ...

  7. Java课程作业之动手动脑(五)

    1.请阅读并运行AboutException.java示例. import javax.swing.*; class AboutException { public static void main( ...

  8. Robot Operating System (ROS)学习笔记4---语音控制

    搭建环境:XMWare  Ubuntu14.04  ROS(indigo) 转载自古月居  转载连接:http://www.guyuehome.com/260 一.语音识别包 1.安装         ...

  9. TCP/UDP模型

    1网络接口层 MAC地址2网络互联层 IP地址3传输层 TCP/UDP端口号4应用层 应用层协议

  10. 41. timestamp 字段设值

    select TO_TIMESTAMP('2099-12-31 08:00:00.000000000', 'RR-MM-DD HH24:MI:SS.FF')  from dual;