[SHOI2007] gardener troubles (discrete, Fenwick tree)

[SHOI2007] gardener troubles (luogu)

Solution

Found a large coordinate range, consider discrete

But then still can not use the discrete two-dimensional data structure, the array size is too large

Found no changes, only the query

When the x-coordinate may be considered to gradually increase, only the y coordinate information recording

The query then divided into four to (0,0) for the interrogation of the lower left corner, and then successively calculates Summary

The coordinates and the coordinates of each interrogation tree in an array, y coordinate discretization

Press x coordinate small to large tree in front of the x-coordinate equal, ask back

Through the array, the array when it hit a tree in its y-coordinate plus 1

When the inquiry request tree array hit less and its y-coordinate to

Code

 

#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int N=3e6+10;
int tr[N],n,m,cnt,A,B,C,D,ans[N],tot;
struct node
{
    int x,id;
    bool operator <(const node &o)const
    {
        return x<o.x;
    }
}a[N];
struct mode
{
    int x,y,id,inv;
    bool operator <(const mode &o)const
    {
        return x==o.x?id<o.id:x<o.x;
    }
}d[N];
int lowbit(int x)
{
    return x&(-x);
}
int get(int x)
{
    int ans=0;
    for(;x;x-=lowbit(x)) ans+=tr[x];
    return ans;
}
void add(int x)
{
    for(;x<=cnt;x+=lowbit(x)) tr[x]++;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        tot++;
        scanf("%d%d",&d[tot].x,&d[tot].y);
        d[tot].x++,d[tot].y++;
        d[tot].id=0;
    }
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d%d",&A,&B,&C,&D);
        A++,B++,C++,D++;
        d[all ++] = (mode) {A- 1 , B- 1 , and 1 }; 
        d [ ++ all] = (mode) {C, D, and 1 }; 
        d [ ++ all] = (mode) {A- 1 , D, and - 1 }; 
        d [ ++ all] = (mode) {C, B- 1 and - 1 }; 
    } 
    For ( int i = 1 ; i <= all; i ++ ) 
        a [i] .x = d [i] .y in [i] .id = i; 
    Luckily (a + 1 , a + 1 + all);
    for ( int i = 1 ; i <= all; i ++)
    {
        if(i==1 || a[i].x!=a[i-1].x) cnt++;
        d[a[i].id].y=cnt;
    }
    sort(d+1,d+1+tot);
    for(int i=1;i<=tot;i++)
    {
        if(d[i].id) ans[d[i].id]+=d[i].inv*get(d[i].y);
        else add(d[i].y);
    }
    for(int i=1;i<=m;i++) printf("%d\n",ans[i]);
    return 0;
}

 

 

 

 

Guess you like

Origin www.cnblogs.com/hsez-cyx/p/12418199.html