zoukankan      html  css  js  c++  java
  • hdu 3577 线段树

    题意:一列经过1000000个站点的火车上最多同时乘坐 K 个人,有 Q 个乘客按照题目给出的顺序去买票,输出所有购票成功的乘客

    做法:RMQ线段树 + 区间更新

      1 #include "bits/stdc++.h"
      2 using namespace std;
      3 #define lson l, m, rt<<1
      4 #define rson m + 1, r, rt<<1|1
      5 const int MAXN = 1000010;
      6 const int INF = 0x3f3f3f3f;
      7 
      8 struct Node
      9 {
     10     int Max;
     11 
     12     int add;
     13     bool same;
     14     int sameVal;
     15 }node[MAXN<<2];
     16 
     17 inline void PushDown(int rt, int segLen)
     18 {
     19     if (node[rt].same) {
     20         node[rt<<1].Max = node[rt].sameVal;
     21         node[rt<<1|1].Max = node[rt].sameVal;
     22 
     23         node[rt<<1].same = node[rt<<1|1].same = true;
     24         node[rt<<1].sameVal = node[rt<<1|1].sameVal = node[rt].sameVal;
     25         node[rt].same = false;
     26     }
     27     if(node[rt].add) {
     28         node[rt<<1].Max += node[rt].add;
     29         node[rt<<1|1].Max += node[rt].add;
     30 
     31         node[rt<<1].add += node[rt].add;
     32         node[rt<<1|1].add += node[rt].add;
     33         node[rt].add = 0;
     34     }
     35 }
     36 
     37 inline void PushUp(int rt)
     38 {
     39     node[rt].Max = max(node[rt<<1].Max, node[rt<<1|1].Max);
     40 }
     41 
     42 void Build(int l, int r, int rt)
     43 {
     44     node[rt].same = false;
     45     node[rt].add = 0;
     46 
     47     if (l == r) {
     48 //        scanf("%d", &node[rt].Max);
     49         node[rt].Max = 0;
     50         return ;
     51     }
     52     int m = (l + r)>>1;
     53     Build(lson);
     54     Build(rson);
     55 
     56     PushUp(rt);
     57 }
     58 
     59 void UpdateAdd(int p, int add, int l, int r, int rt)
     60 {
     61     if (l == r) {
     62         node[rt].Max += add;
     63         return ;
     64     }
     65     PushDown(rt, r - l + 1);
     66     int m = (l + r) >> 1;
     67     if (p <= m) {
     68         UpdateAdd(p, add, lson);
     69     }
     70     else {
     71         UpdateAdd(p, add, rson);
     72     }
     73 
     74     PushUp(rt);
     75 }
     76 
     77 void UpdateModify(int p, int newVal, int l, int r, int rt)
     78 {
     79     if (l == r) {
     80         node[rt].Max = newVal;
     81         return ;
     82     }
     83     PushDown(rt, r - l + 1);
     84     int m = (l + r) >> 1;
     85     if (p <= m) {
     86         UpdateModify(p, newVal, lson);
     87     }
     88     else {
     89         UpdateModify(p, newVal, rson);
     90     }
     91 
     92     PushUp(rt);
     93 }
     94 
     95 void UpdateAdd_Seg(int L, int R, int add, int l, int r, int rt)
     96 {
     97     if (L <= l && r <= R) {
     98         node[rt].Max += add;
     99 
    100         node[rt].add += add;
    101         return ;
    102     }
    103 
    104     PushDown(rt, r - l + 1);
    105     int m = (l + r) >> 1;
    106     if (L <= m) {
    107         UpdateAdd_Seg(L, R, add, lson);
    108     }
    109     if (R > m) {
    110         UpdateAdd_Seg(L, R, add, rson);
    111     }
    112 
    113     PushUp(rt);
    114 }
    115 
    116 void UpdateModify_Seg(int L, int R, int newVal, int l, int r, int rt)
    117 {
    118     if (L <= l && r <= R) {
    119         node[rt].Max = newVal;
    120 
    121         node[rt].same = true;
    122         node[rt].sameVal = newVal;
    123         return ;
    124     }
    125 
    126     PushDown(rt, r - l + 1);
    127     int m = (l + r) >> 1;
    128     if (L <= m) {
    129         UpdateModify_Seg(L, R, newVal, lson);
    130     }
    131     if (R > m) {
    132         UpdateModify_Seg(L, R, newVal, rson);
    133     }
    134 
    135     PushUp(rt);
    136 }
    137 
    138 
    139 int Query(int L, int R, int l, int r, int rt)
    140 {
    141     if (L <= l && r <= R) {
    142         return node[rt].Max;
    143     }
    144     PushDown(rt, r - l + 1);
    145     int m = (l + r) >> 1;
    146     int ans = -INF;
    147     if (L <= m) {
    148         ans = max(ans, Query(L, R, lson));
    149     }
    150     if (R > m) {
    151         ans = max(ans, Query(L, R, rson));
    152     }
    153     return ans;
    154 }
    155 
    156 int n, m;
    157 int POS, ADD, NewVal, L, R;
    158 
    159 int main()
    160 {
    161     int t, T, K, Q;
    162     scanf("%d", &T);
    163     for (t = 1; t <= T; ++t) {
    164         Build(1, 1000000, 1);
    165         scanf("%d%d", &K, &Q);
    166         int index = 0;
    167         printf("Case %d:
    ", t);
    168         while (Q--) {
    169             ++index;
    170             scanf("%d%d", &L, &R);
    171             if(Query(L, R - 1, 1, 1000000, 1) < K) {
    172                 printf("%d ", index);
    173                 UpdateAdd_Seg(L, R - 1, 1, 1, 1000000, 1);
    174             }
    175         }
    176         printf("
    
    ");
    177     }
    178 }
  • 相关阅读:
    ubuntu20.04.2设置配置静态ip方法
    如何在windows10上面运行HyperLedger Fabric1.4
    powershell中临时修改环境变量
    openssl查看pem格式证书细节
    git clone的时候遭遇fatal: early EOF fatal: index-pack failed解决办法
    修改sourcetree的推送账户
    ubuntu上kafka的配置与使用(二)--kafka和zookeeper集群的配置(kafka自带的zookeeper)
    ubuntu上kafka的配置与使用(一)--单机kafka的配置
    李航老师的《统计学习方法》第二章算法的matlab程序
    第8题——计算糖果
  • 原文地址:https://www.cnblogs.com/AC-Phoenix/p/4686449.html
Copyright © 2011-2022 走看看