QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#594340 | #9250. Max GCD | gaofeng | TL | 7ms | 34744kb | C++23 | 4.1kb | 2024-09-27 21:44:28 | 2024-09-27 21:44:29 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>PII;
const int N=1e6+10;
const int mod=998244353;
const int INF = 0x3f3f3f3f;
const ll INFll = 0x3f3f3f3f3f3f3f3f;
#define endl "\n"
#define x first
#define y second
vector<vector<int>>adj(N);
int a[N];
struct Node{
int l, r, w;
}query[N];
int ans[N];
struct gfseg { // 区间最值
int a[N], _n;
struct Node {
int l, r;
int max;
}tr[N];
void pushup(int u) {
tr[u].max = max(tr[u << 1].max, tr[u << 1 | 1].max);
}
void build(int u, int l, int r) {
if(l == r) {
tr[u] = {l, r, a[l]};
return;
}
int mid = l + r >> 1;
tr[u] = {l, r, -INF};
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
void init(vector<int> _a) {
_n = _a.size();
for(int i = 1; i <= _n; i ++) {
a[i] = _a[i - 1];
}
build(1, 1, _n);
}
void modify(int u, int x, int y) {
if(tr[u].l == tr[u].r && tr[u].l == x) {
tr[u].max = max(tr[u].max, y);
return;
}
int mid = tr[u].l + tr[u].r >> 1;
if(x <= mid) modify(u << 1, x, y);
if(x > mid) modify(u << 1 | 1, x, y);
pushup(u);
}
int querymax(int u, int l, int r) {
if(l <= tr[u].l && tr[u].r <= r) {
return tr[u].max;
}
int res = -INF;
int mid = tr[u].l + tr[u].r >> 1;
if(l <= mid) res = max(querymax(u << 1, l, r), res);
if(r > mid) res = max(querymax(u << 1 | 1, l, r), res);
return res;
}
void debug() {
for(int i = 1; i <= _n; i ++)
cout << querymax(1, i, i) << " "; cout << endl;
}
}tr;
void solve()
{
int n, q; cin >> n >> q;
for(int i = 1; i <= n; i ++) {
cin >> a[i];
// a[i] = rand();
int x = a[i];
for(int j = 1; j * j <= x; j ++) {
if(x % j == 0) {
adj[j].push_back(i);
if(j * j != x)
adj[x/j].push_back(i);
}
}
}
vector<Node> no;
for(int i = 1; i < N; i ++) {
int l = 0, r = 0;
for(int l = 0; l < adj[i].size(); l ++) {
while(r <= l + 1 && r < adj[i].size()) r ++;
while(r < adj[i].size()) {
if(r < adj[i].size() && adj[i][l + 1] - adj[i][l] <= adj[i][r] - adj[i][l + 1]) {
no.push_back({adj[i][l], adj[i][r], i});
break;
}
else r ++;
}
}
}
// cout << no.size() << endl;
// for(auto [l, r, w]:no) cout << l << " " << r << " " << w << endl;
vector<int>T; for(int i = 1; i <= n + 10; i ++) T.push_back(0); tr.init(T);
for(int i = 0; i < q; i ++) {
cin >> query[i].l >> query[i].r; query[i].w = i;
}
sort(query , query + q, [&](Node a, Node b){
return a.l > b.l;
});
sort(no.begin(), no.end(), [&](Node a, Node b){
if(a.l == b.l) return a.w > b.w;
return a.l > b.l;
});
vector<int>mx (n + 10, 0);
int i = 0, j = 0;
for(int L = n; L >= 0; L --) {
// cout << L << ":\n";
while(i < no.size() && no[i].l >= L) {
// cout << no[i].l << " " << no[i].r << " " << no[i].w << endl;
if(mx[no[i].r] < no[i].w) {
tr.modify(1, no[i].r, no[i].w);
mx[no[i].r] = no[i].w;
}
i ++;
}
while(j < q && query[j].l >= L) {
ans[query[j].w] = tr.querymax(1,1,query[j].r);
j ++;
}
}
// tr.debug();
// cout << i << " " << j << endl;
for(int i = 0; i < q; i ++) cout << ans[i] << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cout << setprecision(11) << fixed;
int t;t=1;
//cin>>t;
for(int i=1;i<=t;i++){
//printf("Case %d: ",i);
solve();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 7ms
memory: 34744kb
input:
8 8 8 24 4 6 6 7 3 3 1 5 2 6 3 7 5 8 4 8 1 3 2 5 3 8
output:
4 2 3 1 3 4 2 3
result:
ok 8 tokens
Test #2:
score: -100
Time Limit Exceeded
input:
150000 100000 982800 554400 665280 982800 997920 720720 786240 831600 997920 982800 786240 982800 942480 831600 887040 665280 831600 960960 786240 982800 786240 942480 665280 831600 942480 665280 982800 960960 960960 997920 720720 960960 960960 665280 982800 665280 982800 942480 786240 997920 554400...