hdu 6601 (Chairman of the tree)

Portal

Meaning of the questions:

To give you a length \ (n-\) sequences have \ (Q \) a query, each interrogation give you an interval \ ([L, R & lt] \) , each time you ask asked in the interval \ ([L , R & lt] \) , the maximum circumference of the triangle can be composed.

analysis:

Because the two sides of a triangle having properties greater than the third side, i.e. (a + b> c \) \ properties. And if there are a number of conditions are in line with the number, then if we will change the equal sign equal sign, which formed a Fibonacci Fibonacci series. According to the nature of the Fibonacci number, in the range ([1, a] \) \ is the Fibonacci columns have up \ (log2 (a) \) key.

And here you can take advantage of this nature, we ask each greedy to get the first \ (k \) large, the \ (k + 1 \) large, the \ (k + 3 \) large numbers to compare, if you do not eligible then keep looking backward for \ (k + 4 \) key until you find the answer so far. Because the upper limit of range \ (10 ^ 9 \) , it is probably the greatest need only to enumerate \ (44 \) times. And each searching for their first \ (k \) large, we can Chairman tree with \ (\ mathcal {O} ( logn) \) queries at the first time complexity \ (k \) large. Therefore, the overall time complexity is: \ (\ mathcal {O} (44qlogn) \)

Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 100010;
const int M = MAXN * 30;
int n,q,m,tot;
int a[MAXN], t[MAXN];
int T[M], lson[M], rson[M], c[M];

void Init_hash()
{
    for(int i = 1; i <= n;i++)
        t[i] = a[i];
    sort(t+1,t+1+n);
    m = unique(t+1,t+1+n)-t-1;
}
int build(int l,int r)
{
    int root = tot++;
    c[root] = 0;
    if(l != r)
    {
        int mid = (l+r)>>1;
        lson[root] = build(l,mid);
        rson[root] = build(mid+1,r);
    }
    return root;
}
int Hash(int x)
{
    return lower_bound(t+1,t+1+m,x) - t;
}
int update(int root,int pos,int val)
{
    int newroot = tot++, tmp = newroot;
    c[newroot] = c[root] + val;
    int l = 1, r = m;
    while(l < r)
    {
        int mid = (l+r)>>1;
        if(pos <= mid)
        {
            lson[newroot] = tot++; rson[newroot] = rson[root];
            newroot = lson[newroot]; root = lson[root];
            r = mid;
        }
        else
        {
            rson[newroot] = tot++; lson[newroot] = lson[root];
            newroot = rson[newroot]; root = rson[root];
            l = mid+1;
        }
        c[newroot] = c[root] + val;
    }
    return tmp;
}
int query(int left_root,int right_root,int k)
{
    int l = 1, r = m;
    while( l < r)
    {
        int mid = (l+r)>>1;
        if(c[lson[left_root]]-c[lson[right_root]] >= k )
        {
            r = mid;
            left_root = lson[left_root];
            right_root = lson[right_root];
        }
        else
        {
            l = mid + 1;
            k -= c[lson[left_root]] - c[lson[right_root]];
            left_root = rson[left_root];
            right_root = rson[right_root];
        }
    }
    return l;
}
void read(int &ret){
    ret=0;
    char ch=getchar();
    int flag=1;
    while(ch>'9'||ch<'0'){if(ch=='-') flag=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){
        ret=ret*10+ch-'0';
        ch=getchar();
    }
    ret*=flag;
}
int main(){
    while(~scanf("%d%d",&n,&q)){
        for (int i = 1; i <= n; i++) read(a[i]);
        tot=0;
        Init_hash();
        T[n + 1] = build(1, m);
        for (int i = n; i; i--) {
            int pos = Hash(a[i]);
            T[i] = update(T[i + 1], pos, 1);
        }
        while (q--) {
            int l, r;
            read(l), read(r);
            int len = (r - l + 1);
            ll ans = -1;
            while (len >= 3) {
                ll x1 = t[query(T[l], T[r + 1], len)];
                ll x2 = t[query(T[l], T[r + 1], len - 1)];
                ll x3 = t[query(T[l], T[r + 1], len - 2)];
                if (x1 < x2 + x3) {
                    ans = 1LL * x1 + 1LL * x2 + 1LL * x3;
                    break;
                }
                len--;
            }
            printf("%lld\n", ans);
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/Chen-Jr/p/11240189.html