题意: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的更多相关文章

  1. Gym 101201J Shopping (线段树+取模)

    题意:给定 n 个物品,然后有 m 个人买东西,他们有 x 元钱,然后从 l - r 这个区间内买东西,对于每个物品都尽可能多的买,问你最少剩下多少钱. 析:对于物品,尽可能多的买的意思就是对这个物品 ...

  2. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

  3. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  4. Codeforces Gym 100513F F. Ilya Muromets 线段树

    F. Ilya Muromets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/probl ...

  5. Codeforces GYM 100114 D. Selection 线段树维护DP

    D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...

  6. 【线段树】BAPC2014 E Excellent Engineers (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  7. K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)

    题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子 ...

  8. Codeforces Gym 100733J Summer Wars 线段树,区间更新,区间求最大值,离散化,区间求并

    Summer WarsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.a ...

  9. 【拓扑排序】【线段树】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 ...

随机推荐

  1. Android应用程序App应用上线流程

    对于很多初级开发者,可能对app应用上线不太了解,本文跟大家介绍一下怎么上线app应用.上线App并不是一件很困难的事情,App的应用功能也不需要很强大,甚至不用联网,只有简单的一两个页面的App应用 ...

  2. Part2-HttpClient官方教程-Chapter5-流利的API

    5.1. 易于使用的Facade API 使用之前注意引入相应Jar包或者Maven依赖 <dependency> <groupId>org.apache.httpcompon ...

  3. Tomcat8配置默认项目

    <!-- 配置默认访问项目 --> <Host name="localhost" appBase="webapps" unpackWARs=& ...

  4. LINUX-内核-中断分析-中断向量表(3)-arm【转】

    转自:http://blog.csdn.net/haolianglh/article/details/51986987 arm中断概念 在<ARM体系结构与编程>第9章中说到,ARM 中有 ...

  5. linux pthread【转】

    转自:http://www.cnblogs.com/alanhu/articles/4748943.html Posix线程编程指南(1) 内容:  一. 线程创建  二.线程取消 关于作者  线程创 ...

  6. arm---先搞清楚各种版本号【转】

    转自:http://blog.csdn.net/linnoo/article/details/53214689 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] ARM的几种版本 ...

  7. 自动化测试===requests+unittest+postman的接口测试

    postman是一个跨平台的接口测试工具,下载链接在这里:https://www.getpostman.com/ unittest是一个单元测试框架,python中安装:pip install uni ...

  8. Dev的js智能提示

    VS中"工具"->"选项"->"文本编辑器"->"JavaScript"->"Inte ...

  9. [Think] position与anchorPoint关系

    1.简介: 最近在学习动画,所以接触到了CAlayer类. 其中的position与anchorPoint关系一感觉一头雾水,网上有篇文章 彻底理解position和anchorPoint关系  里面 ...

  10. JavaSE项目之聊天室swing版

    引子: 当前,互联网 体系结构的参考模型主要有两种,一种是OSI参考模型,另一种是TCP/IP参考模型. 一.OSI参考模型,即开放式通信系统互联参考模型(OSI/RM,Open Systems In ...