HDU-4325 line segment tree + discretization

Source: Internet
Author: User

The time span of this question is too large. If we build the time directly, the memory is obviously insufficient, so we have to consider that there are only 10 ^ 5 flowers, so we need to adopt discretization, but if we only want to discretization the Time of the flowers, when we query a certain time point, the information in the online segment tree does not exist, so we cannot get the correct answer, therefore, we need to discretize the flowers and ask all the time, so that the information is complete.

The Code is as follows:

#include <cstdlib>#include <cstdio>#include <cstring>#include <algorithm>#include <map>using namespace std;int N, M, ti[300005], cnt, Q[100005];struct Flower{    int s, e;    }f[100005];map<int,int>mp;struct Node{    int l, r, lazy;    }e[1200005];void build(int p, int l, int r){    e[p].l = l, e[p].r = r;    e[p].lazy = 0;    if (l != r) {        int mid = (l + r) >> 1;        build(p<<1, l, mid);        build(p<<1|1, mid+1, r);    }}void push_down(int p){    if (e[p].lazy) {        e[p<<1].lazy += e[p].lazy;        e[p<<1|1].lazy += e[p].lazy;        e[p].lazy = 0;    }    }void modify(int p, int l, int r){    if (l == e[p].l && r == e[p].r) {        e[p].lazy += 1;        }    else {        push_down(p);        int mid = (e[p].l + e[p].r) >> 1;        if (r <= mid) {            modify(p<<1, l, r);        }        else if (l > mid) {            modify(p<<1|1, l, r);            }        else {            modify(p<<1, l, mid);            modify(p<<1|1, mid+1, r);            }    } }int query(int p, int pos){    if (e[p].l == e[p].r) {        return e[p].lazy;        }    else {        push_down(p);        int mid = (e[p].l + e[p].r) >> 1;        if (pos <= mid) {            return query(p<<1, pos);            }            else {            return query(p<<1|1, pos);            }    }}int main(){    int T, ca = 0;    scanf("%d", &T);    while (T--) {        cnt = -1;        mp.clear();        scanf("%d %d", &N, &M);        for (int i = 1; i <= N; ++i) {            scanf("%d %d", &f[i].s, &f[i].e);                ti[++cnt] = f[i].s, ti[++cnt] = f[i].e;        }        for (int i = 1; i <= M; ++i) {            scanf("%d", &Q[i]);            ti[++cnt] = Q[i];        }        sort(ti, ti+cnt+1);        cnt = unique(ti, ti+cnt+1) - ti;        for (int i = 0; i < cnt; ++i) {            mp[ti[i]] = i;        }        build(1, 0, cnt-1);        for (int i = 1; i <= N; ++i) {            modify(1, mp[f[i].s], mp[f[i].e]);        }        printf("Case #%d:\n", ++ca);        for (int i = 1; i <= M; ++i) {            printf("%d\n", query(1, mp[Q[i]]));        }    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.