VK Cup 2015 - Round 1 E. Rooks and Rectangles 線段樹 定點修改,區間最小值

來源:互聯網
上載者:User

標籤:

E. Rooks and Rectangles

Time Limit: 1 Sec  Memory Limit: 256 MB

題目串連http://codeforces.com/problemset/problem/524/E
DescriptionPolycarpus has a chessboard of size n × m, where k rooks are placed. Polycarpus hasn‘t yet invented the rules of the game he will play. However, he has already allocated q rectangular areas of special strategic importance on the board, they must be protected well. According to Polycarpus, a rectangular area of ??the board is well protected if all its vacant squares can be beaten by the rooks that stand on this area. The rooks on the rest of the board do not affect the area‘s defense. The position of the rooks is fixed and cannot be changed. We remind you that the the rook beats the squares located on the same vertical or horizontal line with it, if there are no other pieces between the square and the rook. Help Polycarpus determine whether all strategically important areas are protected.InputThe first line contains four integers n, m, k and q (1 ≤ n, m ≤ 100 000, 1 ≤ k, q ≤ 200 000) — the sizes of the board, the number of rooks and the number of strategically important sites. We will consider that the cells of the board are numbered by integers from 1 to n horizontally and from 1 to m vertically. Next k lines contain pairs of integers "x y", describing the positions of the rooks (1 ≤ x ≤ n, 1 ≤ y ≤ m). It is guaranteed that all the rooks are in distinct squares. Next q lines describe the strategically important areas as groups of four integers "x1 y1 x2 y2" (1 ≤ x1 ≤ x2 ≤ n, 1 ≤ y1 ≤ y2 ≤ m). The corresponding rectangle area consists of cells (x, y), for which x1 ≤ x ≤ x2, y1 ≤ y ≤ y2. Strategically important areas can intersect of coincide.
OutputPrint q lines. For each strategically important site print "YES" if it is well defended and "NO" otherwise.
Sample Input4 3 3 3
1 1
3 2
2 3
2 3 2 3
2 1 3 3
1 2 2 3Sample OutputYES
YES
NO
HINTPicture to the sample: For the last area the answer is "NO", because cell (1, 2) cannot be hit by a rook.

題意

給你,n個矩形,判斷這個n個矩形是否被在矩形內的車全部覆蓋

題解:

維護兩個資料結構分別表示前i行裡第幾列是否被覆蓋到和前i列裡第j行是否被覆蓋到 然後詢問的時候就相當於查詢第i1到第i2行這個區間的線段樹中第j1到第j2列中最小的覆蓋數是否為0,如果是就說明未被覆蓋

思想比較麻煩,但是寫起來特別快 

代碼:

 

//qscqesze#include <cstdio>#include <cmath>#include <cstring>#include <ctime>#include <iostream>#include <algorithm>#include <set>#include <vector>#include <sstream>#include <queue>#include <typeinfo>#include <fstream>#include <map>typedef long long ll;using namespace std;//freopen("D.in","r",stdin);//freopen("D.out","w",stdout);#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)#define maxn 500001#define mod 10007#define eps 1e-9//const int inf=0x7fffffff;   //無限大const int inf=0x3f3f3f3f;/*inline ll read(){    int x=0,f=1;char ch=getchar();    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}    return x*f;}int buf[10];inline void write(int i) {  int p = 0;if(i == 0) p++;  else while(i) {buf[p++] = i % 10;i /= 10;}  for(int j = p-1; j >=0; j--) putchar(‘0‘ + buf[j]);  printf("\n");}*///**************************************************************************************int a[maxn],n,m,k,q;;struct node{    int x,y;};node p[maxn];struct pp{    int x1,y1,x2,y2,id;};pp que[maxn];void updata(int xx, int L, int R, int x, int val){    if(L==R)    {        a[xx]=val;        return;    }    int M=(L+R)>>1;    if(x <= M)        updata(xx*2,L,M,x,val);    else        updata(xx*2+1,M+1,R,x,val);    a[xx]=min(a[xx*2], a[xx*2+1]);}int query(int x, int L, int R, int l, int r){    if(l<=L&&R<=r)        return a[x];    int M=(L+R)>>1;    if(r<=M)        return query(x*2,L,M,l,r);    else if(l>M)        return query(x*2+1,M+1,R,l,r);    else        return min(query(x*2,L,M,l,r), query(x*2+1,M+1,R,l,r));}bool cmp(node x,node y){    return x.x<y.x;}bool cmp1(pp x,pp y){    return x.x2<y.x2;}int ans[maxn];void solve(){    memset(a,0,sizeof(a));    int pic=0;    for(int i=0;i<q;i++)    {        while(pic<k&&p[pic].x<=que[i].x2)        {            updata(1,1,m,p[pic].y,p[pic].x);            pic++;        }        if(query(1,1,m,que[i].y1,que[i].y2)>=que[i].x1)            ans[que[i].id]=1;    }}void change(){    swap(n,m);    for(int i=0;i<k;i++)        swap(p[i].x,p[i].y);    sort(p,p+k,cmp);    for(int i=0;i<q;i++)    {        swap(que[i].x1,que[i].y1);        swap(que[i].x2,que[i].y2);    }    sort(que,que+q,cmp1);}int main(){    scanf("%d%d%d%d",&n,&m,&k,&q);    for(int i=0;i<k;i++)        scanf("%d%d",&p[i].x,&p[i].y);    sort(p,p+k,cmp);    for(int i=0;i<q;i++)    {        scanf("%d%d%d%d",&que[i].x1,&que[i].y1,&que[i].x2,&que[i].y2);        que[i].id=i;    }    sort(que,que+q,cmp1);    solve();    change();    solve();    for(int i=0;i<q;i++)    {        if(ans[i])            puts("YES");        else            puts("NO");    }}

 

VK Cup 2015 - Round 1 E. Rooks and Rectangles 線段樹 定點修改,區間最小值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.