正题
题意
一条长m线,有n条长度不同的线段,查询x到x+1有多少条线
解题思路
标记直接覆盖颜色数,然后找到那个点,之后向上到根节点把所有叠加的线统计
代码
#include<cstdio>
#include<cstring>
using namespace std;
struct xjq{int l,r,cover;
}tree[400000];
int n,ll,rr,w,s,cl,h,m;
void build(int x,int a,int b)//建树
{tree[x].l=a;tree[x].r=b;if (b-a==1) return;else{int m=(a+b)/2;build(x*2,a,m);build(x*2+1,m,b);}
}
void inster(int x,int a,int b)//插入
{if (tree[x].l==a && tree[x].r==b){tree[x].cover++;return;//标记数}int m=tree[x*2].r;if (b<=m) inster(x*2,a,b);else if (a>=m) inster(x*2+1,a,b);else{inster(x*2,a,m);inster(x*2+1,m,b);}return;
}
void find(int x,int a,int b)//查找点
{if (tree[x].l==a && tree[x].r==b){h=x;return;}int m=tree[x*2].r;if (b<=m) find(x*2,a,b);else if (a>=m) find(x*2+1,a,b);else{find(x*2,a,m);find(x*2+1,m,b);}
}
int main()
{scanf("%d%d",&m,&n);build(1,1,m);for (int i=1;i<=n;i++){scanf("%d%d",&ll,&rr);inster(1,ll,rr);}scanf("%d%d",&ll,&rr);s=0;find(1,ll,rr);while (h!=0)//向上统计{if(tree[h].cover>0) s+=tree[h].cover;h/=2;}printf("%d\n",s);
}