HDU 5042 GCD pair 预处理+二分 分段

点击打开链接

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
ll gcd(ll x, ll y){
    if(x>y)swap(x,y);
    while(x){
        y%=x;
        swap(x,y);
    }
    return y;
}
const ll inf = 1<<30;
const double eps = 1e-8;

typedef pair<ll,ll> pii;
template <class T>
inline bool rd(T &ret) {
    char c; ll sgn;
    if(c=getchar(),c==EOF) return 0;
    while(c!='-'&&(c<'0'||c>'9')) c=getchar();
    sgn=(c=='-')?-1:1;
    ret=(c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
    ret*=sgn;
    return 1;
}
template <class T>
inline void pt(T x) {
    if (x <0) {
        putchar('-');
        x = -x;
    }
    if(x>9) pt(x/10);
    putchar(x%10+'0');
}

#define N 100005
struct node{
    ll pos, l, r, gval;
    node(ll a=0,ll b=0,ll c=0,ll d=0):pos(a),l(b),r(c),gval(d){}
    bool operator < (const node &x) const {
        if(x.gval != gval)
            return gval < x.gval;
        if(x.pos != pos)
            return pos < x.pos;
        return r < x.r;
    }
}p[N*15];

vector<node> G[N];
ll a[N], n, que, top;
pii ans;
ll sum[N*15]; ll cnt[N*15];
void precal(){
    G[n].push_back(node(n,n,n,a[n]));
    for(ll i = n-1; i; i--){
        G[i].push_back(node(i,i,i,a[i]));
        for(ll j = 0; j < G[i+1].size(); j++){
            node tmp = G[i+1][j];
            ll x = gcd(a[i], tmp.gval);
            if(x == G[i][G[i].size()-1].gval)
                G[i][G[i].size()-1].r = tmp.r;
            else G[i].push_back(node(i, tmp.l, tmp.r, x));
        }
    }
    top = 0;
    for(ll i = 1 ; i <= n; i++)
        for(ll j = 0; j < (ll)G[i].size(); j++)
            p[++top] = G[i][j];
    sort(p+1, p+top+1);
    sum[0] = 0;
    for(ll i = 1; i <= top; i++) sum[i] = sum[i-1] + (ll)p[i].r-(ll)p[i].l+1LL;
    cnt[1] = 1;
    for(ll i = 2; i <= top; i++) cnt[i] = cnt[i-1] + (p[i-1].gval!=p[i].gval);
}
ll F(ll l, ll r){
    ll z = 0, y = G[l].size()-1;
  //  putchar('&');for(ll i =0;i<G[l].size(); i++)pt(G[l][i].gval),putchar(' ');puts("");
    while(z < y)
    {
        ll mid = (z+y)>>1;
  //      prllf("(%d,%d)\n", z, y);
        if(G[l][mid].r < r)
            z = mid+1;
        else
            y = mid;
    }
    return G[l][z].gval;
}
void Rank(ll l, ll r){
 //   puts("2--");
    ll x = F(l,r);
 //   puts("**");
    node tmp = node(-inf, 0, 0, x); //1
    ll now1 = lower_bound(p+1, p+1+top, tmp) - p;
    ans.first = (ll)cnt[now1];
    tmp = node(l, l, r, x);
    ll now2 = lower_bound(p+now1, p+1+top, tmp) - p;
    ans.second = sum[now2-1] - sum[now1-1] + (ll)r - (ll)p[now2].l + 1LL;
//    puts("end2");
}
void Select(ll k1, ll k2){
 //   puts("3--");
    ans.first = -1;
    if(cnt[top] < k1)return ;
    ll L = lower_bound(cnt + 1,cnt + top + 1,k1) - cnt;
    ll R = upper_bound(cnt + 1,cnt + top + 1,k1) - cnt - 1;
    if(sum[R] - sum[L-1] < (ll)k2) return ;
    ll pos = lower_bound(sum+L, sum+R+1, (ll)k2 + sum[L-1]) - sum;
    ll tx = sum[pos] - sum[L-1] - (ll)k2;
    ans.first = (ll)p[pos].pos;
    ans.second = (ll)p[pos].r - tx;
//    puts("end3");
}
void input(){
    rd(n);  rd(que);
    for(ll i = 1; i <= n; i++) {
        rd(a[i]);
        G[i].clear();
    }
    G[0].clear();
}
int main(){
    char s[10];
    ll T, Cas = 1, l, r; rd(T);
    while(T--){
        input();
        printf("Case #%I64d:\n", Cas++);
        precal();
        while(que--){
            scanf("%s", s);
            rd(l); rd(r);
      //      prllf("(%d,%d)\n", l, r);
            if(s[0] == 'R') {
                Rank(l, r);
                pt(ans.first); putchar(' '); pt(ans.second); putchar('\n');
            }
            else {
                Select(l, r);
                if(ans.first == -1)puts("-1");
                else {pt(ans.first); putchar(' '); pt(ans.second); putchar('\n');}
            }
        }
    }
    return 0;
}
/*
1
3 6
6 2 4
RANK 1 1
SELECT 3 1
RANK 2 3
SELECT 2 2
SELECT 1 3
SELECT 1 4

*/
时间: 2024-08-03 03:06:33

HDU 5042 GCD pair 预处理+二分 分段的相关文章

HDU 5726 GCD 区间GCD=k的个数

GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2742    Accepted Submission(s): 980 Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). There ar

