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.
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
因为主席树记录是二维的,所以这道题就可以以x轴建树,在叶子结点记录有多少点以及sum,不要误会,我们是需要先离散化在放点的,这样一个叶子结点的下标就表示当前的靶子在所有靶子里面的排名。因为它可能是有重边的,所以我们在query的时候最后返回的是询问的下标对应的离散化之后的值乘上最后还有几次射击机会= =!
差不多就是这么个意思,这道题不算难,类似于第k小,但是因为自己不够优秀shabi做了好久才做出来。。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e5+5;
#define pa pair<ll,int>
#define mp(a,b) make_pair(a,b)
ll sum[maxn*40],num[maxn*40];
int ls[maxn*40],rs[maxn*40],rt[maxn];
int cnt;
void update(int l,int r,int root,int last,ll val,int numb,int pos)
{
ls[root]=ls[last];
rs[root]=rs[last];
sum[root]=sum[last]+val;
num[root]=num[last]+numb;
if(l==r)
return ;
int mid=l+r>>1;
if(mid>=pos)
update(l,mid,ls[root]=++cnt,ls[last],val,numb,pos);
else
update(mid+1,r,rs[root]=++cnt,rs[last],val,numb,pos);
}
ll a[maxn],b[maxn];
ll query(int l,int r,int root,int pos)
{
if(l==r)
{
return (ll)b[l]*pos;
}
int mid=l+r>>1;
int all=num[ls[root]];
if(all>=pos)
return query(l,mid,ls[root],pos);
else
return sum[ls[root]]+query(mid+1,r,rs[root],pos-all);
}
vector<pa>vec[maxn];
int lll[maxn],rrr[maxn];
int main()
{
int n,m,x,p;
while(~scanf("%d%d%d%d",&n,&m,&x,&p))
{
cnt=0;
memset(sum,0,sizeof(sum));
memset(num,0,sizeof(num));
for(int i=1;i<=x;i++)
vec[i].clear();
int l,r;
for(int i=1;i<=n;i++)
scanf("%d%d%lld",&lll[i],&rrr[i],&a[i]),b[i]=a[i];
sort(b+1,b+1+n);
int all=unique(b+1,b+1+n)-b-1;
for(int i=1;i<=n;i++)
{
int pos=lower_bound(b+1,b+1+all,a[i])-b;
vec[lll[i]].push_back(mp(a[i],pos));
vec[rrr[i]+1].push_back(mp(-a[i],pos));
}
for(int i=1;i<=x;i++)
{
rt[i]=rt[i-1];
for(int j=0;j<vec[i].size();j++)
{
//cout<<i<<" "<<vec[i][j].first<<endl;
if(j==0)
update(1,all,rt[i]=++cnt,rt[i-1],vec[i][j].first,(vec[i][j].first<0?-1:1),vec[i][j].second);
else
{
int now=++cnt;
update(1,all,now,rt[i],vec[i][j].first,(vec[i][j].first<0?-1:1),vec[i][j].second);
rt[i]=now;
}
}
//cout<<i<<" "<<num[rt[i]]<<endl;
}
int pos;
ll a,b,c,pre=1;
while(m--)
{
scanf("%d%lld%lld%lld",&pos,&a,&b,&c);
ll k=(a*pre+b)%c;
ll ans;
if(k>=num[rt[pos]])
ans=sum[rt[pos]];
else if(k==0)
ans=0;
else
ans=query(1,all,rt[pos],k);
if(pre>p)
ans*=2;
printf("%lld\n",ans);
pre=ans;
}
}
return 0;
}