QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#117567 | #6405. Barkley | installb# | WA | 2ms | 13192kb | C++14 | 2.0kb | 2023-07-01 18:05:51 | 2023-07-01 18:05:53 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define M 100005
using ll = long long;
using pli = pair<ll,int>;
vector<pli>pre[M],nex[M];
ll A[M];
int n,q;
void update(int flg){
vector<pli>t;
for(int i=1;i<=n;i++){
t.push_back(pli(A[i],i));
vector<pli>tmp;
for(int j = (int)t.size()-1;j>=0;j--){
int k = j;
ll v = __gcd(A[i],t[j].first);
while(k>=1&& v == __gcd(A[i],t[k-1].first)) k--;
tmp.push_back(pli(v,t[k].second));
j = k;
}
t.clear();
for(int j = (int)tmp.size()-1;j>=0;j--)t.push_back(tmp[j]);
if(!flg) for(auto v: tmp)pre[i].push_back(v);
else for(auto v: tmp)nex[n-i+1].push_back(pli(v.first,n-v.second+1));
}
}
ll st[M][18];
int Log[M];
void Init(){
for(int i=1;i<=n;i++)st[i][0] = A[i];
for(int k=1;k<18;k++)
for(int i=1;i+(1<<k)-1<=n;i++)
st[i][k] = __gcd(st[i][k-1],st[i+(1<<k-1)][k-1]);
for(int i=2;i<=n;i++)Log[i]=Log[i>>1]+1;
}
ll Ask(int l,int r){
if(l>r)return 0;
int k = Log[r-l+1];
return __gcd(st[l][k],st[r-(1<<k)+1][k]);
}
ll Ask_k(int l,int r, int k, ll val){
assert(r-l>=k-1);
if(k==0) return __gcd(Ask(l,r),val);
ll ret = Ask_k(l+1,r,k-1,val);
for(int i = 0; i<nex[l].size();i++){
int j = i;
ll vt = __gcd(val,nex[l][i].first);
while(j+1 < nex[l].size() && __gcd(val,nex[l][j+1].first) == vt) j++;
if(nex[l][j+1].second + k < r){
ret = max(ret,Ask_k(nex[l][j+1].second + 2, r, k-1, __gcd(vt, val)));
}else{
ret = max(ret, vt);
break;
}
i=j;
}
return ret;
}
int main(){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)scanf("%lld",&A[i]);
Init();
update(0);
reverse(A+1,A+1+n);
update(1);
reverse(A+1,A+1+n);
while(q--){
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
printf("%lld\n",Ask_k(l,r,k,0));
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 11256kb
input:
4 4 3 2 6 4 1 3 1 2 4 1 1 4 2 1 4 3
output:
3 2 3 6
result:
ok 4 number(s): "3 2 3 6"
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 13192kb
input:
100 10000 7 25 33 17 13 33 24 29 11 1 3 19 2 20 33 23 14 24 15 12 3 1 5 13 6 35 15 21 10 34 31 19 7 33 17 26 26 1 19 21 31 5 29 20 18 32 19 18 19 31 11 26 6 19 2 26 23 1 4 2 31 21 29 30 1 14 20 23 14 32 4 34 13 29 5 26 24 29 28 5 26 26 21 19 2 33 2 31 30 3 23 24 26 32 36 21 21 11 5 9 56 57 1 90 97 1...
output:
26 3 26 19 26 19 21 29 31 3 21 3 23 3 26 3 19 26 23 5 3 29 11 7 29 19 19 11 26 2 24 19 4 24 2 21 19 2 3 3 21 2 2 1 2 31 23 4 26 33 2 21 3 26 3 1 23 19 2 21 31 36 17 19 23 19 29 2 2 2 17 2 11 2 2 21 1 5 21 2 2 19 2 26 33 19 32 2 2 25 23 17 30 23 2 7 26 29 7 29 33 2 13 21 35 2 2 19 33 3 31 11 17 2 26 ...
result:
wrong answer 2nd numbers differ - expected: '1', found: '3'