九野的部落格,轉載請註明出處:http://blog.csdn.net/acmmmm/article/details/12070909
題意:
n多凸邊形 m刀 (把n切m刀,問切完後的圖形中 最多的邊數 是多少)
切a點-b點
資料保證切的刀不會相交
思路:
2點之間的剩餘點數就是邊數,
把a-b距離 近 排序
切完一刀就統計一下切出來的蛋糕的邊數,並捨棄
[a,b] 表示a,b 點間剩下的點數(就是邊數)
先計算[a,b]的點數, 然後刪除(a,b) 區間的點 (注意刪除的是(a,b) ,所以實際操作是 刪除[a,b] )
最後要特殊算下 剩下那塊的(因為那塊沒有切)
#include<iostream>#include<stdio.h>#include<string>#include<string.h>#include<algorithm>#include<set>#include <cstdio> #include <cstring> #include <iostream> #include <math.h> #include <queue> #define N 10100 #define M 2000100 #define inf64 0x7ffffff #define inf 1073741824 #define ll int #define L(x) x<<1 #define R(x) x<<1|1 #define Mid(x,y) (x+y)>>1 using namespace std; inline ll Min(ll a,ll b){return a>b?b:a;} inline ll Max(ll a,ll b){return a>b?a:b;} struct Point{int x,y,dis;}p[N];bool cmp(Point a,Point b){return a.dis<b.dis;}struct node{ int l,r; ll sum;}tree[N*4]; void pushup(int id){tree[id].sum = tree[R(id)].sum + tree[L(id)].sum;}void build(int l,int r,int id){tree[id].l = l, tree[id].r = r;tree[id].sum = r - l + 1;if(l == r)return ;int mid = Mid(l,r);build( l, mid, L(id));build( mid+1, r, R(id));}void updata(int l, int r,int id){if(l == tree[id].l && tree[id].r == r){ tree[id].sum = 0; return ;}int mid=Mid(tree[id].l, tree[id].r);if(r <= mid)updata(l, r, L(id));else if(mid < l) updata(l, r, R(id));else {updata(l, mid, L(id));updata(mid+1, r, R(id));}pushup(id);}int query(int l, int r, int id){if(tree[id].sum==0)return 0;if( tree[id].l == tree[id].r)return tree[id].sum;int mid=Mid(tree[id].l, tree[id].r);if(r <= mid)return query(l, r, L(id));if(mid < l )return query(l, r, R(id));return query(l, mid, L(id)) + query(mid+1, r, R(id));}int main(){int n, m, i,temp;while(~scanf("%d %d",&n,&m)){for(i=1;i<=m;i++){scanf("%d %d",&p[i].x,&p[i].y);if(p[i].x>p[i].y)temp=p[i].x, p[i].x=p[i].y, p[i].y=temp;p[i].dis=p[i].y-p[i].x;}sort(p+1, p+m+1, cmp);build(1,n,1);int ans=0;for(i=1;i<=m;i++){ans=Max(ans, query(p[i].x, p[i].y, 1));updata(p[i].x+1, p[i].y-1, 1);}ans=Max(ans, query(1,n,1));printf("%d\n",ans);}return 0;}/*6 31 51 41 3ans:3*/