bzoj千题计划143:bzoj1935:[shoi2007]tree园丁的烦恼

日拱一卒功不唐捐 日拱一卒功不唐捐     2022-10-05     642

关键词:

http://www.lydsy.com/JudgeOnline/problem.php?id=1935

 

二维偏序问题

排序x,离散化树状数组维护y

 

#include<cstdio>
#include<iostream>
#include<algorithm>

#define lowbit(x) x&-x 

using namespace std;

#define N 500001

struct TREE
{
    int xi,yi;
}Tree[N];

struct ASK
{
    int ldx,ldy,rux,ruy;
}Ask[N];

int hy[N*3];

struct node
{
    int x,y,bl,mul;
    bool ty;
}e[N*5],tmp[N*5];

int ans[N];

int c[N*3];

int toty;

void read(int &x)
{
    x=0; char c=getchar();
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) { x=x*10+c-0; c=getchar(); }
}

bool cmp(node p,node q)
{
    if(p.x!=q.x) return p.x<q.x;
    return p.ty<q.ty;
}

void change(int x)
{
    while(x<=toty)
    {
        c[x]++;
        x+=lowbit(x);
    }
}

int query(int x)
{
    int sum=0;
    while(x)
    {
        sum+=c[x];
        x-=lowbit(x);
    }
    return sum;
}

int main()
{
    int n,m;
    read(n); read(m);
    for(int i=1;i<=n;++i) 
    {
        read(Tree[i].xi); 
        read(Tree[i].yi); hy[++toty]=Tree[i].yi;
    }
    for(int i=1;i<=m;++i)
    {
        read(Ask[i].ldx); 
        read(Ask[i].ldy); hy[++toty]=Ask[i].ldy;
        read(Ask[i].rux); 
        read(Ask[i].ruy); hy[++toty]=Ask[i].ruy;
    }
    sort(hy+1,hy+toty+1);
    toty=unique(hy+1,hy+toty+1)-hy-1;
    int tot=0;
    for(int i=1;i<=n;++i)
    {
        ++tot;
        e[tot].x=Tree[i].xi;
        e[tot].y=lower_bound(hy+1,hy+toty+1,Tree[i].yi)-hy;
    }
    int ly,ry;
    for(int i=1;i<=m;++i)
    {
        ly=lower_bound(hy+1,hy+toty+1,Ask[i].ldy)-hy;
        ry=lower_bound(hy+1,hy+toty+1,Ask[i].ruy)-hy;
        e[++tot].bl=i; e[tot].mul=1; e[tot].ty=true; e[tot].x=Ask[i].rux; e[tot].y=ry;
        e[++tot].bl=i; e[tot].mul=1; e[tot].ty=true; e[tot].x=Ask[i].ldx-1; e[tot].y=ly-1;
        e[++tot].bl=i; e[tot].mul=-1; e[tot].ty=true; e[tot].x=Ask[i].ldx-1; e[tot].y=ry;
        e[++tot].bl=i; e[tot].mul=-1; e[tot].ty=true; e[tot].x=Ask[i].rux; e[tot].y=ly-1; 
    }
    sort(e+1,e+tot+1,cmp);
    for(int i=1;i<=tot;++i)
    {
        if(!e[i].ty) change(e[i].y);
        else ans[e[i].bl]+=e[i].mul*query(e[i].y);
    }
    for(int i=1;i<=m;++i) cout<<ans[i]<<
;
}

 

1935: [Shoi2007]Tree 园丁的烦恼

Time Limit: 15 Sec  Memory Limit: 357 MB
Submit: 1499  Solved: 673
[Submit][Status][Discuss]

Description

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

Input

第一行有两个整数n,m(0≤n≤500000,1≤m≤500000)。n代表皇家花园的树木的总数,m代表骑士们询问的次数。 文件接下来的n行,每行都有两个整数xi,yi,代表第i棵树的坐标(0≤xi,yi≤10000000)。 文件的最后m行,每行都有四个整数aj,bj,cj,dj,表示第j次询问,其中所问的矩形以(aj,bj)为左下坐标,以(cj,dj)为右上坐标。

Output

共输出m行,每行一个整数,即回答国王以(aj,bj)和(cj,dj)为界的矩形里有多少棵树。

Sample Input

3 1
0 0
0 1
1 0
0 0 1 1

Sample Output

3

bzoj千题计划120:bzoj1032[jsoi2007]祖码zuma

http://www.lydsy.com/JudgeOnline/problem.php?id=1032 https://www.luogu.org/discuss/show?postid=8416 #include<cstdio>#include<cstring>#include<iostream>usingnamespacestd;#de 查看详情

bzoj千题计划214:bzoj3589:动态树

http://www.lydsy.com/JudgeOnline/problem.php?id=3589 树链剖分用线段数维护扫描线的方式来写,标记只打不下传 #include<cstdio>#include<iostream>#include<algorithm>#defineN200001usingnamespacestd;intn;in 查看详情

bzoj千题计划119:bzoj1029:[jsoi2007]建筑抢修

http://www.lydsy.com/JudgeOnline/problem.php?id=1029 把任务按截止时间从小到大排序如果当前时间+当前任务耗时<=当前任务截止时间,把这个任务耗时放到大根堆里,ans++否则如果堆顶>当前任务耗时,删除堆顶,把这个任务耗时放... 查看详情

