D. Wizards and Roads

题目连接:

http://www.codeforces.com/contest/167/problem/D

Description

In some country live wizards. They love to build cities and roads.

The country used to have k cities, the j-th city (1 ≤ j ≤ k) was located at a point (xj, yj). It was decided to create another n - k cities. And the i-th one (k < i ≤ n) was created at a point with coordinates (xi, yi):

xi = (a·xi - 1 + b) mod (109 + 9)

yi = (c·yi - 1 + d) mod (109 + 9)

Here a, b, c, d are primes. Also, a ≠ c, b ≠ d.

After the construction of all n cities, the wizards have noticed something surprising. It turned out that for every two different cities i and j, xi ≠ xj and yi ≠ yj holds.

The cities are built, it's time to build roads! It was decided to use the most difficult (and, of course, the most powerful) spell for the construction of roads. Using this spell creates a road between the towns of u, v (yu > yv) if and only if for any city w which lies strictly inside the corner at the point u, v (see below), there is a city s that does not lie in the corner, which is located along the x-coordinate strictly between w and u and simultaneously ys > yv.

A corner on the points p2(x2, y2), p1(x1, y1) (y1 < y2) is the set of points (x, y), for which at least one of the two conditions is fulfilled:

min(x1, x2) ≤ x ≤ max(x1, x2) and y ≥ y1

y1 ≤ y ≤ y2 and (x - x2)·(x1 - x2) ≥ 0

The pictures showing two different corners

In order to test the spell, the wizards will apply it to all the cities that lie on the x-coordinate in the interval [L, R]. After the construction of roads the national government wants to choose the maximum number of pairs of cities connected by the road, so that no city occurs in two or more pairs. Your task is for each m offered variants of values L, R to calculate the maximum number of such pairs after the construction of the roads. Please note that the cities that do not lie in the interval [L, R] on the x-coordinate, do not affect the construction of roads in any way.

Input

The first line contains two space-separated integers n, k (1 ≤ k ≤ n ≤ 105, k ≤ 30). Next k lines contain coordinates of the cities' location points from the first to the k-th one. The j-th line contains space-separated pair of integers xj, yj (0 ≤ xj, yj < 109 + 9) — coordinates of the j-th city.

The next line contains space-separated integers a, b, c, d (2 ≤ a, b, c, d < 109 + 9). It is guaranteed that those numbers are prime and also that a ≠ c, b ≠ d.

It's guaranteed, that for every two different cities i and j, xi ≠ xj and yi ≠ yj holds.

The next line contains integer m (1 ≤ m ≤ 105) — the number of variants to build the roads. Next m lines contain pairs of space-separated integers Li, Ri (0 ≤ Li ≤ Ri < 109 + 9) — the variants of choosing the cities to build the roads.

Output

For any pair of numbers Li, Ri print the answer to the problem on a single line. Print the answers for the pairs in the order, in which the pairs are given in the input data.

Sample Input

6 6

0 0

1 1

2 2

3 3

4 4

5 5

2 3 3 2

4

0 5

1 4

2 3

3 3

Sample Output

3

2

1

0

Hint

题意

平面上给你n个点,保证数据随机

然后有一个定义叫做拐角的东西,就是题目中的图片显示的那个

现在如果某两个点u,v(假设uy>vy)构成的拐角中的任何一个点w,都存在一个能与w形成拐角的点s满足sx在w,v之间,且sy>vy

那么uv就可以连边

现在问你横坐标在l,r区间的点做二分图匹配的最大值是多少

题解:

那个神TM的题意,真是迷

经过猜测,观察,迷之领悟,知道了这么一个结论

假设我们把每个点的下方分成左下角和右下角两个区域,那么每个点只能和这两个区域中的y坐标最大值连边

