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. 团队项目选题报告(I know)

    一.团队成员及分工 团队名称:I know 团队成员: 陈家权:选题报告word撰写 赖晓连:ppt制作,原型设计 雷晶:ppt制作,原型设计 林巧娜:原型设计,博客随笔撰写 庄加鑫:选题报告word ...

  2. 关于Filter的一点误解

    之前一直以为请求达到Web应用时,经过过滤器1,过滤器2……,处理后产生响应再经过过滤器n……过滤器2,过滤器1.这样的阐述似乎没有问题,但我的理解却有问题.比如过滤器1的doFilter方法执行了一 ...

  3. DB2 日志

    跟Oracle类似DB2也分为两个模式,日志循环vs归档日志,也就是非归档和归档模式,下面对这两种模式做简单的介绍. 日志循环 日志循环是默认方式,也就是非归档模式,这种模式只支持backup off ...

  4. 初识 es6之 const

    const声明一个只读的常量.一旦声明,常量的值就不能改变. 例子: const a=12; a=2;//报错,const 声明的是常量,不能改 const声明的变量不得改变值,这意味着,const一 ...

  5. MiniOS系统

    实验一  命令解释程序的编写 一.目的和要求 1. 实验目的 (1)掌握命令解释程序的原理: (2)*掌握简单的DOS调用方法: (3)掌握C语言编程初步. 2.实验要求 编写类似于DOS,UNIX的 ...

  6. PAT L1-034 点赞

    https://pintia.cn/problem-sets/994805046380707840/problems/994805098188750848 微博上有个“点赞”功能,你可以为你喜欢的博文 ...

  7. 设计模式PHP篇(三)————装饰器模式

    简单的用php实现了装饰器模式: <?php /** *简单的装饰器模式 */ class PrintText { protected $decorators = []; public func ...

  8. php框架中常用的设计模式

    1.单例模式 //单例模式 class Demo { private static $obj; private function __construct() { } private function ...

  9. HDU4675_GCD of Sequence

    很有意思的一个数论题. 是这样的,给你一个数数组a[i],其中给出的每一个数字和要求的数字方位都是[1,m],现在问你根据a[]构造一个b[],且a和b中间的不相等的元素的个数恰好有k个. 现在问你g ...

  10. BZOJ4887 Tjoi2017可乐(动态规划+矩阵快速幂)

    设f[i][j]为第i天到达j号城市的方案数,转移显然,答案即为每天在每个点的方案数之和.矩乘一发即可. #include<iostream> #include<cstdio> ...