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.
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
题意:给你n,m,X,p。n表示有n个线段,m表示有m次询问。接下来n行,每行有3个数l,r,d,表示线段的左端点右端点以及线段的高度。接下来m行,每行4个数x,a,b,c,k=(lastans*a+b)%c,表示你从x这个点向上开枪,能击穿k条线段,每击穿一条线段,你获得的价值为该线段的高度。如果该点上面的线段小于k,其上线段条数为x,那么获得的总价值为x条线段的价值加上x上面最高的线段的高度*(k-x)。并且,如果lastans>p,那么本次开枪获得的价值讲翻倍。输出每次开枪的获得的价值。
思路:能击穿的k条线段,一定是x向上的前k高的线段,这时我们很容易想到主席树求区间前k小的值,很容易联想到主席树。那么主席树就维护高度,由于高度最大是1e7,因此我们需要进行离散化。那么我们怎么向主席树里插入这些线段呢?我们可以将一个线段拆成2个点:l和r+1,类似于差分的思想,在l处+1,在r+1处-1,按位置从小到大排序,依次插入到主席树中。我们再记录一个cnt,表示现在主席树中有几条线段,二分找前k个即可。
#include
using namespace std;
typedef long long ll;
const int maxn=1e6+100;
int n,m,X,l,r,d,cnt,cont,ct,x,k;
int root[maxn],b[maxn];
ll p,pre,ans,A,B,C;
struct node{
int pos;
int d;
int flag;
friend bool operator < (node x,node y)
{
if(x.pos==y.pos) return x.d>1;
build(tree[cur].l,l,m);
build(tree[cur].r,m+1,r);
}
void update(int &now,int last,int tar,int flag,ll val,int l,int r)
{
now=++ct;
tree[now]=tree[last];
tree[now].sum+=flag*val;
tree[now].cnt+=flag;
if(l==r) return ;
int m=(l+r)>>1;
if(tar<=m) update(tree[now].l,tree[last].l,tar,flag,val,l,m);
else update(tree[now].r,tree[last].r,tar,flag,val,m+1,r);
}
ll query(int cur,int k,int l,int r)
{
if(tree[cur].cnt<=k) return tree[cur].sum;
if(l==r) return (ll)(tree[cur].sum/tree[cur].cnt)*k;
int m=(l+r)>>1;
if(tree[tree[cur].l].cnt>=k) return query(tree[cur].l,k,l,m);
else return tree[tree[cur].l].sum+query(tree[cur].r,k-tree[tree[cur].l].cnt,m+1,r);
}
int main()
{
while(~scanf("%d%d%d%lld",&n,&m,&X,&p))
{
cnt=0;pre=1;ct=0;
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&l,&r,&d);
a[++cnt].pos=l;
a[cnt].d=d;
a[cnt].flag=1;
a[++cnt].pos=r+1;
a[cnt].d=d;
a[cnt].flag=-1;
b[i]=d;
}
sort(b+1,b+1+n);
sort(a+1,a+1+cnt);
cont=unique(b+1,b+1+n)-(b+1);
build(root[0],1,cont);
for(int i=1;i<=cnt;i++) update(root[i],root[i-1],getid(a[i].d),a[i].flag,a[i].d,1,cont);
while(m--)
{
scanf("%d%lld%lld%lld",&x,&A,&B,&C);
k=((A*pre)%C+B)%C;
node tmp;
tmp.pos=x;tmp.flag=0;tmp.d=0x3fffffff;
int id=upper_bound(a+1,a+1+cnt,tmp)-a-1;
if(id<=0) ans=0;
else ans=query(root[id],k,1,cont);
if(pre>p) ans*=2;
printf("%lld\n",ans);
pre=ans;
}
}
return 0;
}