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

Buses and People (线段树,偏序集)

时间:2019-10-01 22:09:26      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:getch   err   for   处理   struct   线段   people   long   fine   

题意:有n两车,起点s终点t,t时间发车并达到。现在又m个人,每一个人想从l到r时间为t,问他能搭乘的最近一班汽车。
抽象出来的问题为,解决,A<=A‘,B‘<=B,T‘<=T的问题。
我们离线处理。先按照l即车的起点和人的起点排序,得到在每一个人查询前,已经插入进去了车子且保证车子起点比人靠前。
我们继续建立时间线段树(先离散化了时间),在每一个时间轴上,建立一个右端点,表示这个时间所能达到的最远站。每一次对人查询的时候,就在线段树上二分,优先进入左子树,保证t考前,查询是否能满足右端点>=。
(离散化写错了,unique居然能写错re了几次都没看见!!!)

    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<cstdlib>
    #include<climits>
    #include<stack>
    #include<vector>
    #include<queue>
    #include<set>
    #include<map>
    //#include<regex>
    #include<cstdio>
    #define up(i,a,b)  for(int i=a;i<b;i++)
    #define dw(i,a,b)  for(int i=a;i>b;i--)
    #define upd(i,a,b) for(int i=a;i<=b;i++)
    #define dwd(i,a,b) for(int i=a;i>=b;i--)
    //#define local
    typedef long long ll;
    const double esp = 1e-6;
    const double pi = acos(-1.0);
    const int INF = 0x3f3f3f3f;
    const int inf = 1e9;
    using namespace std;
    int read()
    {
        char ch = getchar(); int x = 0, f = 1;
        while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
        while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
        return x * f;
    }
    typedef pair<int, int> pir;
    #define lson l,mid,root<<1
    #define rson mid+1,r,root<<1|1
    #define lrt root<<1
    #define rrt root<<1|1
    const int N = 2e5 + 10;
    struct node { ll l, r, t, op,pos; bool operator<(const node a) { return l == a.l ? op < a.op:l<a.l; } }a[N<<1];
    vector<ll>time;
    ll tree[N << 2];
    int id[N << 2];
    int ans[N<<2];
    int n, m;
    void pushup(int root)
    {
        tree[root] = max(tree[lrt], tree[rrt]);
    }
    void build(int l, int r, int root)
    {
        id[root] = -1;
        if (l == r)
        {
            tree[root] = -1;
            return;
        }
        int mid = (l + r) >> 1;
        build(lson);
        build(rson);
        pushup(root);
    }
    void update(int l, int r, int root, int pos, ll val,int idk)
    {
        if (l == r)
        {
            tree[root] = val;
            id[root] = idk;
            return;
        }
        int mid = (l + r) >> 1;
        if (pos <= mid)update(lson, pos, val,idk);
        else update(rson, pos, val,idk);
        pushup(root);
    }
    ll querry(int l, int r, int root, int lf, int rt, int val)
    {
        if (tree[root] < val)
        {
            return -1;
        }
        if (l == r)
        {
            return id[root];
        }
        int mid = (l + r) >> 1;
        int ans = -1;
        if (lf <= mid)
        {
            ans = querry(lson, lf, rt, val);
            if (ans >= 0)return ans;
        }
        if (rt > mid)return querry(rson, lf, rt, val);
    }
    int main()
    {
        n = read(), m = read();
        int x, y, z;
        up(i, 0, n)
        {
            x = read(), y = read(), z = read();
            time.push_back(z);
            a[i] = node{ x,y,z,0,i };
        }
        up(i, 0, m)
        {
            x = read(), y = read(), z = read();
            time.push_back(z);
            a[n+i] = node{ x,y,z,1,i };
        }
        sort(time.begin(), time.end());
        time.erase(unique(time.begin(), time.end()),time.end());
        int len = time.size();
        sort(a, a + n + m);
        build(1, len, 1);
        up(i, 0, n + m)
        {
            if (a[i].op == 0)
            {
                int pos = lower_bound(time.begin(), time.end(), a[i].t)-time.begin()+1;
                update(1, len, 1, pos,a[i].r,a[i].pos);
            }
            else
            {
                int pos = lower_bound(time.begin(), time.end(), a[i].t) - time.begin() + 1;
                ll q = querry(1, len, 1, pos, len,a[i].r);
                ans[a[i].pos] = q == -1 ? -1 : q+1;
            }
        }
        up(i, 0, m)cout << ans[i] << " ";
        return 0;
    }

Buses and People (线段树,偏序集)

标签:getch   err   for   处理   struct   线段   people   long   fine   

原文地址:https://www.cnblogs.com/LORDXX/p/11616219.html

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