http://www.lightoj.com/volume_showproblem.php?problem=1089

题意:给出许多区间,查询某个点所在的区间个数

思路:线段树,由于给出的是区间,查询的是点,考虑将其离线并离散化,普通线段树即可。

/** @Date    : 2016-12-17-20.49
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version :
*/
#include<bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; struct yuu
{
int l, r;
int sum, add;
}tt[N << 4]; void pushdown(int p)
{
if(tt[p].add != 0)
{
tt[p << 1].sum += tt[p].add;
tt[p << 1 | 1].sum += tt[p].add;
tt[p << 1].add += tt[p].add;
tt[p << 1 | 1].add += tt[p].add;
tt[p].add = 0;
}
} void pushup(int p)
{
tt[p].sum = tt[p << 1].sum + tt[p << 1 | 1].sum;
} void build(int l, int r, int p)
{
tt[p].l = l;
tt[p].r = r;
tt[p].sum = 0;
tt[p].add = 0;
if(l == r)
return ;
int mid = (l + r) >> 1;
build(l, mid, p << 1);
build(mid + 1, r, p << 1 | 1);
pushup(p);
} void updata(int l, int r, int v, int p)
{
if(l <= tt[p].l && r >= tt[p].r)
{
tt[p].sum += v;
tt[p].add += v;
return ;
}
pushdown(p);
int mid = (tt[p].l + tt[p].r) >> 1;
//cout <<mid<< endl;
if(l <= mid)
updata(l, r, v, p << 1);
if(r > mid)
updata(l, r, v, p << 1 | 1);
pushup(p);
} int query(int l, int r, int p)
{
if(l <= tt[p].l && r >= tt[p].r)
{
return tt[p].sum;
}
pushdown(p);
int mid = (tt[p].l + tt[p].r) >> 1;
int ans = 0;
if(l <= mid)
ans += query(l, r, p << 1);
if(r > mid)
ans += query(l, r, p << 1 | 1);
return ans;
}
mapq;
map::iterator it;
int l[N], r[N];
int x[N];
int main()
{ int T;
int cnt = 0;
cin >> T;
while(T--)
{
int n, m;
q.clear();
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++)
{
scanf("%d%d", l + i, r + i);
q[l[i]] = 1;
q[r[i]] = 1;
}
for(int i = 0; i < m; i++)
{
scanf("%d", x + i);
q[x[i]] = 1;
} printf("Case %d:\n", ++cnt);
int ct = 0;
for(it = q.begin(); it != q.end(); it++)
{
it->se = ++ct;
}
build(0, ct, 1);
for(int i = 0; i < n; i++)
{ //cout << q[l[i]] <<"~"<<q[r[i]]<< endl;
updata(q[l[i]], q[r[i]], 1, 1);
}
for(int i = 0; i < m; i++)
{
// cout << q[x[i]] << endl;
printf("%d\n", query(q[x[i]], q[x[i]], 1));
}
}
return 0;
}

LightOJ 1089 - Points in Segments (II) 线段树区间修改+离散化的更多相关文章

  1. poj 2528 线段树区间修改+离散化

    Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...

  2. POJ-2528 Mayor's posters (线段树区间更新+离散化)

    题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...

  3. Codeforces Round #442 (Div. 2) E Danil and a Part-time Job (dfs序加上一个线段树区间修改查询)

    题意: 给出一个具有N个点的树,现在给出两种操作: 1.get x,表示询问以x作为根的子树中,1的个数. 2.pow x,表示将以x作为根的子树全部翻转(0变1,1变0). 思路:dfs序加上一个线 ...

  4. 题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)

    Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...

  5. POJ2528:Mayor's posters(线段树区间更新+离散化)

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  6. poj2528 Mayor's posters(线段树区间修改+特殊离散化)

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  7. POJ-2528 Mayor's posters(线段树区间更新+离散化)

    http://poj.org/problem?id=2528 https://www.luogu.org/problem/UVA10587 Description The citizens of By ...

  8. POJ 2528 Mayor's posters (线段树区间更新+离散化)

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

  9. E - Just a Hook HDU - 1698 线段树区间修改区间和模版题

    题意  给出一段初始化全为1的区间  后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...

随机推荐

  1. JDK,JRE,JVM之间的关系

    JDK包括JRE包括JVM http://java-mzd.iteye.com/blog/838514

  2. Linux 环境下svn 服务器搭建

    可使用自己下载的svn安装包,但要安装相关依赖包,yum 安装源提供的稳定版本svn 1.yum -y install subversion 2.创建本地库 mkdir -p /var/svn svn ...

  3. iOS-封装UIPickerView

    创建类WJPickerView继承与UIView ProvinceModel是省市的model,包含属性 @property (nonatomic, strong) NSString *provinc ...

  4. jQuery异步Deferred

    原文链接:http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html 普通 ...

  5. tomcat下部署了多个项目启动报错java web error:Choose unique values for the 'webAppRootKey' context-param in your web.xml files

    应该是tomcat下部署了多个项目且都使用log4j. <!--如果不定义webAppRootKey参数,那么webAppRootKey就是缺省的"webapp.root". ...

  6. php面试必知必会常见问题

    1 说出常用的10个数组方法 我觉得数组比较最能体现PHP基础语法的一个数据结构了,下面给大家列一下常用的10个关于操作数组的函数 in_array(判断数组中是否有某个元素) implode(将数组 ...

  7. Java多线程 -yield用法

    前几天复习了一下多线程,发现有许多网上讲的都很抽象,所以,自己把网上的一些案例总结了一下! 一. Thread.yield( )方法: 使当前线程从执行状态(运行状态)变为可执行态(就绪状态).cpu ...

  8. 【移动端debug-1】css3中box-shadow的溢出问题

    今天做项目遇到一个box-shadow的溢出父容器的问题,如下面的代码中,子容器inner的box-shadow在没有任何设置的情况下是溢出父容器的. 代码: <!DOCTYPE html> ...

  9. 如果使用引用方式引用了js后 则不能再本地写js 因为写了后不会有效果

    如果使用引用方式引用了js后 则不能再本地写js 因为写了后不会有效果

  10. 洛谷 P1678 烦恼的高考志愿

    题目背景 计算机竞赛小组的神牛V神终于结束了万恶的高考,然而作为班长的他还不能闲下来,班主任老t给了他一个艰巨的任务:帮同学找出最合理的大学填报方案.可是v神太忙了,身后还有一群小姑娘等着和他约会,于 ...