HDU 3622 Bomb Game(二分+2-SAT)

Bomb Game Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5396    Accepted Submission(s): 1925 Problem Description Robbie is playing an interesting computer game. The game field is an unbounde

hdu 6154 CaoHaha&#39;s staff 二分

题意: 一笔可以画一条长为1的边或者一条根号二的对角线 问围成一个面积是x的图形最少需要几条边 思路: 用公式直接二分 1 #include<bits/stdc++.h> 2 #define cl(a,b) memset(a,b,sizeof(a)) 3 #define debug(a) cerr<<#a<<"=="<<a<<endl 4 using namespace std; 5 typedef long long ll;

hdu 5317 合数分解+预处理

RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2818    Accepted Submission(s): 1108 Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and m

hdu 3264 圆的交+二分

Open-air shopping malls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1822    Accepted Submission(s): 651 Problem Description The city of M is a famous shopping city and its open-air shopping

HDU 1695 GCD 欧拉函数+容斥原理+质因数分解

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a <= b <= 100000, c=1, c <= d <= 100000, 0 <= k <= 100000) 思路:因为x与y的最大公约数为k,所以xx=x/k与yy=y/k一定互质.要从a/k和b/k之中选择互质的数,枚举1~b/k,当选择的yy小于等于a/k时,可以

HDU 5877 Weak Pair(弱点对)

HDU 5877 Weak Pair(弱点对) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Description 题目描述 You are given a rooted tree of N nodes, labeled from 1 to N. To the ith node a non-negative value ai is assigned. An ordere

HDU 1695 GCD (数论-整数和素数,组合数学-容斥原理)

GCD Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. GCD(x, y) means the greatest common divisor of x and y. Since the number of choices may be very large, you're only required to output t

HDU 3081Marriage Match II(二分+并查集+网络流之最大流)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3081 有一段时间没写最大流的题了,这题建图居然想了好长时间...刚开始是按着最终的最大流即是做多轮数去想建图,结果根本没思路,后来想了想,可以用二分答案的思想来找最终答案.然后很明显的并查集,但是并查集学的略渣,居然卡在并查集上了..= =. 但是也不是并查集的事..是我建图的思想太正了,稍微用点逆向思维并查集就可以很好利用了. 建图思路是:建立一个源点与汇点,将女孩与源点相连,男孩与汇点相连,权值