HDU 4630 No Pain no Game (segment tree + offline operation)

Source: Internet
Author: User
Tags greatest common divisor

No Pain no GameTime limit:4000/2000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 1769 Accepted Submission (s): 748


Problem Descriptionlife is a Game,and your lose it,so you suicide.
But you can not kill yourself before your solve this problem:
Given you a sequence of number A1, A2, ..., an. They is also a permutation of 1...N.
You need to answer some queries,each with the following format:
If we chose the number a A, a (shouldn ' t be the same) from interval [L, R],what is the maximum gcd (A, b)? If there's no-to choose-distinct number (L=R) then the-answer is zero.
Inputfirst line contains a number T (T <= 5), denote the number of test cases.
Then follow T test cases.
For each test cases,the first line contains a number n (1 <= n <= 50000).
The second line contains n number a1, A2, ..., an.
The third line contains a number Q (1 <= Q <= 50000) denoting the number of queries.
Then Q lines Follows,each lines contains the integer l, r (1 <= l <= r <= N), denote a query.
Outputfor each test cases,for each query print the answer on one line.
Sample Input
1108 2 4 9 5 7 10 6 1 352 102 46 91 47 10

Sample Output
52243

Authorwjmzbmr
Source2013 multi-university Training Contest 3
Recommendzhuyuanchen520 | We have carefully selected several similar problems for you:5283 5282 5280 5279 5278

Test instructions: There are n numbers, which is an arrangement of the 1~n. There are m inquiries, each asking an interval, asking from this interval, take two numbers of the largest greatest common divisor.

First, the query by the right interval in ascending order, in the array is inserted sequentially, record the current number of factors where the occurrence of the position, if there is a previous occurrence of these two factors appear

There are two numbers in the public approximate number is it, with the segment tree maintenance interval approximate maximum can be.

#include <cstring> #include <cstdio> #include <algorithm> #include <iostream> #include < cmath> #include <vector> #define Lson idx<<1,l,mid#define rson idx<<1|1,mid+1,r#define N 50050# Define LC idx<<1#define RC idx<<1|1using namespace std;int n,q,flag;int a[n],tree[n*4];int L[N],R[N];int    FIRST[N],ANS[N];VECTOR&LT;INT&GT;VEC;STRUCT node {int id; int l,r;}    Q[n];bool CMP (node A,node b) {if (A.R==B.R) return a.l<b.l; return A.R&LT;B.R;}    All factors void FJ (int x) {vec.clear () are obtained;            for (int i=1; i*i<=x; i++) {if (x%i==0) {vec.push_back (i);        if (x/i!=i) vec.push_back (x/i); }}}void push_up (int idx) {Tree[idx]=max (TREE[LC],TREE[RC]);}    void build (int idx,int l,int r) {tree[idx]=0;    if (l==r) {return;    } int mid= (L+R) >>1;    Build (Lson); Build (Rson);} void update (int idx,int l,int r,int x,int v) {//x value is changed to V if (l==r) {if (tree[idx]<v) Tree[idx]=v;    Return    } int mid= (L+R) >>1;    if (x<=mid) update (LSON,X,V);    else update (RSON,X,V); PUSH_UP (idx);}    int query (int idx,int l,int r,int x,int y) {if (l>=x&&y>=r) {return TREE[IDX];    } int ans=0;    int mid= (L+R) >>1;    if (x<=mid) {Ans=max (Ans,query (lson,x,y));    } if (Y>mid) {Ans=max (Ans,query (rson,x,y)); } return ans;    void Debug () {for (int i=0; i<vec.size (); i++) {printf ("%d", vec[i]); } Cout<<endl;}    int main () {//freopen ("test.in", "R", stdin);    int t;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        for (int i=1; i<=n; i++) {scanf ("%d", &a[i]);        } scanf ("%d", &q);            for (int i=1; i<=q; i++) {scanf ("%d%d", &AMP;Q[I].L,&AMP;Q[I].R);        Q[i].id=i;        } sort (q+1,q+1+q,cmp);        memset (first,0,sizeof first);        memset (l,0,sizeof L); MEmset (r,0,sizeof R);        Build (1,1,n);        Preprocessing the same interval of the left and right interval int f=1;        L[q[f].r]=f;        R[q[f].r]=f; for (int i=1; i<=q;)            {while (q[i].r==q[f].r&&i<=q) {i++;            } l[q[f].r]=f;            R[q[f].r]=i-1;        F=i;            } for (int i=1; i<=n; i++) {//FJ (a[i]);            Debug ();            int xx=a[i]; for (int k=1; k*k<=xx; k++) {if (xx%k==0) {if (!first[k]) {Fir                    St[k]=i;                        } else {update (1,1,N,FIRST[K],K);                    First[k]=i;                    } int kk=xx/k;                        if (K!=KK) {if (!first[kk]) {first[kk]=i;                            } else {update (1,1,N,FIRST[KK],KK);                        First[kk]=i;            }        }}} int x=l[i],y=r[i]; if (x==0| |            y==0) continue;                for (int j=x; j<=y; J + +) {int k=q[j].l;                if (k==i) {ans[q[j].id]=0;                } else {ans[q[j].id]=query (1,1,n,k,i);        }} if (y==q) break;        } for (int i=1; i<=q; i++) {printf ("%d\n", Ans[i]); }} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 4630 No Pain no Game (segment tree + offline operation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.