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

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

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

时间: 2024-10-20 10:38:18

BZOJ 1935 SHOI 2007 Tree 园丁的烦恼 树状数组的相关文章

BZOJ 1935: [Shoi2007]Tree 园丁的烦恼 [树状数组 离线 离散化]

传送门 刚才我还在郁闷网上怎么没人用$CDQ$分治做 突然发现根本没有时间序.... #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int N=3e6+5; inline int read(){ char c=getchar();int x=0,f=1; whi

BZOJ 1935 SHOI2007 园丁的烦恼 树状数组

题目大意:给定平面上的一些点,多次询问某个矩形中有多少个点 将每个询问拆成4个 然后把所有询问和点都按照横坐标排序 对于每个询问,将所有x值小于等于这个询问的x的点的y值加入树状数组 然后在树状数组上查询小于等于这个询问的y值的点的数量 别被1000W吓到了 如果不爆内存的话1E也是能搞的 套个log就没多少了 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm&

hoj 1867 经理的烦恼 树状数组

链接:http://acm.hit.edu.cn/hoj/problem/view?id=1867 谁说是入门题啊,真想拍他一巴掌 经理的烦恼 My Tags   (Edit)   Source : HCPC 2005 Spring   Time limit : 2 sec   Memory limit : 32 M Submitted : 2578, Accepted : 606 Jerry是一家公司销售部门的经理.这家公司有很多连锁店,编号为1,2,3,... Jerry每天必须关注每家连锁

POJ-3321 Apple Tree 【DFS序+树状数组】

Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree. The tree has N forks which are connected by branches.

Codeforces 570D TREE REQUESTS dfs序+树状数组

链接 题解链接:点击打开链接 题意: 给定n个点的树,m个询问 下面n-1个数给出每个点的父节点,1是root 每个点有一个字母 下面n个小写字母给出每个点的字母. 下面m行给出询问: 询问形如 (u, deep) 问u点的子树中,距离根的深度为deep的所有点的字母能否在任意排列后组成回文串,能输出Yes. 思路:dfs序,给点重新标号,dfs进入u点的时间戳记为l[u], 离开的时间戳记为r[u], 这样对于某个点u,他的子树节点对应区间都在区间 [l[u], r[u]]内. 把距离根深度相

Codeforces 570D TREE REQUESTS dfs序+树状数组 异或

http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Roman planted a tree consisting of?n?vertices. Each vertex contains a lowercase

poj3321-Apple Tree(DFS序+树状数组)

Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36442   Accepted: 10894 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has bee

Codeforces1076E. Vasya and a Tree(dfs+离线+树状数组维护)

题目链接:传送门 题目: E. Vasya and a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Vasya has a tree consisting of n vertices with root in vertex 1. At first all vertices has 0 written on it.

Apple Tree(需要预处理的树状数组)

Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20335   Accepted: 6182 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been