CODEVS1191|數軸染色|線段樹

來源:互聯網
上載者:User

標籤:

題目描述 Description
在一條數軸上有N個點,分別是1~N。一開始所有的點都被染成黑色。接著
我們進行M次操作,第i次操作將[Li,Ri]這些點染成白色。請輸出每個操作執行後
剩餘黑色點的個數。
輸入描述 Input Description
輸入一行為N和M。下面M行每行兩個數Li、Ri
輸出描述 Output Description
輸出M行,為每次操作後剩餘黑色點的個數。
範例輸入 Sample Input
10 3
3 3
5 7
2 8
範例輸出 Sample Output
9
6
3
資料範圍及提示 Data Size & Hint
資料限制
對30%的資料有1<=N<=2000,1<=M<=2000
對100%資料有1<=Li<=Ri<=N<=200000,1<=M<=200000

#include<iostream>#include<cstdio>using namespace std;int x,y,n,m;struct tree{   int l,r,v;}t[800001];void build(int now,int x,int y){    t[now].l=x; t[now].r=y;    if (x==y) { t[now].v=1; return; }    int mid=(x+y)>>1;    build(now<<1,x,mid);    build(now<<1|1,mid+1,y);    t[now].v=t[now<<1].v+t[now<<1|1].v;    }void change(int k,int x,int y){    if (t[k].v==0) return;    int l=t[k].l,r=t[k].r;    if (l==x && r==y) { t[k].v=0; return; }    int mid=(l+r)>>1;    if (mid>=y) change(k<<1,x,y);        else if (mid<x) change(k<<1|1,x,y);        else         {            change(k<<1,x,mid);            change(k<<1|1,mid+1,y);        }    t[k].v=t[k<<1].v+t[k<<1|1].v;}int main(){        cin >> n >> m;    build(1,1,n);    for (int i=1; i<=m; i++)    {        cin >> x >> y;        change(1,x,y);        printf("%d\n",t[1].v);    }    return 0;}

 

CODEVS1191|數軸染色|線段樹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.