bzoj千题计划219:bzoj1568:[jsoi2008]bluemary开公司

http://www.lydsy.com/JudgeOnline/problem.php?id=1568 写多了就觉着水了。。。 #include<cstdio>#include<iostream>#include<algorithm>usingnamespacestd;#defineN100001doublea[N<<2],b[ 查看详情

bzoj千题计划197:bzoj4247:挂饰

http://www.lydsy.com/JudgeOnline/problem.php?id=4247 先把挂饰按挂钩数量从大到小排序dp[i][j]前i个挂饰,剩下j个挂钩的最大喜悦值分挂和不挂转移 #include<cstdio>#include<cstring>#include<iostream>#include<algorit 查看详情

bzoj千题计划106:bzoj1014[jsoi2008]火星人prefix

 http://www.lydsy.com/JudgeOnline/problem.php?id=1014 两个后缀的最长公共前缀:二分+hash带修改带插入:splay维护#include<cstdio>#include<cstring>#include<iostream>#defineL100001typedefunsignedlongl 查看详情

bzoj千题计划123:bzoj1027:[jsoi2007]合金

http://www.lydsy.com/JudgeOnline/problem.php?id=1027 因为x+y+z=1,所以z=1-x-y第三维可以忽略 将x,y看做平面上的点 简化问题:若只有两种材料,那么可以合成两点线段上的所有的点 推广到多种材料:若用户点在材料点构成的... 查看详情

bzoj千题计划165:bzoj5127:数据校验

http://www.lydsy.com/JudgeOnline/upload/201712/prob12.pdf 区间的任意一个子区间都满足值域连续等价于区间任意一个长为2的子区间都满足值域连续即区间任意相邻的两个数大的减小的<=1线段树维护即可 #include<cstdio>#include<iost... 查看详情

bzoj千题计划144:bzoj1176:[balkan2007]mokia

http://www.lydsy.com/JudgeOnline/problem.php?id=1176 CDQ分治 #include<cstdio>#include<iostream>#include<algorithm>#definelowbit(x)x&-xusingnamespacestd;#defineN160001#def 查看详情

bzoj千题计划177:bzoj1858:[scoi2010]序列操作

http://www.lydsy.com/JudgeOnline/problem.php?id=1858 2018自己写的第1题,一遍过^_^元旦快乐 #include<cstdio>#include<iostream>#include<algorithm>usingnamespacestd;#defineN100001structnode{ 查看详情

bzoj千题计划142:bzoj3144:[hnoi2013]切糕

http://www.lydsy.com/JudgeOnline/problem.php?id=3144 如果D=2,两个点,高度为4,建图如下   #include<queue>#include<cstdio>#include<cstring>#include<iostream>#include<alg 查看详情

bzoj千题计划242:bzoj4034:[haoi2015]树上操作

http://www.lydsy.com/JudgeOnline/problem.php?id=4034 dfs序,树链剖分 #include<cstdio>#include<iostream>usingnamespacestd;#defineN100001typedeflonglongLL;intn,a[N];intfront[N],nxt[N< 查看详情

bzoj千题计划139:bzoj2229:[zjoi2011]最小割

http://www.lydsy.com/JudgeOnline/problem.php?id=2229  最小割树介绍:http://blog.csdn.net/jyxjyx27/article/details/42750833http://blog.csdn.net/miaomiao_ymxl/article/details/54931876 #include&l 查看详情

bzoj千题计划199:bzoj1055:[haoi2008]玩具取名

http://www.lydsy.com/JudgeOnline/problem.php?id=1055 区间DPdp[i][j][k]表示区间[i,j]能否合成k #include<cstdio>#include<cstring>usingnamespacestd;boolok[4][4][4];chars[201];booldp[201][201][ 查看详情

bzoj千题计划115:bzoj1024:[scoi2009]生日快乐

http://www.lydsy.com/JudgeOnline/problem.php?id=1024 枚举横着切还是竖着切,一边儿分多少块 #include<cstdio>#include<algorithm>usingnamespacestd;doubleS;doubledfs(doublex,doubley,intn){if(n==1)retur 查看详情

bzoj千题计划171:bzoj2456:mode

http://www.lydsy.com/JudgeOnline/problem.php?id=2456 任意删除序列中两个不同的数,众数仍然是众数不停的删,剩下的最后的数一定是众数 具体实现:记录一个当前数和出现次数如果下一个数和当前数不相等,出现次数-1当出现次数... 查看详情

bzoj千题计划236:bzoj2300:[haoi2011]防线修建

http://www.lydsy.com/JudgeOnline/problem.php?id=2300 维护动态凸包,人懒用的set用叉积判断,不要用斜率#include<set>#include<cmath>#include<cstdio>#include<iostream>usingnamespacestd;#defineN10000 查看详情

bzoj千题计划137:bzoj[cqoi2014]危桥

http://www.lydsy.com/JudgeOnline/problem.php?id=3504 往返n遍,即单向2*n遍危桥流量为2,普通桥流量为inf原图跑一遍最大流交换b1,b2再跑一遍最大流如果两次的结果都等于(an+bn)*2则可以 证明参见http://www.cnblogs.com/chenyushuo/p/5139556.ht... 查看详情