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. JavaScript与OC的交互-WebViewJavascriptBridge

    WebViewJavascriptBridge实现了在使用UIWebView时JS与ios 的Objective-C nativecode之间的互相调用, 支持的功能有消息发送.接收.消息处理器的注册 ...

  2. jdbc 5.0

    1.事务 事务将单个SQL语句或一组SQL语句视为一个逻辑单元,如果任何语句失败,整个事务将失败. jdbc的MySQL驱动程序中的事务默认是自动提交. 默认情况下,每个SQL语句在完成后都会提交到数 ...

  3. PAT 甲级 1019 General Palindromic Number

    https://pintia.cn/problem-sets/994805342720868352/problems/994805487143337984 A number that will be ...

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

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

  5. utuntu下安装pip&pip3

    在utuntu下建议不要使用apt-get install 安装pip,会出现很多问题. 建议使用如下方式安装: wget https://bootstrap.pypa.io/get-pip.py - ...

  6. mysql(四)log

    慢查询: https://blog.csdn.net/leshami/article/details/39829605 日志组成: https://blog.csdn.net/leshami/arti ...

  7. bzoj2669-局部极小值

    题意 有一个 \(n\times m\) 的矩阵,其中每个数都是 \([1,n\times m]\) 中的一个,不会重复.有一些地方的值比周围的8个位置都小(如果有的话).给出这些位置,求这样的矩阵有 ...

  8. 【bzoj1005】[HNOI2008]明明的烦恼 Prufer序列+高精度

    题目描述 给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连线,可产生多少棵度数满足要求的树? 输入 第一行为N(0 < N < = 1000),接下来N行,第i+1行给出第i ...

  9. 抽屉点赞及jQuery CSS操作

    1.需要用到的知识点: CSS处理 $('t1').css('color','red') 点赞: -$('t1').append() -$('t1').remove() -setInterval -o ...

  10. [HEOI2014]人人尽说江南好 博弈论

    题面 题面 题解 感觉这题挺神仙的,根据一些奇奇怪怪的证明可以得到: 最后的终止状态一定是\(m, m, m, m, .... n \% m\). 因此我们可以O(1)计算到终止状态所需步数,然后根据 ...