4822: [Cqoi2017]老C的任务&&1935: [Shoi2007]Tree 园丁的烦恼

题目自己看吧,

题解:

把询问拆成四个,按 x 坐标扫描,按 y 坐标用树状数组维护。

4822:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int N = 1E5 + 10;  
const int Max = ~0U>>1;  
const int Min = -(1LL << 31LL);  
typedef long long LL; 
int n,m,n1,n2,cx,cy;
int px[N],py[N],p[N],ax[N],ay[N];
struct node{
    int y,typ,num;
    node(int y,int num,int typ):  
        y(y),num(num),typ(typ){}  
};
vector<pair<int,int> >P[N];
vector<node>q[N];
void ins(int x,int y,int Num,int typ)
{
    int nx = lower_bound(px + 1,px + cx + 1,x) - px;  
    int ny = lower_bound(py + 1,py + cy + 1,y) - py;  
    if (px[nx] > x) --nx; if (py[ny] > y) --ny;  
    q[nx].push_back(node(ny,Num,typ)); 
}
void prepare()
{
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d%d",&px[i],&py[i],&p[i]);
        ax[i]=px[i];ay[i]=py[i];
    }
    cx=1;px[++n1]=Min;px[++n1]=Max;
    cy=1;py[++n2]=Min;py[++n2]=Max;
    sort(px+1,px+1+n1);
    for(int i=2;i<=n1;i++)
    if(px[i]!=px[i-1]) px[++cx]=px[i];
    sort(py+1,py+1+n2);
    for(int i=2;i<=n2;i++)
    if(py[i]!=py[i-1]) py[++cy]=py[i];
    for(int i=1;i<=n;i++)
    {
        ax[i]=lower_bound(px+1,px+1+cx,ax[i])-px;
        ay[i]=lower_bound(py+1,py+1+cy,ay[i])-py;
        P[ax[i]].push_back(make_pair(ay[i],p[i]));
    }
    //*********************离散化******************** 
    //printf("!");
    for(int i=1;i<=m;i++)
    {
        int x1,x2,y1,y2;
        scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
        ins(y1,y2,i,1);
        if(x1>Min) ins(x1-1,y2,i,-1);
        if(x2>Min) ins(y1,x2-1,i,-1);
        if(x1>Min&&x2>Min) ins(x1-1,x2-1,i,1); 
    }
    /*cx = 1; ax[++n1] = Min; ax[++n1] = Max;  
    cy = 1; ay[++n2] = Min; ay[++n2] = Max;  
    sort(ax + 1,ax + n1 + 1);  
    for (int i = 2; i <= n1; i++)  
        if (ax[i] != ax[i - 1]) ax[++cx] = ax[i];  
    sort(ay + 1,ay + n2 + 1);  
    for (int i = 2; i <= n2; i++)  
        if (ay[i] != ay[i - 1]) ay[++cy] = ay[i];  
    for (int i = 1; i <= n; i++)  
    {  
        px[i] = lower_bound(ax + 1,ax + cx + 1,px[i]) - ax;  
        py[i] = lower_bound(ay + 1,ay + cy + 1,py[i]) - ay;  
        P[px[i]].push_back(make_pair(py[i],p[i]));  
    }  
    for (int i = 1; i <= m; i++)  
    {  
        int xa,ya,xb,yb;  
       scanf("%d%d%d%d",&xa,&ya,&xb,&yb); 
       Add(xb,yb,i,1);  
        if (xa > Min) Add(xa - 1,yb,i,-1);  
        if (ya > Min) Add(xb,ya - 1,i,-1);  
        if (xa > Min && ya > Min) Add(xa - 1,ya - 1,i,1);  
    }  */
}
LL sum[N],ans[N];
int main()
{
    scanf("%d%d",&n,&m);
    n1=n2=n;
    prepare();
    //printf("!");
    for(int i=1;i<=cx;i++)
    {
        for(int j=0;j<P[i].size();j++)
        {
            for(int u=P[i][j].first;u<=cy;u+=u&-u)
            sum[u]+=1LL*P[i][j].second;
        }
        for(int j=0;j<q[i].size();j++)
        {
            node yu=q[i][j];LL tot=0;
            for(int u=yu.y;u>0;u-=u&-u) tot+=sum[u];
            ans[yu.num]+=1LL*tot*yu.typ;
        }
    }
    for(int i=1;i<m;i++)
    printf("%lld\n",ans[i]);
    printf("%lld",ans[m]);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值