题目链接

题意: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. 记一次使用命令行启动部署在tomcat上的应用

    在Eclipes进行程序开发完成后,一般都会直接在Eclipse部署启动,其中的一些启动参数设置都会在其中进行,若用命令行启动,则需要手动配置. 程序开发完成后打成的war包,需要部署到Tomcat应 ...

  2. Oracle 11g 7个压缩包说明

    最初,我以为都要解压.无意间看到的一博客,明白压缩包的含义.哈哈 Oracle11g有多张安装光盘: 文件名称                                              ...

  3. [转]easyui tree 模仿ztree 使用扁平化加载json

    原文地址:http://my.oschina.net/acitiviti/blog/349377 参考文章:http://www.jeasyuicn.com/demo/treeloadfilter.h ...

  4. Web的结构组件

    HTTP代理服务器的作用 Web安全,应用集成,性能优化 1. 代理 In computer networks, a proxy server is a server (a computer syst ...

  5. 【POJ 2187】Beauty Contest 凸包+旋转卡壳

    xuán zhuǎn qiǎ ké模板题 是这么读吧(≖ ‿ ≖)✧ 算法挺简单:找对踵点即可,顺便更新答案. #include<cstdio> #include<cstring&g ...

  6. 【BZOJ 1875】【SDOI 2009】HH去散步

    水啊水,最后ans别忘了%哦! #include<cstdio> #include<cstring> #include<algorithm> using names ...

  7. MyEclipse10安装SVN插件

    一.下载SVN插件subclipse 下载地址:http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID=2240 在打开的网 ...

  8. poj2762 缩点+topo排序

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16486 ...

  9. 基于Oracle的Mybatis 批量插入

    项目中会遇到这样的情况,一次性要插入多条数据到数据库中,有两种插入方法: 方法一: Mybatis本身只支持逐条插入,比较笨的方法,就是遍历一个List,循环中逐条插入,比如下面这段代码 for(Da ...

  10. js-设置焦点

    function CheckForm() { if(document.form1.trainingName.value==""){ alert("培训班名称不能为空!&q ...