4056 hdu4866 Shooting
题目描述
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.
输入
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 M lines each corresponds to one integer.
样例输入
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
样例输出
11 10 18
呼呼呼临走前终于弄出来了,又一道主席树
总之就是维护X棵线段树的感觉,每条线段拆成添加和修改两个操作
如果同一个位置有多个操作的话rt数组就记录做完最后那一次的
不过这题还有别的做法
用两颗主席树去做,一颗负责在加入时加入,一颗负责在删除时加入
这样的话利用可减性,用第一颗减去第二颗就是实际的树了
我比较懒啊,写的第一种算法,不过总感觉有点鬼。。。
为什么跟我的同学比我校平台上我比较快,而hdu上我就被卡成TLE他就A了???
இ௰இ

1 #include<cstdio> 2 #include<algorithm> 3 #define ll long long 4 using namespace std; 5 int N,M,X,P,tot,siz,st; 6 int rt[100005]; 7 struct node{ 8 int l,r,s; 9 ll sum; 10 }t[2000005]; 11 struct mmove{ 12 int x,opt; 13 ll sum; 14 }g[200005]; 15 bool cmp(mmove A,mmove B){return A.x<B.x;} 16 ll ds[100005],pre; 17 int insert(int k,int x,int o,int l,int r){ 18 t[++tot]=t[k];k=tot; 19 if(l==r){ 20 t[k].s+=o; 21 t[k].sum+=ds[l]*o; 22 return k; 23 } 24 int mid=(l+r)/2; 25 if(x<=mid) t[k].l=insert(t[k].l,x,o,l,mid); 26 else t[k].r=insert(t[k].r,x,o,mid+1,r); 27 t[k].s=t[t[k].l].s+t[t[k].r].s; 28 t[k].sum=t[t[k].l].sum+t[t[k].r].sum; 29 return k; 30 } 31 ll query(int k,int x,int l,int r){ 32 if(l==r)return (t[k].s==0?0:(t[k].sum/t[k].s)*min(t[k].s,x)); 33 int mid=(l+r)/2; 34 if(t[t[k].l].s>=x)return query(t[k].l,x,l,mid); 35 else return t[t[k].l].sum+query(t[k].r,x-t[t[k].l].s,mid+1,r); 36 } 37 int main(){ 38 while(scanf("%d%d%d%d",&N,&M,&X,&P)!=EOF){ 39 pre=1;tot=0; 40 t[0]=(node){0,0,0,0}; 41 for(int i=1;i<=N;i++){ 42 scanf("%d%d%lld",&g[i].x,&g[i+N].x,&ds[i]); 43 g[i+N].x++; 44 g[i].opt=1;g[i+N].opt=-1; 45 g[i+N].sum=g[i].sum=ds[i]; 46 } 47 sort(ds+1,ds+1+N); 48 siz=unique(ds+1,ds+1+N)-ds-1; 49 for(int i=1;i<=2*N;i++) 50 g[i].sum=lower_bound(ds+1,ds+1+siz,g[i].sum)-ds; 51 sort(g+1,g+2*N+1,cmp); 52 st=1; 53 for(int i=1;i<=X;i++){ 54 rt[i]=rt[i-1]; 55 while(st<=2*N&&g[st].x==i){ 56 rt[i]=insert(rt[i],g[st].sum,g[st].opt,1,N); 57 st++; 58 } 59 } 60 for(int i=1,x,a,b,c,K;i<=M;i++){ 61 scanf("%d%d%d%d",&x,&a,&b,&c); 62 K=(1ll*a*pre+b)%c; 63 pre=query(rt[x],K,1,N)*(pre>P?2:1); 64 printf("%lld\n",pre); 65 } 66 } 67 }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
· C# 13 中的新增功能实操
· Supergateway:MCP服务器的远程调试与集成工具
· Vue3封装支持Base64导出的电子签名组件