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. 04慕课网《进击Node.js基础(一)》HTTP讲解

    HTTP:通信协议 流程概述: http客户端发起请求,创建端口默认8080 http服务器在端口监听客户端请求 http服务器向客户端返回状态和内容 稍微详细解析: 1.域名解析:浏览器搜素自身的D ...

  2. OpenCV学习笔记——Mat类型数据存储

    CV_[The number of bits per item][Signed or Unsigned][Type Prefix]C[The channel number] 比如 CV_8UC3 表示 ...

  3. python __call__ 函数

    __call__ Python中有一个有趣的语法,只要定义类型的时候,实现__call__函数,这个类型就成为可调用的. 换句话说,我们可以把这个类型的对象当作函数来使用,相当于 重载了括号运算符. ...

  4. Struts2:<s:action>的使用

    <s:action name=”actionName” namespace=”/” executeResult=”true”> <s:action>可以在jsp中直接调用act ...

  5. Halcon 学习笔记3 仿射变换

    像素的减少 开运算(较少) 腐蚀(去除更多) 对灰度图像的开运算或腐蚀 相当于将灰度图像变暗 像素增加 闭运算(较少) 膨胀(较多) 对灰度图像的闭运算或膨胀 相当于将灰度图像变亮 仿射变换 另外一种 ...

  6. java 基础 --final--008

    finally:被finally控制的语句一定会执行,但是如果执行之前jvm退出了,就不会执行了.比如System.exit(0);final:常见的可以修饰类(该类不能被继承) 方法(方法不能被重写 ...

  7. 网络流量统计using ADB

    /proc/net/xt_qtaguid/stats 基本覆盖目前所有机型且统计流量全面 adb shell cat /proc/net/xt_qtaguid/stats | grep (uid#) ...

  8. php 随机密码和盐 来自wordpress

    生成随机密码或盐. Generate keys and salts using secure CSPRNG $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJ ...

  9. HDFS集中式的缓存管理原理与代码剖析--转载

    原文地址:http://yanbohappy.sinaapp.com/?p=468 Hadoop 2.3.0已经发布了,其中最大的亮点就是集中式的缓存管理(HDFS centralized cache ...

  10. Django文字教程

    user-----URL对应关系-------视图函数def func1()-------------- 函数给用户返回的实质上就是一个字符串,过程:通过open函数打开HTML,把HTML读到内存中 ...