Buses and People (segment tree, Poset)

Meaning of the questions: There are n two vehicles, the starting point s end t, t time of departure and to reach. And now m individual, everyone wants from l to r time t, and asked him to take the next available car.
The abstract problem solving, A <= A ', B ' <= B, T '<= T be.
We offline processing. L according to the first starting point that is sort of the starting point and the people's car, get in before everyone inquiry, has been inserted into the car and ensure that the car starting point than people forward.
We continue to build time segment tree (first discrete time), on each axis, the establishment of a right end point, indicating that the time can reach the farthest station. When every human inquiry, it is half the tree line segment, into the left subtree priority to ensure t exam, inquire whether it can meet the right point> =.
(Discrete wrong, unique actually be able to re wrong a few times did not see !!!)

    #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;
    }

Guess you like

Origin www.cnblogs.com/LORDXX/p/11616219.html