VK Cup 2015 - Round 1 E. Rooks and Rectangles 线段树 定点修改,区间最小值

E. Rooks and Rectangles

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://codeforces.com/problemset/problem/524/E

Description

Polycarpus 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.

Input

The 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.

Output

Print q lines. For each strategically important site print "YES" if it is well defended and "NO" otherwise.

Sample Input

4 3 3 3
1 1
3 2
2 3
2 3 2 3
2 1 3 3
1 2 2 3

Sample Output

YES
YES
NO

HINT

Picture 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");
    }
}
时间: 2024-10-17 17:29:11

VK Cup 2015 - Round 1 E. Rooks and Rectangles 线段树 定点修改,区间最小值的相关文章

VK Cup 2015 - Round 2 B. Work Group

题解: 树形dp 首先开始做时我以为只要贪心选取就可以了... 后来发现根节点可以不用选,而选和为奇数的满足条件的子节点. 所以需要重新构建状态 dp[u][0]表示满足条件的以u为根节点的子树中,节点数为偶数的最大值 dp[u][0]表示满足条件的以u为根节点的子树中,节点数为奇数的最大值 最开始不选根节点.dp[u][0]=0,dp[u][1]为-INF(不存在) 每次加子节点v时 1.偶可以由偶(u)+偶(v),奇(u)+奇(v)更新 2.奇可以由奇(u)+奇(v),奇(u)+奇(v)更新

Educational Codeforces Round 23 F. MEX Queries(线段树)

题目链接:Educational Codeforces Round 23 F. MEX Queries 题意: 一共有n个操作. 1.  将[l,r]区间的数标记为1. 2.  将[l,r]区间的数标记为0. 3.  将[l,r]区间取反. 对每个操作,输出标记为0的最小正整数. 题解: hash后,用线段树xjb标记一下就行了. 1 #include<bits/stdc++.h> 2 #define ls l,m,rt<<1 3 #define rs m+1,r,rt<&l

hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]

传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 221    Accepted Submission(s): 52 Problem Description A topological sort or topological ordering of a directed

Codeforces Round #401 (Div. 2) E 贪心,线段树

Codeforces Round #401 (Div. 2) A 循环节 B 暴力排一下 C 标记出来,但10^5,特耿直地码了个O(n^2)的上去,最气的是在最后3分钟的时候被叉== D 从后往前贪心暴糙一下就好.比赛时一眼瞄出来了不敢写,搞不懂这样竟然不会超时.. E. Hanoi Factory 题意:n个环体,内径a[i],外径b[i],高h[i].当 a[i+1]<b[i]<=b[i+1] 时,第 i 个环体可以堆在第 i+1个环体上.求可以堆出的最高高度. tags:佩服那些大佬,

Codeforces Round #603 (Div. 2) E. Editor 线段树

E. Editor The development of a text editor is a hard problem. You need to implement an extra module for brackets coloring in text. Your editor consists of a line with infinite length and cursor, which points to the current character. Please note that

Codeforces VK Cup 2015 Wild Card Round 1 (AB)

比赛链接:http://codeforces.com/contest/522 A. Reposts time limit per test:1 second memory limit per test:256 megabytes One day Polycarp published a funny picture in a social network making a poll about the color of his handle. Many of his friends started

Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) B. T-Shirt Hunt

B. T-Shirt Hunt time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Not so long ago the Codecraft-17 contest was held on Codeforces. The top 25 participants, and additionally random 25 participant

Facebook Hacker Cup 2015 Round 1--Corporate Gifting(树形动态规划)

原题:https://www.facebook.com/hackercup/problems.php?pid=759650454070547&round=344496159068801 题意:给定一颗有根树,在树上下层的节点要给上层节点礼物,根节点的礼物则给慈善会,但是给礼物有个条件就是你不能送你的父节点已经送出的礼物.问满足要求的最少花费. 题解:这个题卡了一段时间,类似于染色问题,可以用树形动态规划求解.因为已知节点个数为N,则我们单个节点的最大花费不会超过log2(N) = 18. 1.

hacker cup 2015 Round 1 解题报告

A:求区间内素因子个数等于n的数有多少个 解题思路:筛法 解题代码: 1 // File Name: a.cpp 2 // Author: darkdream 3 // Created Time: 2015年01月18日 星期日 13时54分20秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque> 10 #include&