BZOJ 1935 [Shoi2007]Tree 园丁的烦恼

来源:互联网 发布:php 500错误日志 编辑:程序博客网 时间:2024/05/21 10:45

Description
很久很久以前,在遥远的大陆上有一个美丽的国家。统治着这个美丽国家的国王是一个园艺爱好者,在他的皇家花园里种植着各种奇花异草。有一天国王漫步在花园里,若有所思,他问一个园丁道: “最近我在思索一个问题,如果我们把花坛摆成六个六角形,那么……” “那么本质上它是一个深度优先搜索,陛下”,园丁深深地向国王鞠了一躬。 “嗯……我听说有一种怪物叫九头蛇,它非常贪吃苹果树……” “是的,显然这是一道经典的动态规划题,早在N元4002年我们就已经发现了其中的奥秘了,陛下”。 “该死的,你究竟是什么来头?” “陛下息怒,干我们的这行经常莫名其妙地被问到和OI有关的题目,我也是为了预防万一啊!” 王者的尊严受到了伤害,这是不可容忍的。看来一般的难题是难不倒这位园丁的,国王最后打算用车轮战来消耗他的实力: “年轻人,在我的花园里的每一棵树可以用一个整数坐标来表示,一会儿,我的骑士们会来轮番询问你某一个矩阵内有多少树,如果你不能立即答对,你就准备走人吧!”说完,国王气呼呼地先走了。 这下轮到园丁傻眼了,他没有准备过这样的问题。所幸的是,作为“全国园丁保护联盟”的会长——你,可以成为他的最后一根救命稻草。


【题目分析】
离线+树状数组


【代码】

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;struct event{    int opt;//0 add: 1 que    int x;// position    int y;// position    int ans;//belongs to    int tmp;//+ or -}t[3000001];int top=0,xx,yy,x1,y1,x2,y2,n,m;int ans[500001];int tr[10000001];int rank[3000001];inline bool cmp(int a,int b){return (t[a].x==t[b].x)?t[a].opt<t[b].opt:t[a].x<t[b].x;}inline void add(int k){for (int i=k;k<=10000000;k+=k&(-k)) tr[k]++;}inline int gs(int k){    int ret=0;    for (int i=k;i;i-=i&(-i)) ret+=tr[i];    return ret; }int main(){    scanf("%d%d",&n,&m);    for (int i=1;i<=n;++i)    {        scanf("%d%d",&xx,&yy);        xx++;yy++;        ++top;        rank[top]=top;        t[top].opt=0;        t[top].x=xx;        t[top].y=yy;        t[top].ans=0;        t[top].tmp=0;    }    for (int i=1;i<=m;++i)    {        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);        x1++;x2++;y1++;y2++;        ++top;        rank[top]=top;        t[top].opt=1;        t[top].x=x1-1;        t[top].y=y1-1;        t[top].ans=i;        t[top].tmp=1;        ++top;        rank[top]=top;        t[top].opt=1;        t[top].x=x2;        t[top].y=y2;        t[top].ans=i;        t[top].tmp=1;        ++top;        rank[top]=top;        t[top].opt=1;        t[top].x=x2;        t[top].y=y1-1;        t[top].ans=i;        t[top].tmp=-1;        ++top;        rank[top]=top;        t[top].opt=1;        t[top].x=x1-1;        t[top].y=y2;        t[top].ans=i;        t[top].tmp=-1;    }    sort (rank+1,rank+top+1,cmp);    for (int i=1;i<=top;++i)    {        if (!t[rank[i]].opt) add(t[rank[i]].y);        else        {            ans[t[rank[i]].ans]+=t[rank[i]].tmp*gs(t[rank[i]].y);        }    }    for (int i=1;i<=m;++i)    printf("%d\n",ans[i]);}
0 0