day1 night P4145 God made the title of seven minutes 2 / Flora countries traveled segment tree

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cmath>
  4 
  5 using namespace std;
  6 
  7 const int maxn=1e6+10;
  8 struct node
  9 {
 10     int l,r;
 11     long long sum,maxx;
 12 }tree[maxn];
 13 long long n;
 14 long long num[maxn];
 15 long long pushup(long long x)
 16 {
 17     tree[x].sum=tree[x<<1].sum+tree[x<<1|1].sum;
 18     if(tree[x<<1].maxx>tree[x<<1|1].maxx)
 19     {
 20         tree[x].maxx=tree[x<<1].maxx;
 21     }
 22     else
 23     {
 24         tree[x].maxx=tree[x<<1|1].maxx;
 25     }
 26 }
 27 void build(long long x,long long l,long long r)
 28 {
 29     tree[x].l=l;
 30     tree[x].r=r;
 31     if(l==r)
 32     {
 33         tree[x].sum=tree[x].maxx=num[l];
 34         return ;
 35     }
 36     int mid=(l+r)>>1;
 37     build(x<<1,l,mid);
 38     build(x<<1|1,mid+1,r);
 39     pushup(x);
 40 }
 41 void modify(long long x,long long l,long long r)
 42 {
 43     if(tree[x].l==tree[x].r)
 44     {
 45         tree[x].sum=sqrt(tree[x].sum);
 46         tree[x].maxx=sqrt(tree[x].maxx);
 47         return ;
 48     }
 49     long long mid=(tree[x].l+tree[x].r)>>1;
 50     if(l<=mid&&tree[x<<1].maxx>1)
 51     {
 52         modify(x<<1,l,r);
 53     }
 54     if(r>mid&&tree[x<<1|1].maxx>1)
 55     {
 56         modify(x<<1|1,l,r);
 57     }
 58     pushup(x);
 59 }
 60 long long query(long x,long long l,long long r)
 61 {
 62     if(l<=tree[x].l&&r>=tree[x].r)
 63     {
 64         return tree[x].sum;
 65     }
 66     long long  mid=(tree[x].l+tree[x].r)>>1;
 67     long long  ans=0;
 68     if(l<=mid)
 69     {
 70         ans+=query(x<<1,l,r);
 71     }
 72     if(r>mid)
 73     {
 74         ans+=query(x<<1|1,l,r);
 75     }
 76     return ans;
 77 }
 78 int main()
 79 {
 80     cin>>n;
 81     for(long long i=1;i<=n;i++)
 82     {
 83         cin>>num[i];
 84     }
 85     build(1,1,n);
 86     long long m,opt,l,r;
 87     cin>>m;
 88     while(m--)
 89     {
 90         cin>>opt>>l>>r;
 91         if(l>r)
 92         {
 93             swap(l,r);
 94         }
 95         if(opt==0)
 96         {
 97             modify(1,l,r);
 98         }
 99         else
100         {
101             cout<<query(1,l,r)<<endl;
102         }
103     }
104     return 0;
105 }

 

Guess you like

Origin www.cnblogs.com/2529102757ab/p/11224802.html