【线段树】Gym - 101201J - Shopping
题意:n个数,m次询问,每次给你一个询问v,l,r,问你v%a[l]%a[l+1]%...%a[r]是多少。
a%b,结果要么不变,要么至少缩小到a的一半,于是用线段树,每次询问当前区间最靠左侧的小于等于当前数的值是多少,只需不超过log次询问就能使该数模完,就行了。
O(n(logn)^2)。
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
int n,m;
ll minv[800005],a[200005];
void buildtree(int rt,int l,int r){
if(l==r){
scanf("%I64d",&minv[rt]);
a[l]=minv[rt];
return;
}
int m=(l+r>>1);
buildtree(rt<<1,l,m);
buildtree(rt<<1|1,m+1,r);
minv[rt]=min(minv[rt<<1],minv[rt<<1|1]);
}
int pos;
int find(ll v,int rt,int l,int r){
if(l==r){
return l;
}
int m=(l+r>>1);
if(minv[rt<<1]<=v){
return find(v,rt<<1,l,m);
}
else{
return find(v,rt<<1|1,m+1,r);
}
}
bool query(int ql,int qr,ll v,int rt,int l,int r){
if(ql<=l && r<=qr){
if(minv[rt]<=v){
pos=find(v,rt,l,r);
return 1;
}
return 0;
}
int m=(l+r>>1);
if(ql<=m){
if(query(ql,qr,v,rt<<1,l,m)){
return 1;
}
}
if(m<qr){
if(query(ql,qr,v,rt<<1|1,m+1,r)){
return 1;
}
}
return 0;
}
int main(){
// freopen("j.in","r",stdin);
ll z;
int x,y;
scanf("%d%d",&n,&m);
buildtree(1,1,n);
for(int i=1;i<=m;++i){
scanf("%I64d%d%d",&z,&x,&y);
while(x<=y && query(x,y,z,1,1,n)){
z%=a[pos];
x=pos+1;
}
printf("%I64d\n",z);
}
return 0;
}
【线段树】Gym - 101201J - Shopping的更多相关文章
- Gym 101201J Shopping (线段树+取模)
题意:给定 n 个物品,然后有 m 个人买东西,他们有 x 元钱,然后从 l - r 这个区间内买东西,对于每个物品都尽可能多的买,问你最少剩下多少钱. 析:对于物品,尽可能多的买的意思就是对这个物品 ...
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- Codeforces Gym 100513F F. Ilya Muromets 线段树
F. Ilya Muromets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/probl ...
- Codeforces GYM 100114 D. Selection 线段树维护DP
D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...
- 【线段树】BAPC2014 E Excellent Engineers (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)
题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子 ...
- Codeforces Gym 100733J Summer Wars 线段树,区间更新,区间求最大值,离散化,区间求并
Summer WarsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.a ...
- 【拓扑排序】【线段树】Gym - 101102K - Topological Sort
Consider a directed graph G of N nodes and all edges (u→v) such that u < v. It is clear that this ...
随机推荐
- bzoj 1305 二分+最大流判定|贪心
首先我们二分一个答案mid,在判定是否能举办mid次,那么对于每个次我们可以用最大流根据是否满流(流量为n*mid)来判定,对于每个点我们拆成两个点,分别表示这个人要和他喜欢和不喜欢的人一起跳舞,那么 ...
- jsp之jstl核心标签库
JSTL核心标签库技术 1. JSTL介绍 在JSP页面中即可书写html,也可以书写Java代码,导致页面混乱,维护,修改,升级难度加大,于是国际上不同的公司在实际应用中,根据页面的需求将Java代 ...
- ”未能加载文件或程序集“Oracle.DataAccess”或它的某一个依赖项
引用:http://www.cnblogs.com/joey0210/archive/2012/09/29/2708420.html 上一篇文章说到了DLL引用问题,主要是说的程序中如果使用过了反射, ...
- Android--hardwareAccelerated 硬件加速详解 android:largeHeap="true"
做项目时,引导页面的ViewPager报了OOM异常,图片并不大,在清单文件Application节点中添加了两行代码就解决了这个问题 android:hardwareAccelerated=&quo ...
- fragment+tabhost与viewpager
学到哪里写到哪里吧 A.viewpager a.用V4包中的fragment,activity继承FragmentActivity b.布局中加入<android.support.v4.view ...
- 4.0docker部署
设置容器的端口映射 -P :容器暴露的所有端口映射 -p :指定映射容器暴露的端口 Nginx部暑流程 docker run -p 80 --name web -t -i ubuntu /bin/b ...
- /proc/diskstats文件注解
/proc/diskstats 注解 今儿在准备利用shell监控磁盘读写次数等信息时,看到该文件,但是又不清楚每段的具体含义,这里备注下. 文件内容 [root@namenode proc]# ca ...
- 【Python学习笔记】Coursera之PY4E学习笔记——String
1.字符串合并 用“+”来进行字符串的合并,注意空格是要自己加的. 例: >>> a='Hello' >>> b= a+ 'There' >>> ...
- 12-7 NSDictionary
原文:http://rypress.com/tutorials/objective-c/data-types/nsdictionary NSDictionary 如同NSSet,NSDictionar ...
- 在ubuntu上配置LAMP架构
1. 安装MySQL /* ubuntu默认进入系统是普通用户 所以在真实工作中,我们会得到root的授权. 所以我们需要用sudo做一切只有root才能完成的操作. */ [root@LAMP ~] ...