Nested Segments

题目链接

  • 题意:

    给n个区间,任意两个区间要么完全包含,要么相离。m个查询,每次输入一个整数,输出这个数属于哪个区间,如果有多个区间,选长度最小的

    (1 ≤ n ≤ 10e5)、区间短点值(1 ≤ ai < bi ≤ 10e9)、(1 ≤ m ≤ 10e5)、查询值(1 ≤ cj ≤ 10e9)

  • 分析:

    离散化加set输出答案。离散化的时候,将[a, b]加入数组中时,插入a-1,a,b,b+1;同时,将m个查询也离散化,加入数组中(这里也可以不插入,处理会稍稍麻烦点)

const int MAXN = 1100000;

struct Seg
{
    int l, r;
    bool operator< (const Seg& rhs)const
    {
        return r - l < rhs.r - rhs.l;
    }
} ipt[MAXN];

VI split;
int ans[MAXN], q[MAXN];
set<int> st;
set<int>::iterator it;
int main()
{
    int n, m;
    while (~RI(n))
    {
        split.clear();
        CLR(ans, -1);
        FE(i, 1, n)
        {
            RII(ipt[i].l, ipt[i].r);
            split.push_back(ipt[i].l);
            split.push_back(ipt[i].r);
            split.push_back(ipt[i].l - 1);
            split.push_back(ipt[i].r + 1);
        }
        RI(m);
        REP(i, m)
        {
            RI(q[i]);
            split.push_back(q[i]);
        }
        sort(all(split));
        split.erase(unique(all(split)), split.end());
        FE(i, 0, split.size())
            st.insert(i);
        st.insert(INF);
        FED(i, n, 1)
        {
            int l = lower_bound(all(split), ipt[i].l) - split.begin();
            int r = lower_bound(all(split), ipt[i].r) - split.begin();
            it = st.lower_bound(l);
            while (*it <= r)
            {
                ans[*it] = i;
                st.erase(it++);
            }
        }
        REP(i, m)
        {
            int id = lower_bound(all(split), q[i]) - split.begin();
            WI(ans[id]);
        }
    }
    return 0;
}

Nested Segments,布布扣,bubuko.com

时间: 2024-08-28 15:14:44

Nested Segments的相关文章

URAL1987. Nested Segments 线段树

1987. Nested Segments Time limit: 1.0 second Memory limit: 64 MB You are given n segments on a straight line. For each pair of segments it is known that they either have no common points or all points of one segment belong to the second segment. Then

bnu36905 Nested Segments 离散化+线段树

bnu36905 Nested Segments 离散化+线段树区间更新 也可以用离散化+set(或双向链表) #include <cstdio> #include <ctime> #include <cstdlib> #include <cstring> #include <queue> #include <string> #include <set> #include <stack> #include &l

codeforces 652D - Nested Segments

You are given n segments on a line. There are no ends of some segments that coincide. For each segment find the number of segments it contains. Input The first line contains a single integer n (1?≤?n?≤?2·105) - the number of segments on a line. Each

URAL 1987. Nested Segments(数学 &amp; 线段树)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1987 You are given n segments on a straight line. For each pair of segments it is known that they either have no common points or all points of one segment belong to the second segment. Then m queries

codeforces 652D Nested Segments 离散化+树状数组

题意:给你若干个区间,询问每个区间包含几个其它区间 分析:区间范围比较大,然后离散化,按右端点排序,每次更新树状数组中的区间左端点,查询区间和 注:(都是套路) #include<cstdio> #include<cstring> #include<queue> #include<cstdlib> #include<algorithm> #include<vector> #include<cmath> using name

Educational Codeforces Round 10 D. Nested Segments (树状数组)

题目链接:http://codeforces.com/problemset/problem/652/D 给你n个不同的区间,L或者R不会出现相同的数字,问你每一个区间包含多少个区间. 我是先把每个区间看作整体,按照R从小到大排序.然后从最小的R开始枚举每个区间,要是枚举到这个区间L的时候,计算之前枚举的区间有多少个Li在L之后,那么这些Li大于L的区间的数量就是答案.那我每次枚举的时候用树状数组add(L , 1) 说明在L这个位置上出现了区间,之后枚举的时候计算L之前的和,然后i - 1 -

CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组

题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点值离散化,按照左端点从大到小排序,顺着这个顺序处理所有线段,那么满足在它内部的线段一定是之前已经扫到过的.用树状数组判断有多少是在右端点范围内. 1 #include <iostream> 2 #include <vector> 3 #include <algorithm>

[转]Patching the Mach-o Format the Simple and Easy Way

From:http://secureallthethings.blogspot.jp/2014/08/patching-mach-o-format-simple-and-easy.html I'm relatively new to mac research. ?So when I find something new, that seems cutting edge, but relatively simple I question it. Has anyone else done this

Spring Cloud ZooKeeper集成Feign的坑2,服务调用了一次后第二次调用就变成了500,错误:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.n

错误如下: 2017-09-19 15:05:24.659 INFO 9986 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]56528192: startup date [Tue Sep 19 15:05:24 CST 2017]; root of context hierarchy 2017-09-19 15:05:24.858 INFO 9986 --