题目链接

题意: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. 比较Windows Azure 网站(Web Sites), 云服务(Cloud Services)and 虚机(Virtual Machines)

    Windows Azure提供了几个部署web应用程序的方法,比如Windows Azure网站.云服务和虚拟机.你可能无法确定哪一个最适合您的需要,或者你可能清楚的概念,比如IaaS vs PaaS ...

  2. java的HashCode方法

    总的来说,Java中的集合(Collection)有两类,一类是List,再有一类是Set. 前者集合内的元素是有序的,元素可以重复: 后者元素无序,但元素不可重复. 要想保证元素不重复,可两个元素是 ...

  3. 深入理解拉格朗日乘子法(Lagrange Multiplier) 和KKT条件

    [整理]   在求解最优化问题中,拉格朗日乘子法(Lagrange Multiplier)和KKT(Karush Kuhn Tucker)条件是两种最常用的方法.在有等式约束时使用拉格朗日乘子法,在有 ...

  4. 屠龙之路_击败DB小boss_FifthDay

    摘要:服务器大魔王被击败的消息传到了恶龙boss那里,恶龙大怒派出了自己的首级大将DB人称小boss,但小boss的名号并没有吓到七位屠龙勇士,经过他们齐心协力的进攻,最终击败了DB,小boss临死前 ...

  5. 1010每次备份我的MySQL数据库

    转自http://bbs.csdn.net/topics/320136534 在windows平台下面 /*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/* ...

  6. Ubuntu14.04安装MySql

    我们要确保Ubuntu14.04的服务器是可以上网的,这里我就不操作,这个比较简单.由于我的服务器是用Cobbler部署的,所以要更改源. # vi /etc/apt/source.list   我这 ...

  7. AndroidStudio修改项目名称

    项目名称修改了,想修改Android Studio 中 project的名字 右键project 的名字,refactor - rename ,填写好新名字后修改,被提示 “can’t rename ...

  8. Elasticsearch 权威指南

    Elasticsearch 权威指南 http://fuxiaopang.gitbooks.io/learnelasticsearch/content/index.html

  9. struts2的核心和工作原理

    struts2的核心和工作原理 设计目标 Struts设计的第一目标就是使MVC模式应用于web程序设计.技术优势 Struts2有两方面的技术优势,一是所有的Struts2应用程序都是基于clien ...

  10. IntelliJ_编译一直报错“找不到符号”

    执行maven compile时一直报错"找不到符号",类 XXX 各种clean.compile都不行 最后执行Rebuild Project一次后解决   执行rebuild ...