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. sql server与C#中的字符串相等等效写法

    sql server两个字段相等判断默认不区分大小写,并且字符串进行Unicode规范化处理. 等效c#中的相等为s=="字符".ToLower().Normalize(Syste ...

  2. es6基础(2)--解构赋值

    //解构赋值 { let a,b,rest; [a,b]=[1,2]; console.log(a,b); } //数组解构赋值 { let a,b,rest; [a,b,...rest]=[1,2, ...

  3. 关于SqlServer2008小记(查询数据库连接数,强行干掉连接)

    查询连接数 select count(*) from master.dbo.sysprocesses 这条语句查出来的是所有连接到本机(或者连接到本服务器)的连接数,并非是某一个库的连接数. 查询连接 ...

  4. EasyuiDatagird绑定分页.NetMVC

    引入EasyUi所有需要的脚本,样式 <link href="/easyui/themes/default/easyui.css" rel="stylesheet& ...

  5. iOS oc 检测手机移动网络和wifi是否开启

    利用Reachability的的一个方法currentReachabilityStatus 获取枚举类型值判断是否为2即可,不是2就不是WiFi. enum {        // DDG Netwo ...

  6. nopcommerce 常用属性验证

    Decimal可空:[UIHint("DecimalNullable")] datetime可空: [UIHint("DateTimeNullable")] i ...

  7. hive随机采样

    hive> select * from account limit 10;OKaccount.accountname     account.accid   account.platid  ac ...

  8. bar

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  9. C# 中的 ConfigurationManager类引用方法

    c#添加了Configuration;后,竟然找不到 ConfigurationManager 这个类,后来才发现:虽然引用了using System.Configuration;这个包,但是还是不行 ...

  10. 在keil调用Notepad++

    先打开keil, 新建一个 取名为notepad 选择notepad++的安装路径 设置参数 保持后可以看多了notepad的选项 运行当前的文件在notepad++打开