码迷,mamicode.com
首页 > 编程语言 > 详细

BZOJ 1935 SHOI 2007 Tree 园丁的烦恼 树状数组

时间:2014-12-02 16:41:44      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:bzoj   shoi2007   树状数组   fenwick   离线处理   

题目大意:给出平面中的一些点,询问平面中的一些矩形中有多少点。


思路:正常应该是二维树状数组,然后数据范围太大。所以就只能按照一个坐标排序,另一个坐标跑树状数组。注意离线操作,一个问题拆成4个。


CODE:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 500010
#define RANGE 10000010
using namespace std;
 
struct Point{
    int x,y;
     
    bool operator <(const Point &a)const {
        return x < a.x;
    }
    void Read() {
        scanf("%d%d",&x,&y);
        ++x,++y;
    }
}point[MAX];
 
struct Ask{
    int x,y;
    int from,ratio;
     
    Ask(int _,int __,int ___,int ____):x(_),y(__),from(___),ratio(____) {}
    Ask() {}
    bool operator <(const Ask &a)const {
        return x < a.x;
    }
}ask[MAX << 2];
 
int points,asks,cnt;
int fenwick[RANGE];
int ans[MAX];
 
inline void Fix(int x)
{
    for(; x < RANGE; x += x&-x)
        ++fenwick[x];
}
 
inline int GetSum(int x)
{
    int re = 0;
    for(; x; x -= x&-x)
        re += fenwick[x];
    return re;
}
 
int main()
{
    cin >> points >> asks;
    for(int i = 1; i <= points; ++i)
        point[i].Read();
    for(int x1,y1,x2,y2,i = 1; i <= asks; ++i) {
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        ++x1,++x2,++y1,++y2;
        ask[++cnt] = Ask(x1 - 1,y1 - 1,i,1);
        ask[++cnt] = Ask(x1 - 1,y2,i,-1);
        ask[++cnt] = Ask(x2,y1 - 1,i,-1);
        ask[++cnt] = Ask(x2,y2,i,1);
    }
    sort(point + 1,point + points + 1);
    sort(ask + 1,ask + cnt + 1);
    int now = 1;
    for(int i = 1; i <= cnt; ++i) {
        for(; point[now].x <= ask[i].x && now <= points; ++now)
            Fix(point[now].y);
        ans[ask[i].from] += GetSum(ask[i].y) * ask[i].ratio;
    }
    for(int i = 1; i <= asks; ++i)
        printf("%d\n",ans[i]);
    return 0;
}


BZOJ 1935 SHOI 2007 Tree 园丁的烦恼 树状数组

标签:bzoj   shoi2007   树状数组   fenwick   离线处理   

原文地址:http://blog.csdn.net/jiangyuze831/article/details/41679165

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!