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 ...
随机推荐
- Unsupported configuration attributes: [FILE_UPLOAD]
Caused by: java.lang.IllegalArgumentException: Unsupported configuration attributes: [FILE_UPLOAD] 情 ...
- AES .net 、JS 相互加密解密
/// <summary> /// AES加密 /// </summary> public class AES { /// <summary> /// 加密 /// ...
- [转]Dcloud App离线本地存储方案
原文地址:http://ask.dcloud.net.cn/article/166 HTML5+的离线本地存储有如下多种方案:HTML5标准方案:cookie.localstorage.session ...
- [deviceone开发]-毛玻璃效果示例
一.简介 do_Bitmap组件可以把图片加载为内存里的Bitmap对象,能够对这个对象做各种图形化处理.目前只有3种处理,圆角,毛玻璃,灰度.以后会添加更多. 二.效果图 三.相关下载 https: ...
- 腾讯用过的插件jQuery twentytwenty 效果对比
在线实例 左右对比 上下对比 使用方法 <div class="twentytwenty-container"> <img src="/api/ ...
- CSAW2013
竞赛地址:https://ctf.isis.poly.edu/challenges/ 第一关:Trivia Trivia意思为琐事,每题分值50,比较简单 1.Drink all the booze, ...
- arcgis批量处理mxd定义服务中的路径
>>> from arcpy import env... env.workspace=r"c:\165mxd"... out = r"c:\166mx ...
- 1分钟实现Autodesk Vault登录对话框
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courie ...
- Android Handler机制(四)---Handler源码解析
Handler的主要用途有两个:(1).在将来的某个时刻执行消息或一个runnable,(2)把消息发送到消息队列. 主要依靠post(Runnable).postAtTime(Runnable, l ...
- HandlerThread
一.概念 1.Android中Handler的使用,一般都在UI主线程中执行,因此在Handler接收消息后,处理消息时,不能做一些很耗时的操作,否则将出现ANR错误. 2.HandlerTh ...