zoukankan      html  css  js  c++  java
  • 3211: 花神游历各国

    3211: 花神游历各国

    链接

    分析:

      1e9的,开5次方,就变成了1,所以暴力开方即可,记录一个tag,当前节点是否全为1或者0。

      记一下各种zz的bug:1、开方后可能为0,不只是1,所以判断<=1,不是==1;2、开longlong,每个节点1e9,加起来就比1e9大了。3、读入的时候也可以判一下是不是1或者0,开始时忘了。4、开空间,这。。。

      后来发现自己的代码写的真。。。于是看着网上的改了一份。

    代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4  
     5 inline int read() {
     6     int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
     7     for (;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f;
     8 }
     9  
    10 const int N = 100100;
    11  
    12 LL sum[N<<2];
    13 int tag[N<<2];
    14  
    15 #define lson l,mid,rt<<1
    16 #define rson mid+1,r,rt<<1|1
    17 void pushup(int rt) {
    18     sum[rt] = sum[rt<<1] + sum[rt<<1|1]; // -- 写成了+= 
    19     tag[rt] = tag[rt<<1] & tag[rt<<1|1];
    20 }
    21 void build(int l,int r,int rt) {
    22     if (l == r) {
    23         scanf("%lld",&sum[rt]);
    24         if (sum[rt] <= 1) tag[rt] = 1;
    25         return; 
    26     }
    27     int mid = (l + r) >> 1;
    28     build(lson);build(rson);
    29     pushup(rt);
    30 }
    31 void change(int l,int r,int rt) {
    32     if (tag[rt]) return ;
    33     if (l == r) {
    34         sum[rt] = sqrt(sum[rt]);
    35         if (sum[rt] <= 1) tag[rt] = 1;
    36         return ;
    37     }
    38     int mid = (l + r) >> 1;
    39     change(lson);change(rson);
    40     pushup(rt);
    41 }
    42 void update(int l,int r,int rt,int L,int R) {
    43     if (L <= l && r <= R) {
    44         if (tag[rt]) return ;
    45         change(l,r,rt);
    46         return ;
    47     }
    48     int mid = (l + r) >> 1;
    49     if (L <= mid) update(lson,L,R);
    50     if (R > mid)  update(rson,L,R);
    51     pushup(rt);
    52 }
    53 LL query(int l,int r,int rt,int L,int R) {
    54     if (L <= l && r <= R) {
    55         return sum[rt];
    56     }
    57     int mid = (l + r) >> 1;
    58     LL res = 0;
    59     if (L <= mid) res += query(lson,L,R);
    60     if (R > mid)  res += query(rson,L,R);
    61     return res;
    62 }
    63 int main() {
    64     int n = read();
    65     build(1,n,1);
    66     int m = read(); 
    67     while (m--) {
    68         int opt = read(),l = read(),r = read();
    69         if (opt==1) {
    70             printf("%lld
    ",query(1,n,1,l,r));
    71         }
    72         else {
    73             update(1,n,1,l,r);
    74         }
    75     }
    76     return 0;
    77 }
    View Code
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4  
     5 inline int read() {
     6     int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
     7     for (;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f;
     8 }
     9 
    10 const int N = 100100;
    11 
    12 LL sum[N<<2];
    13 int tag[N<<2];
    14  
    15 #define lson l,mid,rt<<1
    16 #define rson mid+1,r,rt<<1|1
    17 void pushup(int rt) {
    18     sum[rt] = sum[rt<<1] + sum[rt<<1|1]; // -- 写成了+= 
    19     tag[rt] = tag[rt<<1] & tag[rt<<1|1]; 
    20 }
    21 void build(int l,int r,int rt) {
    22     if (l == r) {
    23         scanf("%lld",&sum[rt]);
    24         if (sum[rt] <= 1) tag[rt] = 1;
    25         return; 
    26     }
    27     int mid = (l + r) >> 1;
    28     build(lson);build(rson);
    29     pushup(rt);
    30 }
    31 void update(int l,int r,int rt,int L,int R) {
    32     if (tag[rt]) return ;
    33     if (l == r) {
    34         sum[rt] = sqrt(sum[rt]);
    35         if (sum[rt] <= 1) tag[rt] = 1;
    36         return ;
    37     }
    38     int mid = (l + r) >> 1;
    39     if (L <= mid) update(lson,L,R);
    40     if (R > mid)  update(rson,L,R);
    41     pushup(rt);
    42 }
    43 LL query(int l,int r,int rt,int L,int R) {
    44     if (L <= l && r <= R) {
    45         return sum[rt];
    46     }
    47     int mid = (l + r) >> 1;
    48     LL res = 0;
    49     if (L <= mid) res += query(lson,L,R);
    50     if (R > mid)  res += query(rson,L,R);
    51     return res;
    52 }
    53 int main() {
    54     int n = read();
    55     build(1,n,1);
    56     int m = read(); 
    57     while (m--) {
    58         int opt = read(),l = read(),r = read();
    59         if (opt==1) printf("%lld
    ",query(1,n,1,l,r));
    60         else update(1,n,1,l,r);
    61     }
    62     return 0;
    63 }
    View Code
  • 相关阅读:
    Hello,world的几种写法!
    浮动与清除浮动
    css中表格的table-layout属性特殊用法
    CSS之照片集效果
    CSS之transition过渡练习
    CSS之过渡简单应用—日落西山
    CSS之立方体绘画步骤
    CSS之立体球体
    transform
    Vue.sync修饰符与this.$emit('update:xxx', newXXX)
  • 原文地址:https://www.cnblogs.com/mjtcn/p/9245526.html
Copyright © 2011-2022 走看看