Skip to content

Commit eb372ae

Browse files
authoredNov 18, 2022
Create Segment-Tree.cpp
1 parent 1920cd7 commit eb372ae

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
 

‎Segment-Tree.cpp

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Problem: LCM Challenge
2+
// Contest: HackerRank - CODEX 15.0
3+
// URL: https://www.hackerrank.com/contests/codex-15-0/challenges/lcm-challenge/problem
4+
// Memory Limit: 512 MB
5+
// Time Limit: 4000 ms
6+
// Author: Priyansh Singh
7+
8+
#include <bits/stdc++.h>
9+
using namespace std;
10+
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
11+
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
12+
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
13+
#define ll long long
14+
#define int long long
15+
#define ld long double
16+
#define endl "\n"
17+
#define pb push_back
18+
#define fill(a,val) memset(a,val,sizeof(a))
19+
#define ff first
20+
#define ss second
21+
#define test ll t; cin>>t; while(t--)
22+
#define loop(i,a,b) for(ll i=a;i<b;i++)
23+
#define loopr(i,a,b) for(ll i=a;i>=b;i--)
24+
#define pii pair<ll,ll>
25+
#define all(v) v.begin(),v.end()
26+
const ll mod = 1000*1000*1000+7;
27+
const ll inf = 1ll*1000*1000*1000*1000*1000*1000 + 7;
28+
const ll mod2 = 998244353;
29+
const ll N = 2000 * 1000 + 1000;
30+
const ld pi = 3.141592653589793;
31+
ll power(ll x,ll y,ll p = LLONG_MAX ){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
32+
ll ncr(ll n,ll r,ll m){if(r>n)return 0;ll a=1,b=1,i;for(i=0;i<r;i++){a=(a*n)%m;--n;}while(r){b=(b*r)%m;--r;}return (a*power(b,m-2,m))%m;}
33+
34+
ll tree[N];
35+
36+
void build(ll node,ll start,ll end,vector<ll> &a){
37+
if(start==end){
38+
tree[node]=a[start];
39+
}
40+
else{
41+
ll mid=(start+end)/2;
42+
build(2*node,start,mid,a);
43+
build(2*node+1,mid+1,end,a);
44+
tree[node]=__gcd(tree[2*node],tree[2*node+1]);
45+
}
46+
}
47+
48+
ll query(ll node,vector<ll> &a,ll start,ll end,ll l,ll r){
49+
if(start>r || l>end){
50+
return 1LL;
51+
}
52+
else if(start>=l && r>=end){ // imp condition
53+
return tree[node];
54+
}
55+
ll mid=(start+end)/2;
56+
return (ll)__gcd(query(2*node,a,start,mid,l,r), query(2*node+1,a,mid+1,end,l,r));
57+
}
58+
ll in(ll x){
59+
return power(x,mod-2,mod)%mod;
60+
}
61+
void solve(){
62+
ll n;
63+
cin>>n;
64+
vector<ll> a(n+1);
65+
vector<ll> prod(n+1);
66+
prod[0]=1;
67+
loop(i,1,n+1){
68+
cin>>a[i];
69+
prod[i]=(prod[i-1]%mod*a[i]%mod)%mod;
70+
prod[i]%=mod;
71+
}
72+
// cout<<a.size()<<endl;
73+
build(1,1,n,a); // node start end array
74+
ll q;
75+
cin>>q;
76+
while(q--){
77+
ll u,v;
78+
cin>>u>>v;
79+
// ll Lcm=((prod[v]%mod*in(prod[u-1])%mod)%mod* in()%mod)%mod;
80+
ll Lcm=prod[v];
81+
Lcm=(Lcm%mod*in(prod[u-1])%mod)%mod;
82+
Lcm=(Lcm%mod*in(query(1,a,1,n,u,v))%mod)%mod;
83+
Lcm%=mod;
84+
cout<<Lcm<<endl;
85+
}
86+
}
87+
signed main(){
88+
fastio();
89+
// cout<<fixed<<setprecision(10);
90+
test
91+
solve();
92+
return 0;
93+
}

0 commit comments

Comments
 (0)
Please sign in to comment.