Shooting
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 846 Accepted Submission(s): 165
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.
![](https://i-blog.csdnimg.cn/blog_migrate/a4b02ade5626f49a87e205dbaff70d71.jpeg)
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
将目标按d排序,一个一个的插入线段树,区间更新,这样会牵涉到标记下传,但在可持久化线段树里面,标记下传是一个很耗内存的事,所以我们不用下传,只是在查询的时候,一路加到底,因为如果我们在某点打了标记,那么他的儿子也都要变化这么多。内存是硬伤。
#pragma comment(linker,"/STACK:102400000,102400000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
typedef long long ll;
#define rep(i,s,t) for(int i=s;i<t;i++)
#define red(i,s,t) for(int i=s-1;i>=t;i--)
#define ree(i,now) for(int i=head[now];i!=-1;i=edge[i].next)
#define clr(a,v) memset(a,v,sizeof a)
#define L t<<1
#define R t<<1|1
#define MID(l,r) int mid=(l+r)>>1
#define lowbit(x) (x&(-x))
#define SQR(a) ((a)*(a))
inline int input(){
int ret=0;bool isN=0;char c=getchar();
while(c<'0' || c>'9'){
if(c=='-') isN=1;
c=getchar();
}
while(c>='0' && c<='9'){
ret=ret*10+c-'0';
c=getchar();
}
return isN?-ret:ret;
}
inline void output(ll x){
if(x<0){
putchar('-');x=-x;
}
int len=0,data[20];
while(x){
data[len++]=x%10;x/=10;
}
if(!len) data[len++]=0;
while(len--)
putchar(data[len]+48);
putchar('\n');
}
const int MAXN=5900100;
int n,m,x,p;
int shotx,a,b,c,k;
ll pre,ans;
int ls[MAXN],rs[MAXN],num[MAXN],tot,root[100005];
ll sum[MAXN];
inline void copy(int k,int p){
ls[k]=ls[p],rs[k]=rs[p],num[k]=num[p],sum[k]=sum[p];
}
inline int build(int l,int r){
int k=(++tot);
sum[k]=num[k]=0;
if(l!=r){
MID(l,r);
ls[k]=build(l,mid);
rs[k]=build(mid+1,r);
}
return k;
}
inline int add(int p,int l,int r,int x,int y,int w){
int k=(++tot);copy(k,p);
if(l>=x && r<=y){
num[k]++,sum[k]+=w;
}
else{
MID(l,r);
if(y<=mid) ls[k]=add(ls[p],l,mid,x,y,w);
else if(x>mid) rs[k]=add(rs[p],mid+1,r,x,y,w);
else{
ls[k]=add(ls[p],l,mid,x,mid,w);
rs[k]=add(rs[p],mid+1,r,mid+1,y,w);
}
}
return k;
}
inline int queryN(int p,int l,int r,int x){
int ans=num[p];
if(l!=r){
MID(l,r);
if(x<=mid) ans+=queryN(ls[p],l,mid,x);
else ans+=queryN(rs[p],mid+1,r,x);
}
return ans;
}
inline ll queryS(int p,int l,int r,int x){
ll ans=sum[p];
if(l!=r){
MID(l,r);
if(x<=mid) ans+=queryS(ls[p],l,mid,x);
else ans+=queryS(rs[p],mid+1,r,x);
}
return ans;
}
inline int Binary_Search(int s,int t,int shotx,int k){
int cnt;
while(s<=t){
MID(s,t);
cnt=queryN(root[mid],1,x,shotx);
if(cnt==k) return mid;
if(cnt<=k) s=mid+1;
else t=mid-1;
}
return s;
}
struct TARGET{
int l,r,d;
void read(){
l=input(),r=input(),d=input();
}
bool operator < (const TARGET &a) const{
return d<a.d || (d==a.d&&l<a.l) || (d==a.d&&l==a.l&&r<a.r);
}
}target[100005];
int main(){
//cout<<sizeof(sum)+sizeof(num)+sizeof(ls)+sizeof(rs)+sizeof(root)<<endl;
while(~scanf("%d%d%d%d",&n,&m,&x,&p)){
rep(i,0,n) target[i].read();
sort(target,target+n);
tot=0;
root[0]=build(1,x);
rep(i,0,n){
root[i+1]=add(root[i],1,x,target[i].l,target[i].r,target[i].d);
}
pre=1;
rep(i,0,m){
shotx=input(),a=input(),b=input(),c=input();
k=(pre*a+b)%c;
if(!k) pre=0;
else{
int pos=Binary_Search(0,n,shotx,k);//二分在shotx处哪棵线段树的目标数==k
ans=queryS(root[pos],1,x,shotx);
if(pre>p) ans<<=1;
pre=ans;
}
output(pre);
}
}
return 0;
}
官方解题报告里面,是按d建树的,把每个目标表示成左端点,和右端点,然后按x升序往线段树里面放这些点,左端点就+1,并且加上d,否则就-1,并且-d
查询就是根据x二分找到合适的线段树,在这棵树上找前k个数的和。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
typedef long long ll;
#define rep(i,s,t) for(int i=s;i<t;i++)
#define red(i,s,t) for(int i=s-1;i>=t;i--)
#define ree(i,now) for(int i=head[now];i!=-1;i=edge[i].next)
#define clr(a,v) memset(a,v,sizeof a)
#define L t<<1
#define R t<<1|1
#define MID(l,r) int mid=(l+r)>>1
#define lowbit(x) (x&(-x))
#define SQR(a) ((a)*(a))
inline int input(){
int ret=0;bool isN=0;char c=getchar();
while(c<'0' || c>'9'){
if(c=='-') isN=1;
c=getchar();
}
while(c>='0' && c<='9'){
ret=ret*10+c-'0';
c=getchar();
}
return isN?-ret:ret;
}
inline void output(int x){
if(x<0){
putchar('-');x=-x;
}
int len=0,data[11];
while(x){
data[len++]=x%10;x/=10;
}
if(!len) data[len++]=0;
while(len--)
putchar(data[len]+48);
putchar('\n');
}
const int MAXN=100005;
struct TARGET{
int l,r,d;
void read(){
scanf("%d%d%d",&l,&r,&d);
}
bool operator < (const TARGET &a) const{
return l<a.l || (l==a.l && r<a.r);
}
}ta[MAXN];
struct Add{
int x,d,flag;
void init(int _x,int _d,int _flag){
x=_x,d=_d,flag=_flag;
}
bool operator < (const Add &a) const {
return x<a.x || (x==a.x && flag>a.flag) || (x==a.x && flag==a.flag && d<a.d);
}
}A[MAXN<<1];
int root[4000005],ls[4000005],rs[4000005],l[4000005],r[4000005],num[4000005];
ll sum[4000005];
int d[MAXN],cnt,tot;
int n,m,x,p,shotx,shota,shotb,shotc;
ll pre;
inline void copy(int k,int p){
sum[k]=sum[p];num[k]=num[p];
ls[k]=ls[p],rs[k]=rs[p];
l[k]=l[p],r[k]=r[p];
}
inline int build(int x,int y){
int k=tot++;
l[k]=x,r[k]=y;
num[k]=sum[k]=0;
if(x!=y){
MID(x,y);
ls[k]=build(x,mid);
rs[k]=build(mid+1,y);
}
return k;
}
inline int change(int p,int x,int val,int v){
int k=tot++;
copy(k,p);
num[k]+=v,sum[k]+=val;
if(l[p]==r[p]) return k;
MID(l[p],r[p]);
if(x<=mid) ls[k]=change(ls[p],x,val,v);
else rs[k]=change(rs[p],x,val,v);
return k;
}
inline ll query(int p,int l,int r,int k){
if(l==r) return k*1LL*d[l];
MID(l,r);
if(k<=num[ls[p]]) return query(ls[p],l,mid,k);
return sum[ls[p]]+query(rs[p],mid+1,r,k-num[ls[p]]);
}
int main(){
while(~scanf("%d%d%d%d",&n,&m,&x,&p)){
cnt=tot=0;
rep(i,0,n){
ta[i].read();
d[++cnt]=ta[i].d;
}
sort(d+1,d+cnt+1);
cnt=unique(d+1,d+cnt+1)-(d+1);
int id=0;
rep(i,0,n){
int dd=lower_bound(d+1,d+cnt+1,ta[i].d)-(d+1)+1;
A[id++].init(ta[i].l,dd,1);
A[id++].init(ta[i].r,dd,-1);
}
sort(A,A+id);
root[0]=build(1,cnt);
rep(i,0,id){
root[i+1]=change(root[i],A[i].d,A[i].flag*d[A[i].d],A[i].flag*1);
}
pre=1;
rep(i,0,m){
scanf("%d%d%d%d",&shotx,&shota,&shotb,&shotc);
Add a;
a.init(shotx,1,0);
int xid=lower_bound(A,A+id,a)-(A);
int pos=(pre*shota+shotb)%shotc;
ll ans=query(root[xid],1,cnt,pos);
if(pre>p) ans<<=1;
pre=ans;
cout<<ans<<endl;
}
}
}