1745: group

Time limit: 1000 ms Memory Limit: 262144 KB

[title] Description

communities n inhabitants, each resident has a certain status and age, ri represents the i personal position, ai represents the i individual's age.

Recently community to hold events, requiring several people into a team must have a team captain, to become captain following conditions are satisfied:

①, captain position in the group should be the highest (can be tied for first);

② the age gap and the captain of the age of the other members of the group can not exceed K.

Some people want to own close group of people in the same group, while the group of people where the hope of better.

For example, x and y want to be in the same group, while the group of people where they want, the better, of course. They must also choose a captain meet the above criteria, then ask you to include both x and y group, you can set the maximum number of people? [Input] The first line of two integers n and K. The next line of input n integers: r1, r2, ..., rn . The next line of input n integers: a1, a2, ..., an . Now enter Q represents a Q of inquiry. The next Q lines each input x, y, expressed ask: when x and y are set in the same group, they team can have a maximum number of people (x and y are also likely to be selected as captain, as long as they meet the conditions). [Output] For each inquiry, the corresponding output answers, each answer per line. When x and when y can not be set at the same age output 1 (for example, x is 1, y is the age of 100, K = 1, no matter who serves as both captain, x and y, there is always someone older than captain gap over K, then output -1). [Sample] input . 5. 1 . 1. 5. 1. 4 2
 
























2. 4 3 2. 4
. 4
5 3
2 3
2 5
. 4 1

[] sample output

. 4
3
-1
. 4

[Note]

[explain] Sample

interrogation 1: 5 when the first and the third individual when a person wants group, Group {1,3,4,5} can be members, selected captain 3, and 2 can not be added, because it was added 2, 2 and 5 the gap between the age of 2, than the K = 1.

Inquiry 2: When the second and third individual person wants at a set, {1,2,3} may be selected.

Answer 3: 2 and 5 when want to be together, can not meet the requirements.

Ask 4: 1 and wanted to 4 when taken together, may select {1,3,4,5}.

[Data] scale

data for 20% of: 2≤n≤100,0≤k≤100,1≤ri, ai≤100,1≤q≤100.

For 40% of the data: 2≤n≤1000,0≤k≤1000,1≤ri, ai≤1000,1≤q≤1000.

For 60% of the data: 2≤n≤ \ (10 ^ {4 } \), 0≤k≤ \ (10 ^ {9} \), 1≤ri, ai≤ \ (10 ^ {9} \), 1≤q≤ \ (10 ^ {4} \).

To 100% of the data: 2≤n≤ \ (10 ^ {5 } \), 0≤k≤ \ (10 ^ {9} \), 1≤ri, ai≤ \ (10 ^ {9} \), 1≤q≤ \ (10 ^ {5} \), 1≤x, y≤n, x ≠ y.

【answer】

 

More often it will certainly be asked how many people may be pretreated with each person as leader, then take the maximum number of people in leadership can be selected.

 

The number may be advisable to ask offline status by asking two people maximum in descending order, each time before querying the status of all the people is greater than the maximum age of safety pressed into the tree line, each query ages can be.

 

考虑如何预处理每个人可以带多少人,将每个人按地位升序排序,则在他及他之前的人在年龄范围内都可以被带。

 

每扫到一个人,先把他和和他地位相同的人全部压入线段树中,再查询年龄区间内人数。

 

