hooting
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1254 Accepted Submission(s): 259
Problem Description
In the shooting game, the player can choose to stand in the position of [1, X] to shoot, you can shoot all the nearest K targets. The value of K may be different on different shootings. There are N targets to shoot, each target occupy the position of [Li, Ri] , the distance from the starting line is Di(1<=i<=N). Shooting a target can get the bonus points equal to the distance to the starting line. After each shooting targets still exist. Additional, if the previous bonus points is more than P, then as a reward for the shooting present bonus points doubled(and then check the next one). Player wants to know the bonus points he got in each shot.
Input
The input consists several test cases.
The first line has two integers, N, M, X, P(1<=N, M ,X<=100000, P<=1000000000), N represents the total number of target shooting, M represents the number of shooting.
The next N lines of three integers L, R, D (1<=L<=R<=X, 1<=D<=10000000), occupy the position of [L, R] and the distance from the starting line is D.
The next M lines, each line contains four integers x, a, b, c (1<=x<=X, 0<=a,b<=N, 1<=c<=10000000), and K = ( a * Pre + b ) % c. Pre is the bonus point of previous shooting , and for the first shooting Pre=1. It denotes standing in the position x and the player can shoot the nearest K targets.
The first line has two integers, N, M, X, P(1<=N, M ,X<=100000, P<=1000000000), N represents the total number of target shooting, M represents the number of shooting.
The next N lines of three integers L, R, D (1<=L<=R<=X, 1<=D<=10000000), occupy the position of [L, R] and the distance from the starting line is D.
The next M lines, each line contains four integers x, a, b, c (1<=x<=X, 0<=a,b<=N, 1<=c<=10000000), and K = ( a * Pre + b ) % c. Pre is the bonus point of previous shooting , and for the first shooting Pre=1. It denotes standing in the position x and the player can shoot the nearest K targets.
Output
Output M lines each corresponds to one integer.
Sample Input
4 3 5 8 1 2 6 2 3 3 2 4 7 1 5 2 2 2 1 5 3 1 1 10 4 2 3 7
Sample Output
11 10 18
Author
FZU
Source
/*是按h建树的,把每个目标表示成左端点,和右端点,然后按x升序往线段树里面放这些点,
左端点就+1,并且加上d,否则就-1,并且-d
查询就是根据x二分找到合适的线段树,在这棵树上找前k个数的和。*/
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long ll;
#define N 100005
#define M 4000000
ll c2[M];
int c1[M],lson[M],rson[M],tot,n,T[N*2],rank[N];
struct pp
{
int x,h,id;
}node[N*2],seg[N];
bool cmp1(pp a,pp b)
{
if(a.h!=b.h) return a.h<b.h;
else return a.id<b.id;
}
bool cmp2(pp a,pp b)
{
if(a.x!=b.x) return a.x<b.x;
else return a.h>b.h;
}
int build(int x,int y)
{
int rt=tot++;
c1[rt]=c2[rt]=0;
if(x<y)
{
int mid=(x+y)>>1;
lson[rt]=build(x,mid);
rson[rt]=build(mid+1,y);
}
return rt;
}
int insert(int root,int p,int num,int sum)
{
int newroot=tot++, tmp=newroot;
c1[newroot]=c1[root]+num;
c2[newroot]=c2[root]+sum;
int l=1,r=n;
while(l<r)
{
int mid=(l+r)>>1;
if(p<=mid)
{
lson[newroot]=tot++; rson[newroot]=rson[root];
newroot=lson[newroot]; root=lson[root];
r=mid;
}
else
{
rson[newroot]=tot++; lson[newroot]=lson[root];
newroot=rson[newroot]; root=rson[root];
l=mid+1;
}
c1[newroot]=c1[root]+num;
c2[newroot]=c2[root]+sum;
}
return tmp;
}
ll query(int rt,int k)
{
ll sum=0;
int l,r;
l=1; r=n;
while(l<r)
{
int mid=(l+r)>>1;
if(c1[lson[rt]]>=k)
{
r=mid;
rt=lson[rt];
}
else
{
k-=c1[lson[rt]];
sum+=c2[lson[rt]];
l=mid+1;
rt=rson[rt];
}
}
return sum+c2[rt];
}
int main()
{
int m,x,p,k,l,r,d,ans,i,a,b,c,num;
ll pre,an;
// freopen("a.txt","r",stdin);
while(scanf("%d%d%d%d",&n,&m,&x,&p)!=EOF)
{
k=0; tot=1;
for(i=1;i<=n;i++)
{
scanf("%d%d%d",&l,&r,&d); k++;
node[k].x=l; node[k].id=i; node[k].h=d; k++;
node[k].x=r; node[k].id=i; node[k].h=-d;
seg[i].id=i; seg[i].h=d;
}
sort(seg+1,seg+1+n,cmp1);
sort(node+1,node+k+1,cmp2);
/* for(i=1;i<=k;i++)
{
printf("%d %d %d\n",node[i].x,node[i].h,node[i].id);
}*/
for(i=1;i<=n;i++)
{
rank[seg[i].id]=i;
}
T[0]=build(1,n);
for(i=1;i<=k;i++)
{
if(node[i].h>=0)
T[i]=insert(T[i-1],rank[node[i].id],1,node[i].h);
else
T[i]=insert(T[i-1],rank[node[i].id],-1,node[i].h);
}
/*for(i=1;i<=k;i++)
{
printf("T[%d]=%d\n",i,T[i]);
}*/
pre=1;
while(m--)
{
scanf("%d%d%d%d",&x,&a,&b,&c);
num=((a*pre%c)+b%c)%c;
if(num==0)
{
printf("0\n");
pre=0;
continue;
}
l=1;r=k;
while(l<=r)
{
int mid=(l+r)>>1;
if(node[mid].x<x||(node[mid].x==x&&node[mid].h>=0))
{
ans=mid;
l=mid+1;
}
else
r=mid-1;
}
// printf("ans=%d num=%d\n",ans,num);
an=query(T[ans],num);
//printf("an=%lld\n",an);
if(pre>p)
pre=an*2;
else pre=an;
printf("%lld\n",pre);
}
}
return 0;
}