然后这样递归下来,显然就能够构成一个笛卡尔树的样子(其实就是

然后这是一颗二叉树

所以询问就很简单了,由于是二叉树,所以就是一个简单的贪心(或者dp

然后均摊的直接像线段树那样去跑一发就好了

复杂度是nlogn的

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
const int mod = 1e9+9;
int n,k,a,b,c,d;
pair<int,int>p[maxn];
int tot=1;
struct Tr
{
int l,r,x,lson,rson;
pair<int,int>v;
}tree[maxn];
int newnode(int &x)
{
return x=++tot;
}
pair<int,int> deal(pair<int,int> a,pair<int,int> b)
{
return make_pair(a.second+b.second,max(a.first+b.second,a.second+b.first)+1);
}
void build_tree(int l,int r,int x)
{
int high=-1,id=l;
for(int i=l;i<=r;i++)
if(p[i].second>high)
high=p[i].second,id=i;
tree[x].l=p[l].first,tree[x].r=p[r].first,tree[x].x=p[id].first;
if(id>l)build_tree(l,id-1,newnode(tree[x].lson));
if(id<r)build_tree(id+1,r,newnode(tree[x].rson));
tree[x].v=deal(tree[tree[x].lson].v,tree[tree[x].rson].v);
}
pair<int,int> query(int l,int r,int x)
{
if(x==0||l>tree[x].r||r<tree[x].l)return make_pair(-1,0);
if(l<=tree[x].l&&r>=tree[x].r)return tree[x].v;
if(r<tree[x].x)return query(l,r,tree[x].lson);
if(l>tree[x].x)return query(l,r,tree[x].rson);
return deal(query(l,r,tree[x].lson),query(l,r,tree[x].rson));
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=0;i<k;i++)
scanf("%d%d",&p[i].first,&p[i].second);
scanf("%d%d%d%d",&a,&b,&c,&d);
for(int i=k;i<n;i++)
p[i]=make_pair((1LL*p[i-1].first*a+b)%mod,(1LL*p[i-1].second*c+d)%mod);
sort(p,p+n);
tree[0].v=make_pair(-1,0);
build_tree(0,n-1,1);
int m;
scanf("%d",&m);
while(m--)
{
int a,b;scanf("%d%d",&a,&b);
printf("%d\n",query(a,b,1).second);
}
}

Codeforces Round #114 (Div. 1) D. Wizards and Roads 笛卡尔树+树贪心+阅读题的更多相关文章

  1. Codeforces Round #114 (Div. 1) B. Wizards and Huge Prize 概率dp

    B. Wizards and Huge Prize Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  2. Codeforces Round #114 (Div. 1) A. Wizards and Trolleybuses 物理题

    A. Wizards and Trolleybuses Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/conte ...

  3. Codeforces Round #114 (Div. 1) E. Wizards and Bets 高斯消元

    E. Wizards and Bets 题目连接: http://www.codeforces.com/contest/167/problem/E Description In some countr ...

  4. Codeforces Round #114 (Div. 1) C. Wizards and Numbers 博弈论

    C. Wizards and Numbers 题目连接: http://codeforces.com/problemset/problem/167/C Description In some coun ...

  5. Codeforces Round #689 (Div. 2, based on Zed Code Competition) E. Water Level (贪心好题)

    题意:你在一家公司工作\(t\)天,负责给饮水机灌水,饮水机最初有\(k\)升水,水的范围必须要在\([l,r]\)内,同事每天白天都会喝\(x\)升水,你在每天大清早可以给饮水机灌\(y\)升水,问 ...

  6. Codeforces Round #114 (Div. 2)

    Codeforces Round #114 (Div. 2) 代码 Codeforces Round #114 (Div. 2) C. Wizards and Trolleybuses 思路 每条车的 ...

  7. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset (0/1-Trie树)

    Vasiliy's Multiset 题目链接: http://codeforces.com/contest/706/problem/D Description Author has gone out ...

  8. Codeforces Round #538 (Div. 2) F 欧拉函数 + 区间修改线段树

    https://codeforces.com/contest/1114/problem/F 欧拉函数 + 区间更新线段树 题意 对一个序列(n<=4e5,a[i]<=300)两种操作: 1 ...

  9. Codeforces Round #327 (Div. 2) A. Wizards' Duel 水题

    A. Wizards' Duel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/prob ...

随机推荐

  1. vue-混入mixin

    混入 基础 混入 (mixins) 是一种分发 Vue 组件中可复用功能的非常灵活的方式.混入对象可以包含任意组件选项.当组件使用混入对象时,所有混入对象的选项将被混入该组件本身的选项. 例子: // ...

  2. vim查找/替换字符串【转】

    转自:http://www.cnblogs.com/GODYCA/archive/2013/02/22/2922840.html vi/vim 中可以使用 :s 命令来替换字符串.该命令有很多种不同细 ...

  3. C后端设计开发 - 第5章-内功-数据结构下卷

    正文 第5章-内功-数据结构下卷 后记 如果有错误, 欢迎指正. 有好的补充, 和疑问欢迎交流, 一块提高. 在此谢谢大家了.

  4. java8新特性视频、spring4.0视频讲解,javaee基础知识讲解等网址汇总

    1.http://ke.atguigu.com/     海量视频首页 2.http://ke.atguigu.com/course/56    java8新特性学习地址

  5. linux 系统调用exec()

    系统调用execve()对当前进程进行替换,替换者为一个指定的程序,其参数包括文件名(filename).参数列表(argv)以及环境变量(envp).exec函数族当然不止一个,但它们大致相同,在 ...

  6. 2.ubuntu的使用

    1. CTRL+ALT+T 可以将命令模式打开 2. 有可能没办法进行yum ,它会告诉你操作的方法 3. 有些操作需要获得root的权限才可以,我们得进入root状态 --> sudo pas ...

  7. C#.Net实体代码生成工具(EntitysCodeGenerate)的使用及.NET中的ORM实现

    1 引言 目前大多数项目或产品都使用关系型数据库实现业务数据的存储,这样在开发过程中,常常有一些业务逻辑需要直接用写SQL语句实现,但这样开发的结果是:遍地布满SQL语句.这些藕合较高的SQL语句给系 ...

  8. poj-1113

    Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31419   Accepted: 10619 Descriptio ...

  9. Dubbo 用户手册学习笔记 —— Dubbo架构

    Dubbo的架构 节点角色说明 节点 角色说明 Provider 服务提供方 Consumer 服务消费方 Registry 服务注册与发现的注册中心 Monitor 统计服务的调用次数和调用时间的监 ...

  10. addeventlistener监听scroll跟touch

    这三个事件只在手机上生效 touchstart,手指开始触屏 touchmove,手指移动 touchend,手指触屏结束   这个事件在手机上跟在pc端都生效 scroll事件     addeve ...