代码如下:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=4e5+5;
map <int,int> dui;
int n,k,cnt,q,sh[N<<2],mx[N<<2],cou[N];
struct lisan
{
    int zhi,id;
}ls[N<<2];
struct people
{
    int r,a,id,dair;
}p[N];
struct xunwen
{
    int x,y,id,di,xs,ys;
}wen[N];
inline bool cmp(lisan x,lisan y)
{
    return x.zhi<y.zhi;
}
inline int read()
{
    char c=getchar();
    int x=0;
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) {x=(x<<3)+(x<<1)+c-'0';c=getchar();}
    return x;
}
//-------------树状数组---------------------// 
inline int lowbit(int x)
{
    return x& (-x);
}
inline void update(int x,int val)
{
    for(int i=x;i<=cnt;i+=lowbit(i)) sh[i]+=val;
}
inline int query(int x)
{
    int daan=0;
    if(x<=0) return 0;
    for(int i=x;i;i-=lowbit(i)) daan+=sh[i];
    return daan;
}
//----------------------------------//
inline void lisanlisan()
{
    sort(ls+1,ls+3*n+1,cmp);
    dui[ls[1].zhi]=++cnt;
    if(ls[1].id) p[ls[1].id].a=ls[1].zhi;
    for(int i=2;i<=n*3;i++)
    {
        if(ls[i].zhi!=ls[i-1].zhi) dui[ls[i].zhi]=++cnt;
        if(ls[i].id)
            p[ls[i].id].a=ls[i].zhi;
    }
}
inline bool cmp1(people x,people y)
{
    return x.r<y.r;
}
inline void get_meiren()
{
    sort(p+1,p+n+1,cmp1);
    int pre=1;
    for(int i=2;i<=n+1;i++)
    {
        if(p[i].r!=p[i-1].r)
        {
            for(int j=pre;j<=i-1;j++)
                update(dui[p[j].a],1);
            for(int j=pre;j<=i-1;j++)
                p[j].dair=query(dui[p[j].a+k])-query(dui[p[j].a-k]-1);
            pre=i;
        }
    }
}
inline bool cmp2(xunwen x,xunwen y)
{
    return x.di>y.di;
}
inline void modify(int now,int l,int r,int x,int zhi)
{
    if(l==r)
    {
        mx[now]=max(mx[now],zhi);
        return;
    }
    int mid=(l+r)>>1;
    if(x<=mid) modify(now<<1,l,mid,x,zhi);
    else modify(now<<1|1,mid+1,r,x,zhi);
    mx[now]=max(mx[now<<1],mx[now<<1|1]);
}
inline int cha(int now,int l,int r,int L,int R)
{
    if(L>R) return 0;
    if(l>=L&&r<=R) return mx[now];
    int mid=(l+r)>>1,daan=0;
    if(L<=mid) daan=cha(now<<1,l,mid,L,R);
    if(R>=mid+1) daan=max(daan,cha(now<<1|1,mid+1,r,L,R));
    return daan;
}
inline void solve()
{
    int dao=n;
    sort(wen+1,wen+q+1,cmp2);
    for(int i=1;i<=q;i++)
    {
        while(dao&&p[dao].r>=wen[i].di)
        {
            modify(1,1,cnt,dui[p[dao].a],p[dao].dair);
            dao--;
        }
        int ans=cha(1,1,cnt,dui[max((int)0,max(wen[i].xs,wen[i].ys)-k)],dui[min(wen[i].xs,wen[i].ys)+k]);
        if(ans==0) cou[wen[i].id]=-1;
        else cou[wen[i].id]=ans;
    }
}
signed main()
{
    n=read();k=read();
    for(int i=1;i<=n;i++) p[i].r=read(),p[i].id=i;
    for(int i=1;i<=n;i++) ls[i].zhi=read(),ls[i].id=i,ls[i+n].zhi=ls[i].zhi+k,ls[i+n+n].zhi=max((int)0,ls[i].zhi-k);
    lisanlisan();
    q=read();
    for(int i=1;i<=q;i++) wen[i].id=i,wen[i].x=read(),wen[i].y=read(),wen[i].di=max(p[wen[i].x].r,p[wen[i].y].r),wen[i].xs=p[wen[i].x].a,wen[i].ys=p[wen[i].y].a;
    get_meiren();
    solve();
    for(int i=1;i<=q;i++) cout<<cou[i]<<"\n";
    return 0;
}
View Code

Guess you like

Origin www.cnblogs.com/betablewaloot/p/12173897.html