题意

求区间l~r的a[l]%a[l+1]%……%a[r]的值

思路

因为取模的变化是很快的,所以线段树查找区间内第一个小于等于a[l]的数的位置,更新ans后继续查找即可。

注意查询满足某种条件的位置要这样写:

int query(int L,int R,int l,int r,int rt,int x)

{

if(mi[rt]>x) return inf;

if(l>R||r<L) return inf;

if(lr) return l;

int m=(l+r)>>1;

int ans=inf;

ans=min(ans,query(L,R,l,m,rt<<1,x));

if(ansinf)

ans=min(ans,query(L,R,m+1,r,rt<<1|1,x));

return ans;

}

代码

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int N=1e5+5;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
int mi[N<<2],add[N<<2];
int a[N],n;
void pushUp(int rt)
{
mi[rt]=min(mi[rt<<1],mi[rt<<1|1]);
}
void build(int l,int r,int rt)
{
if(l==r)
{
mi[rt]=a[l];
return;
}
int m=(l+r)>>1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
pushUp(rt);
}
int query(int L,int R,int l,int r,int rt,int x)
{
// cout<<"gg"<<endl;
if(mi[rt]>x) return inf;
if(l>R||r<L) return inf;
if(l==r) return l;
int m=(l+r)>>1;
int ans=inf;
ans=min(ans,query(L,R,l,m,rt<<1,x));
if(ans==inf)
ans=min(ans,query(L,R,m+1,r,rt<<1|1,x));
return ans;
}
int main()
{
int n;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
}
build(1,n,1);
int q;
scanf("%d",&q);
while(q--)
{
int l,r;
scanf("%d%d",&l,&r);
int ans=a[l];
while(l<=r)
{
int pos=query(l+1,r,1,n,1,ans);
if(pos==inf)
{
break;
}
ans%=a[pos];
if(ans==0)
break;
l=pos;
// cout<<pos<<endl;
}
printf("%d\n",ans);
}
}
return 0;
}

最新文章

  1. hmtl的标签属性
  2. Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper 树状数组暴力更新
  3. windows搭建redis记录
  4. 浅谈ServletContext
  5. 【学习进步之路】-【浏览器兼容】透明背景图IE、360浏览器不兼容
  6. maven profile切换正式环境和测试环境
  7. [LeetCode] 1-bit and 2-bit Characters 一位和两位字符
  8. jquery datatable 实例操作
  9. 1、python基础
  10. textarea跟随内容自动伸缩高度实现方案
  11. GitHub提供服务简介
  12. 写个shell脚本依次运行每个程序半小时
  13. loadrunner场景报错:Error: CCI compilation error -/tmp/brr_5d65oV/netdir/E/\320\324/Action.c (318): undeclared identifier `LAST&#39;解决思路
  14. NodeMCU入门(1):刷入At固件,透传数据到TcpServer和Yeelink平台
  15. Eloquent JavaScript #04# Objects and Arrays
  16. ftell
  17. find 以及linux 和windows 文件互传
  18. laravel从5.2到5.5从入门到精通视频教程共16套
  19. gdb调试报错:Missing separate debuginfos, use: debuginfo-install glibc-XXX
  20. CI框架------codeIgniter

热门文章

  1. springboot项目jar包运行
  2. 首次使用gradle出现Could not find method leftShift() for arguments解决办法
  3. MySQL实战45讲学习笔记:第四十讲
  4. node启动服务后,窗口不能关闭。pm2了解一下
  5. win10 配置python3虚拟环境
  6. 物联网架构成长之路(35)-利用Netty解析物联网自定义协议
  7. Erlang基础2
  8. 环境变量对于 VS 有什么用?
  9. 入门理解mysql-binlog
  10. 【前端知识体系-JS相关】组件化和React