标签:
Input
Output
Sample Input
Sample Output
/* 线段树成段更新开根号 记得如果ans==r-l+1,就不用更新了,否则TLE */ #include <bits/stdc++.h> using namespace std; const int MAX = 1e6 + 10; long long sum[MAX << 2]; long long a[MAX]; int n; void build(long long rt, long long l, long long r) { sum[rt] = 0; if(l == r){ sum[rt] = a[l]; return; } long long mid = l + r >> 1; build(rt*2, l , mid); build(rt*2+1, mid + 1, r); sum[rt] = sum[rt*2] + sum[rt*2+1]; } void update(long long rt, long long l, long long r, long long L, long long R) { if(l == r){ sum[rt] = sqrt(sum[rt]); return; } long long mid = l + r >> 1; if(L <= mid) update(rt*2, l, mid, L, R); if(R > mid) update(rt*2+1, mid + 1, r, L, R); sum[rt] = sum[rt*2] + sum[rt*2+1]; } long long query(long long rt, long long l, long long r, long long L, long long R) { if(L <= l && R >= r) return sum[rt]; long long mid = l + r >> 1; long long ret = 0; if(L <= mid) ret += query(rt*2, l, mid, L, R); if(R > mid) ret += query(rt*2+1, mid+1, r, L, R); return ret; } int main() { int cas = 1; while(~scanf("%d", &n)){ printf("Case #%d:\n", cas++); for(int i = 1; i <= n; i++) scanf("%I64d", &a[i]); build(1, 1, n); int m; scanf("%d", &m); int flag; long long l, r; while(m--){ scanf("%d%I64d%I64d", &flag, &l, &r); if(l >= r) swap(l, r); if(flag){ long long ans = query(1, 1, n, l, r); printf("%I64d\n", ans); } else { long long ans = query(1, 1, n, l, r); if(ans != r - l + 1) update(1, 1, n, l, r); } } puts(""); } return 0; }
HDU4027——线段树成段更新——Can you answer these queries?
标签:
原文地址:http://www.cnblogs.com/zero-begin/p/4696734.html