zoukankan      html  css  js  c++  java
  • POJ 3468 线段树

    链接:

    http://poj.org/problem?id=3468

    题解:

    就是普通的线段树区间更新,区间求和

    代码:

     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 = 1e5 + 7;
    29 // head
    30 
    31 ll n, q;
    32 ll tree[MAXN << 2], lazy[MAXN << 2];
    33 
    34 void pushup(ll rt) {
    35     tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
    36 }
    37 
    38 void pushdown(ll rt, ll m) {
    39     if (lazy[rt] != 0) {
    40         tree[rt << 1] += lazy[rt] * (m - (m >> 1));
    41         tree[rt << 1 | 1] += lazy[rt] * (m >> 1);
    42         lazy[rt << 1] += lazy[rt];
    43         lazy[rt << 1 | 1] += lazy[rt];
    44         lazy[rt] = 0;
    45     }
    46 }
    47 
    48 void build (ll l, ll r, ll rt) {
    49     if (l == r) {
    50         scanf("%lld", &tree[rt]);
    51         return;
    52     }
    53     ll m = (l + r) >> 1;
    54     build(lson);
    55     build(rson);
    56     pushup(rt);
    57 }
    58 
    59 void update(ll L, ll R, ll x, ll l, ll r, ll rt) {
    60     if (L <= l && r <= R) {
    61         tree[rt] += (r - l + 1)*x;
    62         lazy[rt] += x;
    63         return;
    64     }
    65     pushdown(rt, r - l + 1);
    66     ll m = (l + r) >> 1;
    67     if (L <= m) update(L, R, x, lson);
    68     if (R > m) update(L, R, x, rson);
    69     pushup(rt);
    70 }
    71 
    72 ll query(ll L, ll R, ll l, ll r, ll rt) {
    73     if (L <= l && r <= R) return tree[rt];
    74     pushdown(rt, r - l + 1);
    75     ll m = (l + r) >> 1;
    76     ll ret = 0;
    77     if (L <= m) ret += query(L, R, lson);
    78     if (R > m) ret += query(L, R, rson);
    79     return ret;
    80 }
    81 
    82 int main() {
    83     cin >> n >> q;
    84     build(1, n, 1);
    85     while (q--) {
    86         char ch;
    87         ll a, b, c;
    88         scanf("
    %c", &ch);
    89         if (ch == 'C') {
    90             scanf("%lld%lld%lld", &a, &b, &c);
    91             update(a, b, c, 1, n, 1);
    92         }
    93         else {
    94             scanf("%lld%lld", &a, &b);
    95             printf("%lld
    ", query(a, b, 1, n, 1));
    96         }
    97     }
    98     return 0;
    99 }
  • 相关阅读:
    流量数据iftop命令
    DNS A记录和CNAME记录
    centos6.5安装mysql
    Python列表插入字典(转)
    列表转字典
    python 二分法O(logn)
    centos 6.5搭建Samba
    反爬虫-----看这一篇就够了
    windows常用命令
    requests中文页面乱码解决方案【转】
  • 原文地址:https://www.cnblogs.com/baocong/p/6699204.html
Copyright © 2011-2022 走看看