• hdu 5023 线段树


    成端更新+统计区间内的值

    挺模板的题。。。

    一开始没想起来用set统计,傻傻地去排序了【大雾

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<algorithm>
      4 #include<cstring>
      5 #include<set>
      6 using namespace std;
      7 struct
      8 {
      9     int l,r;
     10     int dat;
     11 }t[3000010];
     12 char ch;
     13 int N,M,num,aa,bb,cc;
     14 set<int> s;
     15 
     16 //建树:
     17 void build(int l,int r,int o)
     18 {
     19     if (o>num) num=o;
     20     t[o].l=l;   t[o].r=r;   t[o].dat=2;
     21     if (l!=r)
     22     {
     23         int mid=(l+r)/2;
     24         build(l,mid,2*o);
     25         build(mid+1,r,2*o+1);
     26     }
     27 }
     28 
     29 //push_down:(向下更新一层)
     30 void push_down(int o)
     31 {
     32     if (t[o].dat!=0)
     33     {
     34         t[o<<1].dat=t[o].dat;
     35         t[o<<1|1].dat=t[o].dat;
     36         t[o].dat=0;
     37     }
     38 }
     39 
     40 //更新:cin>>ml>>mr>>md;    update(ml,mr,1,md);        ->令a[ml..mr]=md
     41 void update(int l,int r,int o,int md)
     42 {
     43     if (o>num) return;
     44     int tl=t[o].l,tr=t[o].r;
     45     if ((tl==l)&&(tr==r))
     46     {
     47         t[o].dat=md;
     48         return;
     49     }
     50     if(t[o].dat==md)    return;
     51     int mid=(tl+tr)>>1;
     52     //if (tl==tr) return;
     53     push_down(o);
     54     if (r<=mid)
     55         update(l,r,o<<1,md);
     56     else if (l>mid)
     57         update(l,r,o<<1|1,md);
     58     else
     59     {
     60         update(l,mid,o<<1,md);
     61         update(mid+1,r,o<<1|1,md);
     62     }
     63 }
     64 
     65 void sum(int l,int r,int o)
     66 {
     67     if(o>num)   return;
     68     int tl=t[o].l,tr=t[o].r;
     69     //if((l==tl)&&(r==tr))
     70     if(t[o].dat)                //经过的地方若t[o].dat不为0就加入set,而不是对每个tl==tr的节点统计。lazy思想的精髓
     71     {
     72         s.insert(t[o].dat);
     73         return;
     74     }
     75     else
     76     {
     77         int mid=(tl+tr)/2;
     78         if(r<=mid)
     79             sum(l,r,2*o);
     80         else if (l>mid)
     81             sum(l,r,2*o+1);
     82         else
     83         {
     84             sum(l,mid,2*o);
     85             sum(mid+1,r,2*o+1);
     86         }
     87     }
     88 }
     89 
     90 int main()
     91 {
     92     while(cin>>N>>M)
     93     {
     94         if((N==0)&&(M==0))
     95             break;
     96         build(1,N,1);
     97         for(int i=1;i<=M*2;i++)
     98         {
     99             scanf("%c ",&ch);
    100             //cout<<"iiiiiiii"<<i<<" "<<M<<endl;
    101             if(ch=='P')
    102             {
    103                 scanf("%d%d%d",&aa,&bb,&cc);
    104                 update(aa,bb,1,cc);
    105             }
    106             else if (ch=='Q')
    107             {
    108                 s.clear();
    109                 scanf("%d%d",&aa,&bb);
    110                 sum(aa,bb,1);
    111                 //printf("SS:  %lu
    ", s.size());
    112                 int tmp=s.size();
    113                 for(set<int>::iterator it=s.begin();it!=s.end();it++)
    114                 {
    115                     printf("%d",*it);
    116                     tmp--;
    117                     if(tmp!=0)  printf(" ");
    118                 }
    119                 printf("
    ");
    120             }
    121         }
    122     }
    123     return 0;
    124 }
    View Code

    Ref:http://blog.csdn.net/lyhvoyage/article/details/39518963

  • 相关阅读:
    内排序小结
    HTML初体验
    android动画介绍之 自己定义Animation动画实现qq抖一抖效果
    iOS 把数据库文件打包到mainbundle中,查找不到路径的解决的方法;以及在删除bundle中文件的可行性
    随着ScrollView的滑动,渐渐的运行动画View
    MFC Month Calendar Control 控件使用
    Codeforces Round #148 (Div. 1)
    Codeforces 486E LIS of Sequence(线段树+LIS)
    2014年百度之星资格赛第四题Labyrinth
    UVA10519
  • 原文地址:https://www.cnblogs.com/pdev/p/4686696.html
Copyright © 2020-2023  润新知