zoukankan      html  css  js  c++  java
  • HDU 1556 Color the ball 线段树

    HDU 1556 Color the ball

    线段树模版题,存个模板

     1 #include <iostream>
     2 #include <cstdio>
     3 #define LL long long
     4 #define eps 1e-8
     5 #define INF 0x3f3f3f3f
     6 #define MAXN 100005
     7 using namespace std;
     8 int sum[MAXN * 3], add[MAXN * 3];
     9 
    10 void pushup(int t){
    11     sum[t] = sum[t << 1] + sum[t << 1 | 1];
    12 }
    13 void pushdown(int t, int x){
    14     if (add[t]){
    15         add[t << 1] += add[t];
    16         add[t << 1 | 1] += add[t];
    17         sum[t << 1] += ((x + 1) >> 1)* add[t];
    18         sum[t << 1 | 1] += (x >> 1) * add[t];
    19         add[t] = 0;
    20     }
    21 }
    22 void update(int L, int R, int t, int p, int q, int x){
    23     if (p <= L && q >= R){
    24         sum[t] += (R - L + 1) * x;
    25         add[t] += x;
    26         return;
    27     }
    28 
    29     pushdown(t, R - L + 1);
    30     int mid = (L + R) >> 1;
    31     if (p <= mid){
    32         update(L, mid, t << 1, p, q, x);
    33     }
    34     if (q > mid){
    35         update(mid + 1, R, t << 1 | 1, p, q, x);
    36     }
    37     pushup(t);
    38 }
    39 int query(int L, int R, int t, int p, int q){
    40     if (p <= L && q >= R){
    41         return sum[t];
    42     }
    43     pushdown(t, R - L + 1);
    44     int mid = (L + R) >> 1;
    45     int res = 0;
    46     if (p <= mid){
    47         res += query(L, mid, t << 1, p, q);
    48     }
    49     if (q > mid){
    50         res += query(mid + 1, R, t << 1 | 1, p, q);
    51     }
    52     return  res;
    53 }
    54 int main()
    55 {
    56     int n;
    57     while (~scanf("%d", &n) && n){
    58         memset(sum, 0, sizeof(sum));
    59         memset(add, 0, sizeof(add));
    60         int x, y;
    61         for (int i = 1; i <= n; i++){
    62             scanf("%d%d", &x, &y);
    63             update(1, n, 1, x, y, 1);
    64         }
    65         for (int i = 1; i < n; i++){
    66             printf("%d ", query(1, n, 1, i, i));
    67         }
    68         printf("%d
    ", query(1, n, 1, n, n));
    69     }
    70 }
  • 相关阅读:
    linux命令(3)top
    linux命令(2)vmstat
    学习okhttp wiki--Connections.
    你可以更幸福(转载)
    Android中多表的SQLite数据库(译)
    怎样写有效的设计文档(译)
    Material Design说明
    Android原生Calendar代码阅读(一)
    Android Studio tips and tricks 翻译学习
    Material Calendar View 学习记录(二)
  • 原文地址:https://www.cnblogs.com/macinchang/p/4731616.html
Copyright © 2011-2022 走看看