1935: [Shoi2007]Tree 园丁的烦恼

题目链接

题目大意:矩阵上有若干个数点,每次询问子矩阵的权

题解:差分后就是二维偏序了……
离线,x排序,y离散化上树状数组

我的收获:2333

#include <bits/stdc++.h>
using namespace std;

#define N 500005
#define MX 2999999

int n,m,w,tot;
int X[N],Y[N];
int A[N],B[N],C[N],D[N];
int z[MX];
int ans[N][5];

struct ques{
    int x,y,id,opt;
    void makeby(int i){x=X[i],y=lower_bound(z+1,z+w+1,Y[i])-z;}
}q[MX];

inline bool operator < (ques a,ques b){return a.x<b.x||(a.x==b.x&&a.opt<b.opt);}

struct Tree{
    int c[MX];
    void add(int x,int v){for(;x<=w;x+=x&-x) c[x]+=v;}
    int ask(int x){int ret=0;for(;x;x-=x&-x) ret+=c[x];return ret;}
}T;

void addquery(int i)
{
    B[i]=lower_bound(z+1,z+w+1,B[i])-z;
    D[i]=lower_bound(z+1,z+w+1,D[i])-z;
    q[++tot].x=A[i]-1,q[tot].y=B[i]-1,q[tot].id=i,q[tot].opt=1;
    q[++tot].x=A[i]-1,q[tot].y=D[i],q[tot].id=i,q[tot].opt=2;
    q[++tot].x=C[i],q[tot].y=B[i]-1,q[tot].id=i,q[tot].opt=3;
    q[++tot].x=C[i];q[tot].y=D[i];q[tot].id=i;q[tot].opt=4;
}

void solve()
{
    sort(q+1,q+tot+1);
    for(int i=1;i<=tot;i++)
    {
        if(!q[i].opt) T.add(q[i].y,1);
        else ans[q[i].id][q[i].opt]=T.ask(q[i].y);
    }
}

void work()
{
    solve();
    for(int i=1;i<=m;i++) printf("%d\n",ans[i][4]-ans[i][3]-ans[i][2]+ans[i][1]);
}

void init()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%d%d",&X[i],&Y[i]),X[i]++,Y[i]++,z[++w]=Y[i];
    for(int i=1;i<=m;i++) scanf("%d%d%d%d",&A[i],&B[i],&C[i],&D[i]),A[i]++,B[i]++,C[i]++,D[i]++,z[++w]=B[i],z[++w]=D[i];
    sort(z+1,z+w+1);w=unique(z+1,z+w+1)-z-1;
    for(int i=1;i<=n;i++) q[++tot].makeby(i);
    for(int i=1;i<=m;i++) addquery(i);
}

int main()
{
    init();
    work();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值