Buses and People CodeForces 160E dimensional partial sequence segment tree +

Buses and People CodeForces 160E dimensional partial sequence segment tree +

The meaning of problems

Given N triples (a, b, c), the M existing query, given a query each triplet (a ', b', c '), seeking to satisfy a <a', b ' <b, c '<c tuple number corresponding to the smallest c.

Problem-solving ideas

Three-dimensional partial order problem, is the first time I do, check the problem solution.

A big brother says so, the original blog first, offline processing all inquiries, these N + M tuples arrive according to a small sort, if a same, given the tuple should be at the inquiry tuple prior to. After ordering to ensure that for any tuple asked, the answer must appear before a given tuple of the tuple. Because C is required to meet the minimum conditions should be established tree line on C. C discretization of operation, establishing the segment tree (b, c), the subscripts with C, B is the weight. Segment tree maintained in the maximum value B, the segment tree when asked to half.

Code implementation (see code is easy)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=2e5+7;
struct node{
    int st, ed, t, id;
    bool friend operator < (node a, node b)
    {
        return a.st==b.st ? a.id < b.id : a.st < b.st;
    }
}a[maxn];
vector<int> v;
int tot;
int maxx[maxn<<2], id[maxn<<2];
int ans[maxn>>1];
int n, m;

void update(int rt, int l, int r, int pos, int val, int ID)
{
    if(l==r)
    {
        //if(maxx[rt] < val)
        {
            maxx[rt]=val;
            id[rt]=ID;
        }
        return ;
    }   
    int mid=(l+r)>>1;
    if(pos<=mid)
        update(rt<<1, l, mid, pos, val, ID);
    else 
        update(rt<<1|1, mid+1, r, pos, val, ID);
    maxx[rt]=max(maxx[rt<<1], maxx[rt<<1|1]);
}
int query(int rt, int l, int r, int x, int y, int val)
{
    if(l==r) return id[rt]; 
    int mid=(l+r)>>1;
    int ret=-1;
    if(y<=mid)
    {
        if(maxx[rt<<1] >= val) ret=query(rt<<1, l, mid, x, y, val);
    }
    else if(x > mid)
    {
        if(maxx[rt<<1|1] >= val) ret=query(rt<<1|1, mid+1, r, x, y, val);   
    }
    else 
    {
        if( maxx[rt<<1] >= val ) ret=query(rt<<1, l, mid, x, mid, val);
        if(ret==-1 && maxx[rt<<1|1] >= val) ret=query(rt<<1|1, mid+1, r, mid+1, y, val);
    }
    return ret;
}
int main()
{
    scanf("%d%d", &n, &m);
    for(int i=1; i<=n+m; i++)
    {
        scanf("%d%d%d", &a[i].st, &a[i].ed, &a[i].t);
        a[i].id=i;
        v.push_back(a[i].t);
    }
    sort(v.begin() , v.end());
    v.erase( unique(v.begin() , v.end() ), v.end() );
    tot=v.size() ;
    sort(a+1, a+n+m+1); 
    for(int i=1; i<=n+m; i++)
    {
        a[i].t = lower_bound(v.begin() , v.end() , a[i].t) - v.begin() +1;  
    }       
    for(int i=1; i<=n+m; i++)
    {
        if(a[i].id<=n)
            update(1, 1, tot, a[i].t, a[i].ed, a[i].id);
        else 
            ans[a[i].id - n]=query(1, 1, tot, a[i].t, tot, a[i].ed);
    }
    for(int i=1; i<=m; i++)
    {
        printf("%d ", ans[i]);
    }
    return 0;
} 

Guess you like

Origin www.cnblogs.com/alking1001/p/11440850.html