3165: [Heoi2013]Segment
Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 718 Solved: 296
[ Submit][ Status][ Discuss]
Description
要求在平面直角坐标系下维护两个操作:
1.在平面上加入一条线段。记第i条被插入的线段的标号为i。
2.给定一个数k,询问与直线 x = k相交的线段中,交点最靠上的线段的编号。
Input
第一行一个整数n,表示共n 个操作。
接下来n行,每行第一个数为0或1。
若该数为 0,则后面跟着一个正整数 k,表示询问与直线
x = ((k +lastans–1)%39989+1)相交的线段中交点(包括在端点相交的情形)最靠上的线段的编号,其中%表示取余。若某条线段为直线的一部分,则视作直线与线段交于该线段y坐标最大处。若有多条线段符合要求,输出编号最小的线段的编号。
若该数为 1,则后面跟着四个正整数 x0, y0, x 1, y 1,表示插入一条两个端点为
((x0+lastans-1)%39989+1,(y0+lastans-1)%10^9+1)和((x
1+lastans-1)%39989+1,(y1+lastans-1)%10^9+1) 的线段。
其中lastans为上一次询问的答案。初始时lastans=0。
Output
对于每个 0操作,输出一行,包含一个正整数,表示交点最靠上的线段的编号。若不存在与直线相交的线段,答案为0。
Sample Input
1 8 5 10 8
1 6 7 2 6
0 2
0 9
1 4 7 6 7
0 5
Sample Output
0 3
HINT
对于100%的数据,1 ≤ n ≤ 10^5 , 1 ≤ k, x0, x1 ≤ 39989, 1 ≤ y0 ≤ y1 ≤ 10^9。
Source
#include<cstdio>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
typedef double DL;
const int INF = 2147483647;
const int maxn = 4 * 40000;
const DL eps = 1e-9;
int n,tot,ans,len;
DL k[maxn],b[maxn];
int id[maxn];
inline LL getint()
{
LL ret = 0,f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
ret = ret * 10 + c - '0',c = getchar();
return ret * f;
}
inline bool equal(DL a,DL b)
{
return fabs(a - b) <= eps;
}
inline void pushdown(int o,int l,int r,int x)
{
DL kx = k[x],bx = b[x],ko = k[id[o]],bo = b[id[o]];
int mid = l + r >> 1;
DL xx = kx * mid + bx,xo = ko * mid + bo;
if (l == r)
{
if (xx > xo) id[o] = x;
return;
}
int lc = o << 1,rc = o << 1 | 1;
if (xx < xo || equal(xx,xo))
{
if (kx < ko) pushdown(lc,l,mid,x);
else pushdown(rc,mid + 1,r,x);
}
else
{
if (kx < ko) pushdown(rc,mid + 1,r,id[o]);
else pushdown(lc,l,mid,id[o]);
id[o] = x;
}
}
inline void insert(int o,int l,int r,int al,int ar,int x)
{
if (al <= l && r <= ar){pushdown(o,l,r,x); return;}
int mid = l + r >> 1,lc = o << 1,rc = o << 1 | 1;
if (al <= mid) insert(lc,l,mid,al,ar,x);
if (mid < ar) insert(rc,mid + 1,r,al,ar,x);
}
inline int qmax(int x,int y,int pos)
{
if (equal(k[x] * pos + b[x],k[y] * pos + b[y]))
{
if (x < y) return x;
else return y;
}
if (k[x] * pos + b[x] < k[y] * pos + b[y]) return y;
if (k[x] * pos + b[x] > k[y] * pos + b[y]) return x;
}
inline int query(int o,int l,int r,int pos)
{
int ret = id[o];
if (l == r) return ret;
int mid = l + r >> 1,lc = o << 1,rc = o << 1 | 1;
if (pos <= mid) ret = qmax(ret,query(lc,l,mid,pos),pos);
else ret = qmax(ret,query(rc,mid + 1,r,pos),pos);
return ret;
}
int main()
{
len = 40000;
n = getint();
for (int i = 1; i <= n; i++)
{
int d = getint();
if (!d)
{
// int x = (getint() + ans - 1) % 39989 + 1;
int x = getint();
printf("%d\n",ans = query(1,1,len,x));
}
else
{
// int x0 = (getint() + ans - 1) % 39989 + 1;
// int y0 = (getint() + ans - 1) % (int)(1e9) + 1;
// int x1 = (getint() + ans - 1) % 39989 + 1;
// int y1 = (getint() + ans - 1) % (int)(1e9) + 1;
int x0 = getint(),y0 = getint(),x1 = getint(),y1 = getint();
if (x0 > x1) swap(x0,x1) , swap(y0,y1);
if (x0 == x1){if (y0 > y1) swap(y1,y0); k[++tot] = 0; b[tot] = y1;}
else k[++tot] = (y1 - y0) * 1.0 / (x1 - x0) , b[tot] = y0 - k[tot] * x0;
insert(1,1,len,x0,x1,tot);
}
}
return 0;
}