protagonist

Cake Robbery

Cake Robbery
Time Limit: 2000 msMemory Limit: 65536 KB

As usual, Alice finishes her delicious cake at noon. Unfortunately, the smell of cake beckoned hungry Bob, and he decided to rob one piece of cake.

The cake is a convex polygon with N edges. At the beginning, Bob cut it along the diagonals. After M cuts, Bob decided to rob the 'largest' piece of cake. Strangely, in Bob's opinion, the piece has the most number of edge is the biggest one.

Please help Bob to find the 'largest' piece.

Input

There are multiple test cases (about 20).

The first line of each test case contains two integer number NM (5 <= N <= 10000), indicating the number of point of the cake and the cut, respectively.

The following M lines contain two integer x, y (1 <= x, y <= N), denoting the index of the starting and ending cut point. (the index of points mark from 1 to Nclockwise.)

The input will guarantee that all of the cuts will not intersect inside the cake, but they may cross each other at the edge of cake, and Bob won't cut along the initial edge of the cake.

Output

Output the maximal size (most number of edges) of the piece which Bob will get.

Sample Input

7 2
3 6
7 2

Sample Output

4
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 2e5 + 50;

struct cake {
    int l, r;

    inline void get() {
        scanf("%d%d", &l, &r);
        if (l > r)swap(l, r);
    }

    bool operator<(const cake &cur) const {
        return r - l + 1 <= cur.r - cur.l + 1;
    }
} o[maxn];


struct tree {
    int l, r, cnt;
} e[maxn];

inline void build(int rt, int l, int r) {
    e[rt].l = l;
    e[rt].r = r;
    e[rt].cnt = r - l + 1;
    if (l == r)return;
    int mid = l + r >> 1;
    build(rt << 1, l, mid);
    build(rt << 1 | 1, mid + 1, r);
}

inline void update(int ql, int qr, int rt) {
    int l = e[rt].l;
    int r = e[rt].r;
    if (ql <= l && qr >= r) {
        e[rt].cnt = 0;
        return;
    }
    int mid = l + r >> 1;
    if (ql <= mid) {
        update(ql, qr, rt << 1);
    }
    if (qr > mid) {
        update(ql, qr, rt << 1 | 1);
    }
    e[rt].cnt = e[rt << 1].cnt + e[rt << 1 | 1].cnt;
}


int n, m;


int main() {
#ifndef ONLINE_JUDGE
    freopen("1.txt", "r", stdin);
#endif
    while (scanf("%d%d", &n, &m) != EOF) {
        for (register int i = 1; i <= m; ++i) {
            o[i].get();
        }
        sort(o + 1, o + 1 + m);
        build(1, 1, n);
        int res = 0, tot = e[1].cnt;
        for (register int i = 1; i <= m; ++i) {
            update(o[i].l + 1, o[i].r - 1, 1);
            res = max(res, tot - e[1].cnt + 2);
            tot = e[1].cnt;
        }
        res = max(res, e[1].cnt);
        printf("%d\n", res);
    }
    return 0;
}

 

posted @ 2019-09-08 19:29  czy-power  阅读(376)  评论(0编辑  收藏  举报