HDU 4866Shooting

来源:互联网 发布:叙利亚内战 知乎 编辑:程序博客网 时间:2024/06/07 01:27
In the shooting game, the player can choose to stand in the position of 1,X1,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,RiLi,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,RL,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 81 2 62 3 32 4 71 5 22 2 1 53 1 1 104 2 3 7
Sample Output
1110

18

简单地说,题意是给出n条线段,m个询问,在线的求在x位置上,高度小的前k条线段的高度和。

直接把线段拆成左右两个端点,然后用主席树顺着建树,询问的时候直接在对应树上求前k小的和即可。

题目的空间足够,不用离散化也可以。

#include<map>#include<ctime>#include<cmath>    #include<queue> #include<string>#include<vector>#include<cstdio>    #include<cstring>  #include<iostream>#include<algorithm>    #include<functional>using namespace std;#define ms(x,y) memset(x,y,sizeof(x))    #define rep(i,j,k) for(int i=j;i<=k;i++)    #define per(i,j,k) for(int i=j;i>=k;i--)    #define loop(i,j,k) for (int i=j;i!=-1;i=k[i])    #define inone(x) scanf("%d",&x)    #define intwo(x,y) scanf("%d%d",&x,&y)    #define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)  #define infou(x,y,z,p) scanf("%d%d%d%d",&x,&y,&z,&p) typedef long long LL;const int low(int x) { return x&-x; }const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int N = 2e5 + 10;const int M = 5e6;const double eps = 1e-10;int n, m, len, P, D, x, y, z, a;int ft[N], nt[N], u[N], sz;int rt[N], f[M], L[M], R[M], tot;LL g[M];void insert(int x, int &y, int l, int r, int u){y = ++tot; L[y] = L[x]; R[y] = R[x]; f[y] = f[x] + u / abs(u); g[y] = g[x] + u;if (l == r) return;int mid = l + r >> 1;if (abs(u) <= mid) insert(L[x], L[y], l, mid, u);else insert(R[x], R[y], mid + 1, r, u);}LL get(int x, int l, int r, int k){if (!k || l == r) return 1LL * min(k, f[x]) * l;int mid = l + r >> 1;if (f[L[x]] <= k) return g[L[x]] + get(R[x], mid + 1, r, k - f[L[x]]);return get(L[x], l, mid, k);}int main(){while (~infou(n, m, len, P)){tot = D = sz = 0;rep(i, 1, len) ft[i] = -1;rep(i, 1, n){inthr(x, y, z);u[sz] = z; nt[sz] = ft[x]; ft[x] = sz++;u[sz] = -z; nt[sz] = ft[y + 1]; ft[y + 1] = sz++;D = max(D, z);}rep(i, 1, len){int k = rt[i - 1];loop(j, ft[i], nt) insert(k, rt[i], 1, D, u[j]), k = rt[i];rt[i] = k;}LL pre = 1;rep(i, 1, m){infou(a, x, y, z);pre = (pre > P ? 2 : 1) * get(rt[a], 1, D, (1LL * x * pre + y) % z);printf("%I64d\n", pre);}}return 0;}


0 0