本文主要是介绍【bzoj 1935】【codevs 2342】[Shoi2007]Tree 园丁的烦恼(树状数组),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1935: [Shoi2007]Tree 园丁的烦恼
Time Limit: 15 Sec Memory Limit: 357 MBSubmit: 1018 Solved: 465
[ Submit][ Status][ Discuss]
Description
Input
Output
Sample Input
0 0
0 1
1 0
0 0 1 1
Sample Output
HINT
Source
【题解】【离散化+树状数组】
【先将初始化和询问全部离散一维,然后按未离散的一维排序并去重。然后在离散后的数组中找初始化和询问,并把它们存到一个结构体里,询问的处理方法和count一题相同,都按二维的来处理。最后在处理的时候,如果遇到的是初始,则将其加入树状数组里,如果遇到的是询问,就进行查询操作(由于离线的时候是按大小排过序,所以询问出的答案要按照序号保存)】
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
struct question{int x,y,num,typ;
}ask[2500010];
int x1[500010],y1[500010],ax1[500010],ay1[500010],ax2[500010],ay2[500010];
int que[2500010],tot,ans[500010][5];
int tree[500010];
int n,m,cnt;
inline int tmp(question a,question b)
{return a.x<b.x||(a.x==b.x&&a.typ<b.typ);
}
inline void add(int x,int val)
{for (int i=x;i<=n;i+=i&(-i))tree[i]+=val;return;
}
inline int ask1(int x)
{int sum=0;for(int i=x;i>=1;i-=i&(-i))sum+=tree[i];return sum;
}
int main()
{int i,j;scanf("%d%d",&n,&m);for(i=1;i<=n;++i){int x,y;scanf("%d%d",&x1[i],&y1[i]);x1[i]++; y1[i]++;que[++tot]=y1[i];}for(i=1;i<=m;++i){scanf("%d%d%d%d",&ax1[i],&ay1[i],&ax2[i],&ay2[i]);ax1[i]++; ay1[i]++; ax2[i]++; ay2[i]++;que[++tot]=ay1[i]; que[++tot]=ay2[i];}sort(que+1,que+tot+1);tot=unique(que+1,que+tot+1)-que-1;for(i=1;i<=n;++i){y1[i]=lower_bound(que+1,que+tot+1,y1[i])-que;ask[++cnt].x=x1[i]; ask[cnt].y=y1[i];}for(i=1;i<=m;++i){ay1[i]=lower_bound(que+1,que+tot+1,ay1[i])-que;ay2[i]=lower_bound(que+1,que+tot+1,ay2[i])-que;ask[++cnt].num=i; ask[cnt].x=ax2[i]; ask[cnt].y=ay2[i]; ask[cnt].typ=1;ask[++cnt].num=i; ask[cnt].x=ax1[i]-1; ask[cnt].y=ay2[i]; ask[cnt].typ=2;ask[++cnt].num=i; ask[cnt].x=ax2[i]; ask[cnt].y=ay1[i]-1; ask[cnt].typ=3;ask[++cnt].num=i; ask[cnt].x=ax1[i]-1; ask[cnt].y=ay1[i]-1; ask[cnt].typ=4;}sort(ask+1,ask+cnt+1,tmp);for(i=1;i<=cnt;++i){if (!ask[i].typ) add(ask[i].y,1);elseans[ask[i].num][ask[i].typ]=ask1(ask[i].y);}for(i=1;i<=m;++i){int sum;sum=ans[i][1]-ans[i][2]-ans[i][3]+ans[i][4];printf("%d\n",sum);}return 0;
}
这篇关于【bzoj 1935】【codevs 2342】[Shoi2007]Tree 园丁的烦恼(树状数组)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!