• poj 2667 hotel 线段树


    基本和poj 1823 hotel基本一样 只需加一个find函数

      1 #include<iostream>
    2 #include<cmath>
    3 using namespace std;
    4 #define MAXN 50001
    5 struct node
    6 {
    7 int lmax,rmax,max;
    8 int left,right;
    9 int flag;
    10 int r;
    11 };
    12 node tree[MAXN*4];
    13 void build(int i)
    14 {
    15 tree[i].lmax=tree[i].rmax=tree[i].max=tree[i].right-tree[i].left+1;
    16 tree[i].flag=0;
    17 tree[i].r=tree[i].left;
    18 if(tree[i].left==tree[i].right) return ;
    19 int mid=(tree[i].left+tree[i].right)/2;
    20 tree[2*i].left=tree[i].left; tree[2*i].right=mid;
    21 tree[2*i+1].left=mid+1; tree[2*i+1].right=tree[i].right;
    22 build(2*i); build(2*i+1);
    23 }
    24
    25 void insert(int i,int x,int y,int c)
    26 {
    27 if(tree[i].left==x&&tree[i].right==y)
    28 {
    29 tree[i].flag=c;
    30 if(c==1) tree[i].r=tree[i].lmax=tree[i].rmax=tree[i].max=0;
    31 else
    32 {
    33 tree[i].lmax=tree[i].rmax=tree[i].max=tree[i].right-tree[i].left+1;
    34 tree[i].r=tree[i].left;
    35 }
    36 return ;
    37 }
    38 if(tree[i].flag==0)
    39 {
    40 tree[2*i].r=tree[2*i].left;
    41 tree[2*i+1].r=tree[2*i+1].left;
    42 tree[2*i].flag=tree[2*i+1].flag=0;
    43 tree[2*i].lmax=tree[2*i].rmax=tree[2*i].max=tree[2*i].right-tree[2*i].left+1;
    44 tree[2*i+1].lmax=tree[2*i+1].rmax=tree[2*i+1].max=tree[2*i+1].right-tree[2*i+1].left+1;
    45 }
    46 if(tree[i].flag==1)
    47 {
    48 tree[2*i].r=0;
    49 tree[2*i+1].r=0;
    50 tree[2*i].flag=tree[2*i+1].flag=1;
    51 tree[2*i].lmax=tree[2*i].rmax=tree[2*i].max=0;
    52 tree[2*i+1].lmax=tree[2*i+1].rmax=tree[2*i+1].max=0;
    53 }
    54 int mid=(tree[i].left+tree[i].right)/2;
    55 if(y<=mid) insert(2*i,x,y,c);
    56 else if(x>mid) insert(2*i+1,x,y,c);
    57 else
    58 {
    59 insert(2*i,x,mid,c);
    60 insert(2*i+1,mid+1,y,c);
    61 }
    62 if(tree[2*i].flag==0&&tree[2*i+1].flag==0)
    63 {
    64 tree[i].r=tree[i].left;
    65 tree[i].flag=0;
    66 tree[i].lmax=tree[i].rmax=tree[i].max=tree[i].right-tree[i].left+1;
    67 }
    68 else if(tree[2*i].flag==0&&tree[2*i+1].flag==1)
    69 {
    70 tree[i].r=tree[i].left;
    71 tree[i].flag=2;
    72 tree[i].lmax=tree[i].max=tree[2*i].max;
    73 tree[i].rmax=0;
    74 }
    75 else if(tree[2*i].flag==1&&tree[2*i+1].flag==0)
    76 {
    77 tree[i].r=mid+1;
    78 tree[i].flag=2;
    79 tree[i].rmax=tree[i].max=tree[2*i+1].max;
    80 tree[i].lmax=0;
    81 }
    82 else if(tree[2*i].flag==1&&tree[2*i+1].flag==1)
    83 {
    84 tree[i].r=0;
    85 tree[i].flag=1;
    86 tree[i].lmax=tree[i].rmax=tree[i].max=0;
    87 }
    88 else
    89 {
    90 tree[i].flag=2;
    91 tree[i].max=max(tree[2*i].max,tree[2*i+1].max);
    92 tree[i].max=max(tree[i].max,tree[2*i].rmax+tree[2*i+1].lmax);
    93 if(tree[2*i].flag==0) tree[i].lmax=tree[2*i].max+tree[2*i+1].lmax;
    94 else tree[i].lmax=tree[2*i].lmax;
    95 if(tree[2*i+1].flag==0) tree[i].rmax=tree[2*i].rmax+tree[2*i+1].max;
    96 else tree[i].rmax=tree[2*i+1].rmax;
    97 if(tree[i].max==tree[2*i].max) tree[i].r=tree[2*i].r;
    98 else if(tree[i].max==tree[2*i].rmax+tree[2*i+1].lmax) tree[i].r=mid-tree[2*i].rmax+1;
    99 else if(tree[i].max==tree[2*i+1].max) tree[i].r=tree[2*i+1].r;
    100 }
    101 }
    102 int find(int i,int x)
    103 {
    104 if(tree[i].left==tree[i].right)
    105 return tree[i].r;
    106 if(tree[2*i].max>=x)
    107 return find(2*i,x);
    108 if(tree[2*i].rmax+tree[2*i+1].lmax>=x)
    109 return tree[2*i].right+1-tree[2*i].rmax;
    110 if(tree[2*i+1].max>=x)
    111 return find(2*i+1,x);
    112 return tree[i].r;
    113 }
    114 int main()
    115 {
    116 int n,m;
    117 scanf("%d%d",&n,&m);
    118 tree[1].left=1; tree[1].right=n;
    119 build(1);
    120 int i;
    121 int x,y,c,r;
    122 for(i=1;i<=m;i++)
    123 {
    124 scanf("%d",&c);
    125 if(c==1)
    126 {
    127 scanf("%d",&x);
    128 if(tree[1].max<x) printf("0\n");
    129 else
    130 {
    131 r=find(1,x);
    132 printf("%d\n",r);
    133 insert(1,r,r+x-1,1);
    134 }
    135 }
    136 if(c==2)
    137 {
    138 scanf("%d%d",&x,&y);
    139 insert(1,x,x+y-1,0);
    140 }
    141 }
    142 return 0;
    143 }



  • 相关阅读:
    android 圆角图片的实现
    navigationView 的使用和布局文件的绑定
    android listview 的监听事件
    android第三方框架 xlistview 的使用
    android Baseadapter 和 ViewHolder的使用
    android 调用电话功能
    android 颜色对照
    Android_menu_SubMenu
    Android_menu_optionMenu
    Android_Menu_contextMenu
  • 原文地址:https://www.cnblogs.com/myoi/p/2343376.html
Copyright © 2020-2023  润新知