zoukankan      html  css  js  c++  java
  • HDOJ 1556 线段树

    链接:

    http://acm.hdu.edu.cn/showproblem.php?pid=1556

    题解:

    之前偷懒使用前缀和写的,最近要复习一下线段树,所以又用线段树写了一下

    另外是时候修改一下自己的//head了

    代码:

     1 #include <map>
     2 #include <set>
     3 #include <cmath>
     4 #include <queue>
     5 #include <stack>
     6 #include <cstdio>
     7 #include <string>
     8 #include <vector>
     9 #include <cstdlib>
    10 #include <cstring>
    11 #include <sstream>
    12 #include <iostream>
    13 #include <algorithm>
    14 #include <functional>
    15 using namespace std;
    16 #define rep(i,a,n) for (int i=a;i<n;i++)
    17 #define per(i,a,n) for (int i=n-1;i>=a;i--)
    18 #define all(x) (x).begin(),(x).end()
    19 #define pb push_back
    20 #define mp make_pair
    21 #define lson l,m,rt<<1  
    22 #define rson m+1,r,rt<<1|1 
    23 typedef long long ll;
    24 typedef vector<int> VI;
    25 typedef pair<int, int> PII;
    26 const ll MOD = 1e9 + 7;
    27 const int INF = 0x3f3f3f3f;
    28 const int MAXN = 2e5 + 7;
    29 // head
    30 
    31 int n;
    32 int tree[MAXN << 2], lazy[MAXN << 2];
    33 
    34 void pushdown(int rt) {
    35     if (lazy[rt] != 0) {
    36         tree[rt << 1] += lazy[rt];
    37         tree[rt << 1 | 1] += lazy[rt];
    38         lazy[rt << 1] += lazy[rt];
    39         lazy[rt << 1 | 1] += lazy[rt];
    40         lazy[rt] = 0;
    41     }
    42 }
    43 
    44 void update(int L, int R, int l, int r, int rt) {
    45     if (L <= l && r <= R) {
    46         tree[rt]++;
    47         lazy[rt]++;
    48         return;
    49     }
    50     pushdown(rt);
    51     int m = (l + r) >> 1;
    52     if (L <= m) update(L, R, lson);
    53     if (R > m) update(L, R, rson);
    54 }
    55 
    56 int query(int k, int l, int r, int rt) {
    57     if (l == r) return tree[rt];
    58     pushdown(rt);
    59     int m = (l + r) >> 1;
    60     if (k <= m) query(k, lson);
    61     else query(k, rson);
    62 }
    63 
    64 int main() {
    65     while (cin >> n, n) {
    66         memset(tree, 0, sizeof(tree));
    67         memset(lazy, 0, sizeof(lazy));
    68         rep(i, 0, n) {
    69             int a, b;
    70             scanf("%d%d", &a, &b);
    71             update(a, b, 1, n, 1);
    72         }
    73         cout << query(1, 1, n, 1);
    74         rep(i, 2, n + 1) printf(" %d", query(i, 1, n, 1));
    75         cout << endl;
    76     }
    77     return 0;
    78 }
  • 相关阅读:
    ColorPix——到目前为止最好用的屏幕取色器
    ES+VBA 实现批量添加网络图片
    SQL语句-delete语句
    Visual C++ 2013 and Visual C++ Redistributable Package 更新版官网下载地址
    centos长ping输出日志的脚本
    Centos 常用命令
    c#连接数据库
    C#窗体间的跳转传值
    c#邮件发送
    C#WIFI搜索与连接
  • 原文地址:https://www.cnblogs.com/baocong/p/6698872.html
Copyright © 2011-2022 走看看