码迷,mamicode.com
首页 > 其他好文 > 详细

2018 Multi-University Training Contest 1 - Distinct Values

时间:2019-05-19 23:33:47      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:char   min   turn   ems   space   包含   c++   移动   ini   

set维护

预处理很巧妙,对于完全被大范围包含的小范围我们不用考虑,要处理的只有不完全重合的区间。

因为一个区间可能被下一个区间的一部分包含,所以我们所能选择的数是在变化的,用一个集合来维护,每次取最小值即可。

在读入区间范围的时候,可以用pre数组来存每个区间r对应的最大的区间长度的l,然后再反着用pre[i+1]更新pre[i],可以得到包含每个点的最大区间的l。

在填数字的时候考虑是否在下一个区间,用pl来维护两个区间不重叠的数,pl从l[i-1]移动到l[i+1]-1的过程中,经过的值我们可以再重新插入集合。

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
    int X = 0, w = 0; char ch = 0;
    while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
    while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}
inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
    A ans = 1;
    for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
    return ans;
}
const int N = 100005;
int _, n, m, ret[N], pre[N], l, r;

int main(){

    for(_ = read(); _; _ --){
        n = read(), m = read();
        set<int> s;
        for(int i = 1; i <= n; i ++)
            pre[i] = i, s.insert(i);
        for(int i = 1; i <= m; i ++){
            l = read(), r = read();
            pre[r] = min(pre[r], l);
        }
        for(int i = n - 1; i >= 1; i --)
            pre[i] = min(pre[i], pre[i + 1]);
        int pl = 1;
        for(int i = 1; i <= n; i ++){
            while(pl < pre[i]) s.insert(ret[pl ++]);
            ret[i] = *(s.begin());
            s.erase(ret[i]);
        }
        for(int i = 1; i <= n; i ++){
            printf("%d%c", ret[i], " \n"[i==n]);
        }
    }
    return 0;
}

2018 Multi-University Training Contest 1 - Distinct Values

标签:char   min   turn   ems   space   包含   c++   移动   ini   

原文地址:https://www.cnblogs.com/onionQAQ/p/10891326.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!