The method and application of Hdu 5869 interval GCD

Source: Internet
Author: User
Tags gcd



Test instructions: Sequence of length n, m query interval [L, R], how many different gcd values of all contiguous segments within the interval are asked.



Exercises



1. Because the n number of GCD equals the number of the previous N-1 GCD value and then the nth number GCD the value, plus if the fixed end point, the interval extends forward more gcd must be non-strict decrement, so we can preprocess each number as the end of all the suffix of the GCD, Every time I find the number of the first end of the record after all the GCD value and the number of i+1 gcd, this is good to do.



2. Sort all queries from small to large in the right interval, and update the values of the first step in the tree array, then use the tree array interval to sum up, this step is explained in the code.


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define PF(x) cout << "debug: " << x << " ";
#define EL cout << endl;
#define PC(x) puts(x);
typedef long long ll;
#define CLR(x, v) sizeof (x, v, sizeof(x))
using namespace std;
const int INF = 0x5f5f5f5f;
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;
const double eps = acos(-1);
const double PI= atan(1.0)*4;
int n,q,a[maxn],tree[maxn],vis[2000000],ans[maxn];
vector<pair<int,int> >gd[maxn];
Struct st{
Int l, R, ID;
}qer[maxn];
int gcd(int a,int b){
return b == 0?a:gcd(b,a%b);
}
bool cmp(st x,st y){
return x.r < y.r;
}
void Add(int k,int num){
while(k <= n){
tree[k] += num;
K + = k&amp; (-k);
}
}
int Sum(int k){
Int sum = 0;
while(k > 0){
sum += tree[k];
K = k&amp; (-k);
}
Return sum;
}
Int main () {
freopen("in.txt","r",stdin);
while(~scanf("%d%d",&amp;n,&amp;q)){
for(int i = 1;i <= n;i++){
scanf("%d",&amp;a[i]);
gd[i].clear();
}
for(int i = 1;i <= n;i++){
int x = a[i],y = i;
for(int j = 0;j <gd[i-1].size();j++){
int t = gcd(gd[i-1][j].first,a[i]);
If (t = x) {
gd[i].push_back(make_pair(x,y));
X = t, y = GD [I-1] [J]. Second; / / X records the GCD value from the Y position to the I position of the current processing. Y is the rightmost position in the same GCD. The rightmost position is required
}
}
gd[i].push_back(make_pair(x,y));
}
for(int i = 1;i <= q;i++){
scanf("%d%d",&amp;qer[i].l,&amp;qer[i].r);
qer[i].id = i;
}
sort(qer + 1,qer + 1 + q,cmp);
memset(vis,0,sizeof(vis));
memset(tree,0,sizeof(tree));
Int len = 1;
for(int i = 1; i <= n;i++){
// cout<<i<<":";
for(int j = 0;j < gd[i].size();j++){
// cout<<gd[i][j].first<<"->"<<gd[i][j].second<<" ";
int c1 = gd[i][j].first,c2 = gd[i][j].second;
//If the GCD value C1 appears, its position will be updated. According to the non strict decrement, it will be updated to the position closest to the current I
if(vis[c1] > 0)
Add(vis[c1],-1);
vis[c1] = c2;
Add (c2,1);
}
// cout<<endl;
while(qer[len].r == i){
ans[qer[len].id] = Sum(qer[len].r) - Sum(qer[len].l-1);
Len++;
}
}
for(int i = 1;i <= q;i++)
printf("%d\n",ans[i]);
}
Return 0;
} 


You can refer to the blog http://blog.csdn.net/angon823/article/details/52503408



The method and application of Hdu 5869 interval GCD


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.