题目链接

题意:n( n <= 50000 ) 个线段,q ( q <= 50000) 个点,问每个点在几个线段上

线段端点的和询问的点的值都很大,所以必须离散化

第一种解法:先把所有的线段端点和询问点,离散处理,然后对于每条选段处理,c[x]++, c[y + 1]--,然后令c[x] = c[x] + c[x - 1],所以c[x]就保存了被几个线段覆盖,然后对对于每个询问点,查找他在离散后的位置,然后直接读取c[],这种方法很巧妙,佩服佩服

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int Max = + ;
int c[ * Max],a[Max],b[Max],querry[Max];
int id[ * Max],cnt;
int main()
{
int n, p, test;
scanf("%d", &test);
for(int t = ; t <= test; t++)
{
printf("Case %d:\n", t);
scanf("%d%d", &n, &p);
memset(c, , sizeof(c));
cnt = ;
for(int i = ; i <= n; i++)
{
scanf("%d%d", &a[i], &b[i]);
id[cnt++] = a[i];
id[cnt++] = b[i];
}
for(int i = ; i <= p; i++)
{
scanf("%d", &querry[i]);
id[cnt++] = querry[i];
}
sort(id, id + cnt);
cnt = unique(id, id + cnt) - id;
for(int i = ; i <= n; i++)
{
int x = lower_bound(id, id + cnt, a[i]) - id;
int y = lower_bound(id, id + cnt, b[i]) - id;
c[x]++;
c[y + ]--;
}
for(int i = ; i < * Max; i++)
c[i] += c[i - ]; for(int i = ; i <= p; i++)
{
int x = lower_bound(id, id + cnt, querry[i]) - id;
printf("%d\n", c[x]);
}
}
return ;
}

线段树也可解,只是RE

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int Max = + ;
struct node
{
int l,r;
int tag,cnt;
};
node tree[ * Max];
int a[ * Max], b[ * Max], c[ * Max], querry[Max];
int tot;
void buildTree(int left, int right, int k)
{
tree[k].l = left;
tree[k].r = right;
tree[k].tag = -;
tree[k].cnt = ;
if(left == right)
return;
int mid = (left + right) / ;
buildTree(left, mid, k * );
buildTree(mid + , right, k * + );
}
void upDate(int left, int right, int k, int newp)
{
if(tree[k].tag != -)
{
tree[k * ].tag = tree[k * + ].tag = tree[k].tag;
tree[k * ].cnt++;
tree[k * + ].cnt++;
tree[k].tag = -;
}
if(tree[k].l == left && tree[k].r == right)
{
tree[k].cnt++;
tree[k].tag = newp;
return;
}
int mid = (tree[k].l + tree[k].r) / ;
if(right <= mid)
upDate(left, right, k * , newp);
else if(mid < left)
upDate(left, right, k * + , newp);
else
{
upDate(left, mid, k * , newp);
upDate(mid + , right, k * + , newp);
}
}
int Search(int k, int value)
{
if(tree[k].l == tree[k].r)
return tree[k].cnt;
if(tree[k].tag != -)
{
tree[k * ].tag = tree[k * + ].tag = tree[k].tag;
tree[k * ].cnt++;
tree[k * + ].cnt++;
tree[k].tag = -;
} int mid = (tree[k].l + tree[k].r) / ;
if(value <= mid)
return Search(k * , value);
else
return Search(k * + , value);
}
int main()
{
int test, n, q;
scanf("%d", &test);
for(int t = ; t <= test; t++)
{
scanf("%d%d", &n, &q);
tot = ;
for(int i = ; i <= n; i++)
{
scanf("%d%d", &a[i], &b[i]);
c[tot++] = a[i];
c[tot++] = b[i];
}
for(int i = ; i <= q; i++)
{
scanf("%d", &querry[i]);
c[tot++] = querry[i];
}
sort(c, c + tot);
tot = unique(c, c + tot) - c;
buildTree(, tot, );
for(int i = ; i <= n; i++)
{
int x = lower_bound(c, c + tot, a[i]) - c + ;
int y = lower_bound(c, c + tot, b[i]) - c + ;
upDate(x, y, , );
}
printf("Case %d:\n", t);
for(int i = ; i <= q; i++)
{
int x = lower_bound(c, c + tot, querry[i]) - c + ;
printf("%d\n", Search(, x));
}
}
return ;
}

未AC

