• 月饼


    题解:线段树,区间更新,单点查询!还是不太明白延迟标记是怎么用的!!!!

     1 #pragma warning(disable:4996)
     2 #include <iostream>
     3 #include <cmath>
     4 #include <cstdio>
     5 #include <cstring>
     6 #include <algorithm>
     7 #define lson (root<<1)
     8 #define rson (root<<1|1)
     9 using namespace std;
    10 
    11 const int maxn = 1e5 + 5;
    12 
    13 int n, m, ans;
    14 
    15 struct node {
    16     int l, r, sum, add;
    17 }Tree[maxn<<2];
    18 
    19 inline void Pushup(int root) { Tree[root].sum = Tree[lson].sum + Tree[rson].sum; }
    20 
    21 inline void Pushdown(int root) {
    22     Tree[lson].add += Tree[root].add;
    23     Tree[rson].add += Tree[root].add;
    24     Tree[lson].sum += Tree[root].add*(Tree[lson].r - Tree[lson].l + 1);
    25     Tree[rson].sum += Tree[root].add*(Tree[rson].r - Tree[rson].l + 1);
    26     Tree[root].add = 0;
    27 }
    28 
    29 void Build(int l, int r, int root) {
    30     Tree[root].l = l;
    31     Tree[root].r = r;
    32     Tree[root].add = 0;
    33     if (l == r) {
    34         Tree[root].sum = 100;
    35         return;
    36     }
    37     int mid = (l + r) >> 1;
    38     Build(l, mid, lson);
    39     Build(mid + 1, r, rson);
    40     Pushup(root);
    41 }
    42 
    43 void Update(int l, int r, int root, int x) {
    44     if (r <  Tree[root].l || l >  Tree[root].r) return;
    45     if (l <= Tree[root].l && r >= Tree[root].r) {
    46         Tree[root].add += x;
    47         Tree[root].sum += x * (Tree[root].r - Tree[root].l + 1);
    48         return;
    49     }
    50     if (Tree[root].add) Pushdown(root);
    51     Update(l, r, lson, x);
    52     Update(l, r, rson, x);
    53     Pushup(root);
    54 }
    55 
    56 void Query(int root, int x) {
    57     
    58     if (Tree[root].l == Tree[root].r) {
    59         if (Tree[root].l == x) ans = Tree[root].sum;
    60         return;
    61     }
    62     if (Tree[root].add) Pushdown(root);
    63     int mid = (Tree[root].l + Tree[root].r) >> 1;
    64     if (x <= mid) Query(lson, x);
    65     else Query(rson, x);
    66 }
    67 
    68 int main()
    69 {    
    70     while (scanf("%d%d", &n, &m) != EOF) {
    71         Build(1, n, 1);
    72         for (int i = 0; i < m; i++) {
    73             char op[2];
    74             scanf("%s", op);
    75             if (op[0] == 'L') {
    76                 int a, b, c;
    77                 scanf("%d%d%d", &a, &b, &c);
    78                 Update(a, b, 1, c);
    79             }
    80             else {
    81                 int a;
    82                 scanf("%d", &a);
    83                 Query(1, a);
    84                 printf("%d
    ", ans);
    85             }
    86         }
    87     }
    88     return 0;
    89 }
  • 相关阅读:
    (一)关键字
    何时使用或何时不使用malloc函数?学数据结构有感
    typedef int a[10]; 怎么理解?
    Java的向上转型和向下转型
    com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException 异常解决方法
    java.lang.IllegalArgumentException: Property 'dataSource' is required 的解决
    哈希表
    python之创建虚拟环境
    idea2020.1.1踩坑:找不到程序包和符号
    Mysql使用过程中的问题及解决方案
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/8546897.html
Copyright © 2020-2023  润新知