• A Simple Problem with Integers POJ


    思路:线段树,区间更新,区间查找

      1 #include<iostream>
      2 #include<vector>
      3 #include<string>
      4 #include<cmath>
      5 #include<set>
      6 #include<algorithm>
      7 #include<cstdio>
      8 #include<map>
      9 #include<cstring>
     10 #include<list>
     11 
     12 #define MAXSIZE 100010
     13 
     14 using namespace std;
     15 
     16 long long tree[4*MAXSIZE];
     17 long long lz[4*MAXSIZE];
     18 int N, Q;
     19 
     20 void init()
     21 {
     22     memset(tree, 0, sizeof(tree));
     23     memset(lz, 0, sizeof(lz));
     24 }
     25 
     26 void build(int node, int l, int r)
     27 {
     28     if(l == r)
     29     {
     30         scanf("%lld", &tree[node]);
     31         return;
     32     }
     33     
     34     int mid = (l+r)/2;
     35     build(node*2, l, mid);
     36     build(node*2+1, mid+1, r);
     37     
     38     tree[node] = tree[node*2] + tree[node*2+1];
     39 }
     40 
     41 void push_down(int node, int l, int r)
     42 {
     43     if(lz[node])
     44     {
     45         int mid = (l+r)/2;
     46         lz[node*2] += lz[node];
     47         lz[node*2+1] += lz[node];
     48         tree[node*2] += (mid-l+1)*lz[node];
     49         tree[node*2+1] += (r-mid)*lz[node];
     50         lz[node] = 0;
     51     }
     52 }
     53 
     54 void update_range(int node, int l, int r, int L, int R, int add)
     55 {
     56     if(l <= L && r >= R)
     57     {
     58         lz[node] += add;
     59         tree[node] += (R-L+1)*add;
     60         return;
     61     }
     62     push_down(node, L, R);
     63     int mid = (L+R)/2;
     64     if(mid >= l)
     65         update_range(node*2, l, r, L, mid, add);
     66     if(mid < r)
     67         update_range(node*2+1, l, r, mid+1, R, add);
     68     tree[node] = tree[node*2] + tree[node*2+1];
     69 }
     70 
     71 long long query_range(int node, int l, int r, int L, int R)
     72 {
     73     if(l <= L && r >= R)
     74     {
     75         return tree[node];
     76     }
     77     push_down(node, L, R);
     78     int mid = (L+R)/2;
     79     long long sum = 0;
     80     if(mid >= l)
     81         sum += query_range(node*2, l, r, L, mid);
     82     if(mid < r)
     83         sum += query_range(node*2+1, l, r, mid+1, R);
     84     
     85     return sum;
     86 }
     87 
     88 
     89 
     90 int main()
     91 {    
     92     scanf("%d%d", &N, &Q);
     93     init();
     94     build(1, 1, N);
     95     while(Q--)
     96     {
     97         char ch;
     98         scanf(" %c", &ch);
     99         if(ch == 'Q')
    100         {
    101             int a, b;
    102             scanf("%d%d", &a, &b);
    103             printf("%lld
    ", query_range(1, a, b, 1, N));
    104         }
    105         else if(ch == 'C')
    106         {
    107             int a, b, c;
    108             scanf("%d%d%d", &a, &b, &c);
    109             update_range(1, a, b, 1, N, c);
    110         }
    111     }
    112     
    113 
    114     return 0;
    115 }
  • 相关阅读:
    在ServiceImpl层加载Spring配置文件进行测试
    MyBatis:逆向工程,实现实体类中文注释(Eclipse + MySQL)
    Linux(CentOS):开机自动启动Tomcat脚本(判断MySQL是否启动后再启)
    Linux(CentOS):设置FTP开机自动启动
    转载 PowerDesigner导出mysql数据结构
    SVN分支/主干Merge操作小记
    Quartz.NET+TopSelf 实现定时服务
    关于redis,学会这8点就够了(转)
    kafka 基础知识梳理(转载)
    Centos7 忘记密码的情况下,修改root或其他用户密码
  • 原文地址:https://www.cnblogs.com/FengZeng666/p/11458279.html
Copyright © 2020-2023  润新知