LightOj1089(求点包含几个线段 + 线段树)的更多相关文章

  1. Codevs 1688 求逆序对(权值线段树)

    1688 求逆序对  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 给定一个序列a1,a2,…, ...

  2. POJ 2104 K-th Number ( 求取区间 K 大值 || 主席树 || 离线线段树)

    题意 : 给出一个含有 N 个数的序列,然后有 M 次问询,每次问询包含 ( L, R, K ) 要求你给出 L 到 R 这个区间的第 K 大是几 分析 : 求取区间 K 大值是个经典的问题,可以使用 ...

  3. 【BZOJ4881】5月月赛D 线段游戏 树状数组+set

    Description quailty和tangjz正在玩一个关于线段的游戏.在平面上有n条线段,编号依次为1到n.其中第i条线段的两端点坐 标分别为(0,i)和(1,p_i),其中p_1,p_2,. ...

  4. 【bzoj4881】[Lydsy2017年5月月赛]线段游戏 树状数组+STL-set

    题目描述 quailty和tangjz正在玩一个关于线段的游戏.在平面上有n条线段,编号依次为1到n.其中第i条线段的两端点坐标分别为(0,i)和(1,p_i),其中p_1,p_2,...,p_n构成 ...

  5. HDU3974 Assign the task(多叉树转换为线段+线段树区间染色)

    题目大意:有n个人,给你他们的关系(老板和员工),没有直属上司的人就是整个公司的领导者,这意味着n个人形成一棵树(多叉树).当一个人被分配工作时他会让他的下属也做同样的工作(并且立即停止手头正在做的工 ...

  6. 多校联赛2 Problem2 Warm up 求桥的数目+缩点后的树的直径 当时被不知道原因的爆栈爆到无语了。。

    Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total S ...

  7. POJ 2104 求序列里第K大 主席树裸题

    给定一个n的序列,有m个询问 每次询问求l-r 里面第k大的数字是什么 只有询问,没有修改 可以用归并树和划分树(我都没学过..囧) 我是专门冲着弄主席树来的 对主席树的建树方式有点了解了,不过这题为 ...

  8. xdoj-1319 求树上任意一点的最大距离----利用树的直径

    1 #include <bits/stdc++.h> using namespace std; ; vector < vector <int> > g(N); in ...

  9. 【hdu1542】线段树求矩形面积并

    分割线内容转载自http://hzwer.com/879.html ------------------------------------------------------------------ ...

随机推荐

  1. web安全——代理(nginx)

    场景 过滤非正常用户使用的http请求. 限制正常用户使用的范围(下载速度.访问频率等). 通过架构规划来提升安全. 能自动解决http请求问题. 解决方案 代理自身的安全 千万不要使用root启动! ...

  2. NetworkSocket结构图

    分层思想 NetworkSocket使用分层的思想,分基础层和上层: 1.基础层提供基础通讯,重要的对象有SessionBase.TcpServerBase和TcpClientBase: 2.上层实现 ...

  3. 备忘:maven 错误信息: Plugin execution not covered by lifecycle configuration

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  4. Windows Server+AMD GPU+HDMI时_黑边_不铺满问题的解决办法

    HDMI接显示器或电视,有黑边或者被放大了是个很常见的问题,显卡设置界面里改下Scale或者Overscan/Underscan就行,可问题是WindowsServer版的CCC没有控制颜色对比度和缩 ...

  5. Linq连接查询之左连接、右连接、内连接、全连接、交叉连接、Union合并、Concat连接、Intersect相交、Except与非查询

    内连接查询 内连接与SqL中inner join一样,即找出两个序列的交集 Model1Container model = new Model1Container(); //内连接 var query ...

  6. BGP路由协议详解(完整篇)

    原文链接:http://xuanbo.blog.51cto.com/499334/465596/ 2010-12-27 12:02:45 上个月我写一篇关于BGP协议的博文,曾许诺过要完善这个文档,但 ...

  7. 文本域的宽度和高度应该用cols和rows来控制,还是 用width和height来控制

    文本域宽度如果用cols来控制,缩放网页的时候文本域的宽度不会自动变化 用width来表示就会跟着网页缩放而缩放 看到下面一段文字: 对于内容至上的网页,在禁用CSS的情况下,HTML内容要做到易于阅 ...

  8. openldap+phpadmin的搭建安装

    1.概念介绍 LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP.它是基于X.500标准的,但是简单多了并且可以根据 ...

  9. [转]Hibernate时间总结

    原文地址:http://blog.csdn.net/woshisap/article/details/6543027 1:Hibernate操作时间需要注意的问题 hibernate很大的一个特点就是 ...

  10. Kernel Methods (1) 从简单的例子开始

    一个简单的分类问题, 如图左半部分所示. 很明显, 我们需要一个决策边界为椭圆形的非线性分类器. 我们可以利用原来的特征构造新的特征: \((x_1, x_2) \to (x_1^2, \sqrt 2 ...