HDU 4325 Flowers (線段樹+離散化)

來源:互聯網
上載者:User

去年多校的時候用最暴力的辦法水過了,忽然看到,覺得還是得用正統的方式過了.............


#include <iostream>#include <algorithm>#include <cmath>#include<functional>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <vector>#include <set>#include <queue>#include <stack>#include <climits>//形如INT_MAX一類的#define MAX 100050#define INF 0x7FFFFFFF#define L(x) x<<1#define R(x) x<<1|1using namespace std;inline void RD(int &ret) {    char c;    do {        c = getchar();    } while(c < '0' || c > '9') ;    ret = c - '0';    while((c=getchar()) >= '0' && c <= '9')        ret = ret * 10 + ( c - '0' );}void OT(int a) {    if(a >= 10)OT(a / 10);    putchar(a % 10 + '0');}int n,m,cnt,size;int x[MAX * 3],xx[MAX * 3],le[MAX],ri[MAX],qes[MAX];struct Node {    int l,r,mid;    int add,v;} tr[MAX * 4];void up(int x) {    tr[x].v = tr[L(x)].v + tr[R(x)].v;}void down(int x) {    if(tr[x].add != 0) {        int l,r;        tr[L(x)].add += tr[x].add;        tr[R(x)].add += tr[x].add;        l = tr[L(x)].l;        r = tr[L(x)].r;        tr[L(x)].v += (r - l + 1) * tr[x].add;        //tr[L(x)].v = (r - l + 1) * tr[L(x)].add;        l = tr[R(x)].l;        r = tr[R(x)].r;        tr[R(x)].v += (r - l + 1) * tr[x].add;        //tr[R(x)].v = (r - l + 1) * tr[R(x)].add;        tr[x].add = 0;    }}void build(int l,int r,int x) {    tr[x].l = l;    tr[x].r = r;    tr[x].mid = (l + r) >> 1;    tr[x].v = 0;    tr[x].add = 0;    if(l == r) return ;    build(l,tr[x].mid,L(x));    build(tr[x].mid + 1,r,R(x));}void update(int l,int r,int x,int va) {    if(l <= tr[x].l && r >= tr[x].r) {        tr[x].add += va;        tr[x].v += (tr[x].r - tr[x].l + 1) * va;        //tr[x].v = (tr[x].r - tr[x].l + 1) * tr[x].add;        return ;    }    down(x);    int mid = tr[x].mid;    if(r > mid) update(l,r,R(x),va);    if(l <= mid) update(l,r,L(x),va);    up(x);}int ans = 0;void query(int l,int x) {    if(tr[x].l == tr[x].r) {        ans += tr[x].v;        return ;    }    down(x);    int mid = tr[x].mid;    if(l > mid) query(l,R(x));    if(l <= mid) query(l,L(x));}int main() {    int T;    cin >> T;    int ca = 1;    while(T--) {        RD(n);        RD(m);        cnt = 0;        for(int i=0; i<n; i++) {            RD(le[i]);            RD(ri[i]);            x[cnt++] = le[i];            x[cnt++] = ri[i];        }        for(int i=0; i<m; i++) {            RD(qes[i]);            x[cnt++] = qes[i];        }        sort(x,x+cnt);        size = unique(x,x+cnt) - x;        build(1,size,1);        for(int i=0; i<n; i++) {            int ll = lower_bound(x,x+size,le[i]) - x + 1;            int rr = lower_bound(x,x+size,ri[i]) - x + 1;            update(ll,rr,1,1);        }        printf("Case #%d:\n",ca++);        for(int i=0; i<m; i++) {            int q = lower_bound(x,x+size,qes[i]) - x + 1;            ans = 0;            query(q,1);            OT(ans);            puts("");        }    }    return 0;}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.