标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 9216 Accepted Submission(s): 2106
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <vector> #include <queue> #include <map> #include <set> #include <stack> #include <algorithm> using namespace std; #define root 1,n,1 #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 #define lr rt<<1 #define rr rt<<1|1 typedef long long LL; const int oo = 1e9+7; const double PI = acos(-1.0); const double eps = 1e-6 ; const int N = 100010; LL e[N],date[N<<2]; int n , m ; void Up( int rt ) { date[rt] = date[lr] + date[rr]; } void build( int l , int r , int rt ) { if( l == r ) { date[rt] = e[l] ; return ; } int mid = (l+r)>>1; build(lson) , build(rson); Up(rt); } void update( int l , int r , int rt , int L , int R ) { if( r - l + 1 == date[rt] ) return ; if( l == r ){ date[rt] = (LL)sqrt((double)date[rt] ); return ; } int mid = (l+r)>>1; if( R <= mid ) update(lson,L,R); else if( L > mid ) update(rson,L,R); else update(lson,L,mid),update(rson,mid+1,R); Up(rt); } LL query( int l , int r , int rt , int L , int R ) { if( L == l && r == R )return date[rt]; int mid = (l+r)>>1; if( R <= mid ) return query(lson,L,R); else if( L > mid ) return query(rson,L,R); else return query(lson,L,mid)+query(rson,mid+1,R); } int main() { #ifdef LOCAL freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); #endif // LOCAL int _ , x , y , c , cas = 1 ; while( ~scanf("%d",&n) ) { printf("Case #%d:\n",cas++); for( int i = 1 ; i <= n ; ++i ){ scanf("%I64d",&e[i]); } build(root); scanf("%d",&m); while( m-- ) { scanf("%d%d%d",&c,&x,&y); if( x > y ) swap( x , y ); if( !c ) { update(root,x,y); } else { printf("%I64d\n",query(root,x,y)); } } puts(""); } }
HDU 4027 Can you answer these queries?(线段树)
标签:
原文地址:http://www.cnblogs.com/hlmark/p/4249222.html