zoukankan      html  css  js  c++  java
  • poj 1195 Mobile phones

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

    简单二维线段树题。单点更新区间求和。

    My Code
     1 #include <cstdio>
     2 #include <algorithm>
     3 using namespace std;
     4 #define lson l,m,rt<<1
     5 #define rson m+1,r,rt<<1|1
     6 #define maxn 1024
     7 int n;
     8 struct node
     9 {
    10     int setree1[maxn<<1];
    11 }setree[maxn<<1];
    12 void build1(int num,int l,int r,int rt)
    13 {
    14     setree[num].setree1[rt]=0;
    15     if(l==r)
    16     return;
    17     int m=(l+r)>>1;
    18     build1(num,lson);
    19     build1(num,rson);
    20 }
    21 void build(int l,int r,int rt)
    22 {
    23     build1(rt,0,n-1,1);
    24     if(l==r)
    25     return;
    26     int m=(l+r)>>1;
    27     build(lson);
    28     build(rson);
    29 }
    30 void update1(int num,int l,int r,int rt,int x,int c)
    31 {
    32     setree[num].setree1[rt]+=c;
    33     if(l==r)
    34     return;
    35     int m=(l+r)>>1;
    36     if(x<=m)
    37     update1(num,lson,x,c);
    38     else
    39     update1(num,rson,x,c);
    40 }
    41 void update(int l,int r,int rt,int x,int y,int c)
    42 {
    43     update1(rt,0,n-1,1,y,c);
    44     if(l==r)
    45         return;
    46     int m=(l+r)>>1;
    47     if(x<=m)
    48     update(lson,x,y,c);
    49     else
    50     update(rson,x,y,c);
    51 }
    52 int query1(int num,int l,int r,int rt,int L,int R)
    53 {
    54     if(L<=l&&r<=R)
    55     return setree[num].setree1[rt];
    56     int m=(l+r)>>1;
    57     int ans=0;
    58     if(L<=m)
    59     ans+=query1(num,lson,L,R);
    60     if(R>m)
    61     ans+=query1(num,rson,L,R);
    62     return ans;
    63 }
    64 int query(int l,int r,int rt,int L,int R,int LL,int RR)
    65 {
    66     if(L<=l&&r<=R)
    67     return query1(rt,0,n-1,1,LL,RR);
    68     int m=(l+r)>>1;
    69     int ans=0;
    70     if(L<=m)
    71     ans+=query(lson,L,R,LL,RR);
    72     if(R>m)
    73     ans+=query(rson,L,R,LL,RR);
    74     return ans;
    75 }
    76 int main()
    77 {
    78     int op;
    79     while(scanf("%d",&op)&&op!=3){
    80         if(op==0){
    81             scanf("%d",&n);
    82             build(0,n-1,1);
    83         }
    84         else if(op==1){
    85             int x,y,c;
    86             scanf("%d%d%d",&x,&y,&c);
    87             update(0,n-1,1,x,y,c);
    88         }
    89         else{
    90             int l,r,b,t;
    91             scanf("%d%d%d%d",&l,&b,&r,&t);
    92             printf("%d\n",query(0,n-1,1,l,r,b,t));
    93         }
    94     }
    95     return 0;
    96 }
  • 相关阅读:
    5.6 Go 常用函数
    5.5 Go defer
    5.4 Go 闭包
    5.3 Go 匿名函数
    5.2 Go 包与函数
    python 通过pytz模块进行时区的转换,获取指定时区的时间
    前端在js中获取用户所在地区的时间与时区
    Python2 指定文件编码格式需要注意的地方
    linux 使用进程管理工具 supervisor
    Python 私有变量中两个下划线 _ _item 与 一个下划线的区别 _item
  • 原文地址:https://www.cnblogs.com/kim888168/p/2912923.html
Copyright © 2011-2022 走看看