HDU 4866 Shooting(持久化线段树)
view code//第二道持久化线段树,照着别人的代码慢慢敲,还是有点不理解
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
#define lson l,m,ls[rt]
#define rson m+1,r,rs[rt]
const int N = 200020;
const int M = N*40;
int T[M], ls[M], rs[M], sum[M], id[N];
ll val[M];
int n, m, X, P, tot; struct seg
{
int p, h, id;
seg() {}
seg(int p, int h, int id):p(p),h(h),id(id) {}
bool operator < (const seg &o) const{
return p<o.p || (p==o.p&&h>o.h);
}
}e[N];
int ecnt; struct node
{
int h, id;
node(int h=0, int id=0):h(h),id(id) {}
bool operator < (const node &o) const
{
return h<o.h;
}
}sto[N]; void build(int l, int r, int& rt)
{
rt = ++tot;
sum[rt] = val[rt]=0;
if(l==r) return ;
int m = (l+r)>>1;
build(lson);
build(rson);
} void update(int p, int c, ll v, int last, int l, int r, int &rt)
{
rt = ++tot;
ls[rt] = ls[last], rs[rt] = rs[last];
sum[rt]=sum[last]+c, val[rt] = val[last]+v;
if(r==l) return ;
int m = (l+r)>>1;
if(p<=m) update(p, c, v, ls[last], lson);
else update(p, c, v, rs[last], rson);
} ll query(int k, int l, int r, int rt)
{
if(k==0) return 0;
if(l==r) return val[rt];
int m = (l+r)>>1;
ll ans = 0;
if(sum[ls[rt]]>k) ans = query(k, lson);
else ans = val[ls[rt]] + query(k-sum[ls[rt]], rson);
return ans;
} int find(int key) //找到最后一个小于key||(等于key&&高度大于0) 位置
{
int l = 1, r = ecnt, ans=0;
while(l<=r)
{
int m = (l+r)>>1;
if(e[m].p<key || (e[m].p==key&&e[m].h>=0))
ans = m, l = m+1;
else r = m-1;
}
// int l=1,r=ecnt+1, ans;
// while (l<r)
// {
// int mid=(l+r)>>1;
// if (e[mid].p<key || (e[mid].p==key && e[mid].h>=0)){
// ans=mid;
// l=mid+1;
// }
// else{
// r=mid;
// }
// }
return ans;
} void show()
{
for(int i=1; i<ecnt; i++)
{
printf("e[%d] = {%d, %d, %d}\n", i, e[i].p, e[i].h, id[e[i].id]);
}
} int main()
{
// freopen("in.txt", "r", stdin);
while(scanf("%d%d%d%d", &n, &m, &X, &P)>0)
{
ecnt = 0, tot = 0;
int l, r, w;
for(int i=1; i<=n; i++)
{
scanf("%d%d%d", &l, &r, &w);
e[++ecnt] = seg(l, w, i);
e[++ecnt] = seg(r, -w, i);
sto[i] = node(w, i);
}
sort(e+1, e+ecnt+1);
sort(sto+1, sto+n+1);
for(int i=1; i<=n; i++) id[sto[i].id]=i; // show();
build(1, n, T[0]);
for(int i=1; i<=ecnt; i++)
{
update(id[e[i].id], (e[i].h>=0?1:-1), e[i].h, T[i-1], 1, n, T[i]);
}
ll pre=1;
int pos, a, b, c;
while(m--)
{
scanf("%d%d%d%d", &pos, &a, &b, &c);
ll k = (ll)(a*pre+b)%c;
int p =find(pos);
// printf("pos = %d, p = %d, k = %I64d\n", pos, p, k);
// printf("sum[%d] = %I64d\n", p, val[T[p]]);
ll ans = query(k, 1, n, T[p]);
if(pre>P) ans *=2;
pre = ans;
printf("%I64d\n", ans);
}
}
return 0;
}
view code#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
#define lson l,m,ls[rt]
#define rson m+1,r,rs[rt]
const int N = 200020;
const int M = N*40;
int T[M], ls[M], rs[M], sum[M], id[N];
ll val[M];
int n, m, X, P, tot; struct seg
{
int p, h, id;
seg() {}
seg(int p, int h, int id):p(p),h(h),id(id) {}
bool operator < (const seg &o) const{
return p<o.p || (p==o.p&&h>o.h);
}
}e[N];
int ecnt; struct node
{
int h, id;
node(int h=0, int id=0):h(h),id(id) {}
bool operator < (const node &o) const
{
return h<o.h;
}
}sto[N]; void build(int l, int r, int& rt)
{
rt = ++tot;
sum[rt] = val[rt]=0;
if(l==r) return ;
int m = (l+r)>>1;
build(lson);
build(rson);
} void update(int p, int c, ll v, int last, int l, int r, int &rt)
{
rt = ++tot;
ls[rt] = ls[last], rs[rt] = rs[last];
sum[rt]=sum[last]+c, val[rt] = val[last]+v;
if(r==l) return ;
int m = (l+r)>>1;
if(p<=m) update(p, c, v, ls[last], lson);
else update(p, c, v, rs[last], rson);
} ll query(int k, int l, int r, int rt)
{
if(k==0) return 0;
if(l==r) return val[rt];
int m = (l+r)>>1;
ll ans = 0;
if(sum[ls[rt]]>k) ans = query(k, lson);
else ans = val[ls[rt]] + query(k-sum[ls[rt]], rson);
return ans;
} int find(int key)
{
int l = 1, r = ecnt, ans=0;
while(l<=r)
{
int m = (l+r)>>1;
if(e[m].p<=key)
ans=m, l = m+1;
else r = m-1;
}
return ans;
} int gettype(int x)//negative or not
{
return x>=0?1:-1;
} int main()
{
while(scanf("%d%d%d%d", &n, &m, &X, &P)>0)
{
ecnt = 0, tot = 0;
int l, r, w;
for(int i=1; i<=n; i++)
{
scanf("%d%d%d", &l, &r, &w);
e[++ecnt] = seg(l, w, i);
e[++ecnt] = seg(r+1, -w, i);
sto[i] = node(w, i);
} sort(e+1, e+ecnt+1);
sort(sto+1, sto+n+1);
for(int i=1; i<=n; i++) id[sto[i].id]=i; build(1, n, T[0]);
for(int i=1; i<=ecnt; i++)
{
update(id[e[i].id], gettype(e[i].h), e[i].h, T[i-1], 1, n, T[i]);
}
ll pre=1;
int pos, a, b, c;
while(m--)
{
scanf("%d%d%d%d", &pos, &a, &b, &c);
ll k = (ll)(a*pre+b)%c;
int p =find(pos);
ll ans = query(k, 1, n, T[p]);
if(pre>P) ans *=2;
pre = ans;
printf("%I64d\n", ans);
}
}
return 0;
}
HDU 4866 Shooting(持久化线段树)的更多相关文章
- HDU 5820 (可持久化线段树)
Problem Lights (HDU 5820) 题目大意 在一个大小为50000*50000的矩形中,有n个路灯.(n<=500000) 询问是否每一对路灯之间存在一条道路,使得长度为|x1 ...
- HDU 4866 Shooting (主席树)
题目链接 HDU 4866 题意 给定$n$条线段.每条线段平行$x$轴,离x轴的距离为$D$,覆盖的坐标范围为$[L, R]$. 现在有$m$次射击行动,每一次的射击行动可以描述为在横坐标$ ...
- HDU 4866 Shooting 扫描线 + 主席树
题意: 在二维平面的第一象限有\(n(1 \leq n \leq 10^5)\)条平行于\(x\)轴的线段,接下来有\(m\)次射击\(x \, a \, b \, c\). 每次射击会获得一定的分数 ...
- HDU 4866 Shooting(主席树)题解
题意:在一个射击游戏里面,游戏者可以选择地面上[1,X]的一个点射击,并且可以在这个点垂直向上射击最近的K个目标,每个目标有一个价值,价值等于它到地面的距离.游戏中有N个目标,每个目标从L覆盖到R,距 ...
- HDU 5919 Sequence II(可持久化线段树)
[题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=5919 [题目大意] 给出一个数列,每次查询数列中,区间非重元素的下标的中位数.查询操作强制在线. [ ...
- 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )
在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...
- HDU 4348.To the moon SPOJ - TTM To the moon -可持久化线段树(带修改在线区间更新(增减)、区间求和、查询历史版本、回退到历史版本、延时标记不下放(空间优化))
To the moon Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 2665.Kth number-可持久化线段树(无修改区间第K小)模板 (POJ 2104.K-th Number 、洛谷 P3834 【模板】可持久化线段树 1(主席树)只是输入格式不一样,其他几乎都一样的)
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 4348 To the moon 可持久化线段树,有时间戳的区间更新,区间求和
To the moonTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.a ...
随机推荐
- android控制系统音量
body_sb=(SeekBar)root.findViewById(R.id.body_sb);audioManager=(AudioManager)getActivity().getSystemS ...
- Adaptive Backgrounds – jQuery 自适应背景插件
Adaptive Backgrounds 是一款很特别的 jQuery 插件,可以从图像中提取主导颜色并将它应用到它的父元素.这个插件利用 Canvas 元素和 ImageData 对象.需要注意的是 ...
- 使用nodejs+express+socketio+mysql搭建聊天室
使用nodejs+express+socketio+mysql搭建聊天室 nodejs相关的资料已经很多了,我也是学习中吧,于是把socket的教程看了下,学着做了个聊天室,然后加入简单的操作mysq ...
- (转)高性能JavaScript:加载和运行(动态加载JS代码)
浏览器是如何加载JS的 当浏览器遇到一个<script>标签时,浏览器首先根据标签src属性下载JavaScript代码,然后运行JavaScript代码,继而继续解析和翻译页面.如果需要 ...
- 浅析字符串操作方法slice、substr、substring及其IE兼容性
在截取字符串时常常会用到substr().substring().slice()方法,有时混淆之间的用法,故总结下. slice() 定义:接受一个或者两个参数,第一个参数指定子字符串的开始位置. ...
- SharePoint 2013 Silverlight中使用Net客户端对象模型
1.创建Silverlight时,选择Silverlight 4,不要选择版本5,试了很久版本5都调用不了,自己也不知道什么原因,谷歌也没找到答案,后来尝试版本4,可以调用: 至于Host the S ...
- 在Kali Linux下安装与配置OpenVas
以下是我的Kali版本号: 执行安装命令: apt-get install openvas 安装完成后可以在漏洞分析菜单下查看到OpenVas相关选项. 以下为安装完成后需要用到的几个命令: open ...
- iOS使用Charles(青花瓷)抓包并篡改返回数据图文详解
写本文的契机主要是前段时间有次用青花瓷抓包有一步忘了,在网上查了半天也没找到写的完整的教程,于是待问题解决后抽时间截了图,自己写一遍封存在博客园中以便以后随时查阅. charles又名青花瓷,在iOS ...
- android 浅谈Aidl 通讯机制
服务端: 首先是编写一个aidl文件,注意AIDL只支持方法,不能定义静态成员,并且方法也不能有类似public等的修饰符:AIDL运行方法有任何类型的参数和返回值,在java的类型中,以下的类型使用 ...
- Java 线程池
系统启动一个线程的成本是比较高的,因为它涉及到与操作系统的交互,使用线程池的好处是提高性能,当系统中包含大量并发的线程时,会导致系统性能剧烈下降,甚至导致JVM崩溃,而线程池的最大线程数参数可以控制系 ...