首页 > 其他分享 >HDU 5875 Function

HDU 5875 Function

时间:2022-11-09 19:36:41浏览次数:50  
标签:Function HDU return rr int ll 5875 include define


Problem Description


The shorter, the simpler. With this problem, you should be convinced of this truth.
  
  You are given an array  A of  N postive integers, and  M queries in the form  (l,r). A function  F(l,r) (1≤l≤r≤N) is defined as:
F(l,r)={AlF(l,r−1) modArl=r;l<r.
You job is to calculate  F(l,r), for each query  (l,r).


 



Input


There are multiple test cases.
  
  The first line of input contains a integer  T, indicating number of test cases, and  T test cases follow. 
  
  For each test case, the first line contains an integer  N(1≤N≤100000).
  The second line contains  N space-separated positive integers:  A1,…,AN (0≤Ai≤109).
  The third line contains an integer  M denoting the number of queries. 
  The following  M lines each contain two integers  l,r (1≤l≤r≤N), representing a query.


 



Output


(l,r), output  F(l,r)


 



Sample Input


1 3 2 3 3 1 1 3


 



Sample Output


2



求一个区间最左端的数对于右端的全部数字取模,由于每次取模至少减半,所以取模的次数不会很多,


用线段树来寻找右端小于等于当前值的第一个数字即可。


#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define ff first
#define ss second
#define mp(i,j) make_pair(i,j)
#define pb push_back
#define pii pair<int,int>
#define in(x) scanf("%d", &x);
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
int T, n, m,l,r;
int a[N],f[N<<2];

void build(int x,int l,int r)
{
if (l==r) {in(f[x]); a[l]=f[x];}
else
{
int mid=l+r>>1;
build(lson); build(rson);
f[x]=min(f[x<<1],f[x<<1|1]);
}
}

int find(int x,int l,int r,int ll,int rr,int u)
{
if (f[x] > u) return rr+1;
if (ll<=l&&r<=rr)
{
if (l==r) return l;
int mid=l+r>>1;
if (f[x<<1]<=u) return find(lson,ll,rr,u);
else return find(rson,ll,rr,u);
}
else
{
int mid=l+r>>1,res;
if (ll<=mid)
{
res=find(lson,ll,rr,u);
if (res<=rr) return res;
}
if (rr>mid)
{
res=find(rson,ll,rr,u);
if (res<=rr) return res;
}
return rr+1;
}
}

int main()
{
in(T);
while (T--)
{
scanf("%d",&n);
build(1,1,n);
scanf("%d",&m);
while (m--)
{
scanf("%d%d",&l,&r);
int ans=a[l];
while (l < r)
{
int q = find(1,1,n,l+1,r,ans);
if (q<=r) ans%=a[q]; l=q;
}
printf("%d\n",ans);
}
}
return 0;
}




 


标签:Function,HDU,return,rr,int,ll,5875,include,define
From: https://blog.51cto.com/u_15870896/5838611

相关文章

  • HDU 5320 Fan Li
    DescriptionFanLi(范蠡)wasanancientChineseadvisorinthestateofYueintheSpringandAutumnperiod.Heisasuccessfulmilitaristandasuccessf......
  • HDU 5399 Too Simple
    ProblemDescriptionRhasonCheunghadasimpleproblem,andaskedTeacherMaiforhelp.ButTeacherMaithoughtthisproblemwastoosimple,sometimesna......
  • HDU 5402 Travelling Salesman Problem
    ProblemDescriptionn rowsand m columns.Thereisanon-negativenumberineachcell.TeacherMaiwantstowalkfromthetopleftcorner (1,1......
  • HDU 1272 小希的迷宫
    ProblemDescription上次Gardon的迷宫城堡小希玩了很久(见ProblemB),现在她也想设计一个迷宫让Gardon来走。但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向......
  • HDU 2242 考研路茫茫——空调教室
    ProblemDescription众所周知,HDU的考研教室是没有空调的,于是就苦了不少不去图书馆的考研仔们。Lele也是其中一个。而某教室旁边又摆着两个未装上的空调,更是引起人们无......
  • HDU 4041 Eliminate Witches!
    ProblemDescriptionKanameMadokaisaMagicalGirl(MahouShoujo/PuellaMagi).ThedutyofaMagicalGirlistoeliminateWitches(Majo).Thoughsoundshorrif......
  • HDU 1237 简单计算器
    ProblemDescription读入一个只包含+,-,*,/的非负整数计算表达式,计算该表达式的值。Input测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字......
  • HDU 4739 Zhuge Liang's Mines
    ProblemDescriptionIntheancientthreekingdomperiod,ZhugeLiangwasthemostfamousandsmartestmilitaryleader.HisenemywasShimaYi,whoalways......
  • HDU 3608 最长回文
    ProblemDescription给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.回文就是正反读都是一样的字符串,如aba,abba等 Inp......
  • HDU 5591 ZYB's Game
    ProblemDescription withhisclassmatesinhiking:ahostkeepsanumberin  loses,orthehostwilltellalltheplayersthatthenumbernowisbigger......