7.20 matryoshka (tao)

Matryoshka (tao)

input
7 3
9 5
3 7
10 6
5 10
2 6
10 10
4 1
10 5
3 5
3 9
output
0
1
2

 

sol:

Think of the query (x1, y1) to (x2, y2) if and only if there are edges (x1 <x2, y1 <y2)

Has a nature that: the minimum of this figure longest chain covers = reverse strand

Reverse strand is satisfied (x1 <x2, y1> y2), so the point x according to the sorted sequence is to compute the longest drop

Then scan line sweep in the past, with Fenwick tree maintenance sub-sequence can look at the longest decline

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
    ll s=0; bool f=0; char ch=' ';
    while(!isdigit(ch)) {f|=(ch=='-'); ch=getchar();}
    while(isdigit(ch)) {s=(s<<3)+(s<<1)+(ch^48); ch=getchar();}
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0) {putchar('-'); x=-x;}
    if(x<10) {putchar(x+'0'); return;}
    write(x/10); putchar((x%10)+'0');
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=200005;
int n,Q,c[N],ans[N];
struct Node
{
    int r,h,id;
    inline bool operator<(const Node &tmp)const
    {
        return r>tmp.r||(r==tmp.r&&h<tmp.h)||(r==tmp.r&&h==tmp.h&&id<tmp.id);
    }
}a[N<<1];
struct BIT
{
    int S[N];
    #define lowbit(x) ((x)&(-x))
    inline void add(int x,int y)
    {
        for(;x<=n;x+=lowbit(x)) S[x]=max(S[x],y);
    }
    inline int Que(int x)
    {
        int ans=0;
        for(;x;x-=lowbit(x)) ans=max(ans,S[x]);
        return ans;
    }
}T;
int main()
{
    freopen("tao.in","r",stdin);
    freopen("tao.out","w",stdout);
    int i;
    R(n); R(Q);
    for(i=1;i<=n;i++)
    {
        R(a[i].r); R(a[i].h); a[i].id=0; c[i]=a[i].h;
    }
    for(i=1;i<=Q;i++)
    {
        R(a[n+i].r); R(a[n+i].h); a[n+i].id=i;
    }
    sort(c+1,c+n+1); *c=unique(c+1,c+n+1)-c-1;
    sort(a+1,a+n+Q+1);
    for(i=1;i<=n+Q;i++)
    {
        a[i].h=upper_bound(c+1,c+*c+1,a[i].h)-c-1;
        if(a[i].id) ans[a[i].id]=T.Que(a[i].h);
        else T.add(a[i].h,T.Que(a[i].h)+1);
    }
    for(i=1;i<=Q;i++) Wl(ans[i]);
    return 0;
}
/*
input
7 3
9 5
3 7
10 6
5 10
2 6
10 10
4 1
10 5
3 5
3 9
output
0
1
2
*/
View Code

 

Guess you like

Origin www.cnblogs.com/gaojunonly1/p/11223757.html
Recommended