Educational Codeforces Round 29 - E. Turn Off The TV (离散化+化线成点)

E. Turn Off The TV
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Luba needs your help again! Luba has n TV sets. She knows that i-th TV set will be working from moment of time li till moment ri, inclusive.

Luba wants to switch off one of TV sets in order to free the socket. Let's call some TV set redundant if after switching it off the number of integer moments of time when at least one of TV sets is working won't decrease. Luba will be very upset if she has to switch off a non-redundant TV set.

Help Luba by telling her the index of some redundant TV set. If there is no any, print -1.

Input

The first line contains one integer number n (1 ≤ n ≤ 2·105) — the number of TV sets.

Then n lines follow, each of them containing two integer numbers li, ri (0 ≤ li ≤ ri ≤ 109) denoting the working time of i-th TV set.

Output

If there is no any redundant TV set, print -1. Otherwise print the index of any redundant TV set (TV sets are indexed from 1 to n).

If there are multiple answers, print any of them.

Examples
input
3
1 3
4 6
1 7
output
1
input
2
0 10
0 10
output
1
input
3
1 2
3 4
6 8
output
-1
input
3
1 2
2 3
3 4
output
2
Note

Consider the first sample. Initially all integer moments of time such that at least one TV set is working are from the segment [1;7]. It's easy to see that this segment won't change if we switch off the first TV set (or the second one).

Note that in the fourth sample you can switch off the second TV set, since even without it all integer moments such that any of the TV sets is working denote the segment [1;4].


题意:

给你几个tv的l,r。让你找有么有去掉这个tv,这个区间还是有tv在播放。

求出任意一个即可,或者没有输出-1。

POINT:

离散化,离散化要加点。

化线成点。把线化成两个端点,cnt[l]++,cnt[r+1]--。

那么cnt前缀和就代表这一个点重复了几次。

用pre再来一个前缀和,一个点只存在一次的时候,+1.

那么区间和就代表这个区间有几个只存在一次的点。

如果是0,则可以去掉这个区间。

否则继续往下找。


#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include<algorithm>
using namespace std;
#define LL long long
const int maxn = 1000000;
int cnt[maxn];
int s[maxn];
struct node
{
    int l,r;
}len[maxn];
int main()
{
    int n;
    scanf("%d",&n);
    int num=0;
    for(int i=1;i<=n;i++){
        scanf("%d %d",&len[i].l,&len[i].r);
        s[++num]=len[i].l;
        s[++num]=len[i].r;
        s[++num]=len[i].l-1;
        s[++num]=len[i].r+1;
    }
    sort(s+1,s+1+num);
    int m=1;
    for(int i=2;i<=num;i++){
        if(s[i-1]!=s[i]) s[++m]=s[i];
    }
    for(int i=1;i<=n;i++){
        len[i].l=(int)(lower_bound(s+1,s+1+m,len[i].l)-s);
        len[i].r=(int)(lower_bound(s+1,s+1+m,len[i].r)-s);
        cnt[len[i].l]++;
        cnt[len[i].r+1]--;
    }
    int pre[maxn];
    for(int i=1;i<=m;i++){
        cnt[i]+=cnt[i-1];
    }
    for(int i=1;i<=m;i++){
        pre[i]+=pre[i-1]+(cnt[i]==1);
    }
    int ans=0;
    for(int i=1;i<=n;i++){
        int l=len[i].l;
        int r=len[i].r;
        int flag=pre[r]-pre[l-1];
        if(!flag){
            ans=i;
            break;
        }
    }
    if(ans) printf("%d\n",ans);
    else printf("-1\n");
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值