题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027
修改操作是把区间内的所有数开根号
另一个操作是区间求和操作
2 的 63 次开方6,7 根号也就变为了 1 。
#include <iostream>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <stack>
#include <set>
#include <queue>
#include <map>
using namespace std;
#define ll(ind) (ind<<1)
#define rr(ind) (ind<<1|1)
#define Mid(a,b) (a+((b-a)>>1))
typedef __int64 LL;
const int N = 5020000;
LL a[N];
int n, m, b, c, d;
struct node
{
int left, right;
int mid() { return Mid(left, right); }
LL num;
};
struct segtree
{
node tree[N * 4];
void build(int left, int right, int ind)
{
tree[ind].left = left;
tree[ind].right = right;
tree[ind].num = 0;
if (left == right)
{
tree[ind].num = a[left];
}
else
{
int mid = tree[ind].mid();
build(left, mid, ll(ind));
build(mid + 1, right, rr(ind));
tree[ind].num = tree[ll(ind)].num + tree[rr(ind)].num;
}
}
void update(int st, int ed, int ind)
{
int left = tree[ind].left;
int right = tree[ind].right;
if (left == st && right == ed && tree[ind].num == (right - left + 1))
return;
if (left == right)
{
tree[ind].num = sqrt(tree[ind].num * 1.0);
return;
}
int mid = tree[ind].mid();
if (ed <= mid)
update(st, ed, ll(ind));
else if (st > mid)
update(st, ed, rr(ind));
else
{
update(st, mid, ll(ind));
update(mid + 1, ed, rr(ind));
}
tree[ind].num = tree[ll(ind)].num + tree[rr(ind)].num;
}
LL query(int st, int ed, int ind)
{
int left = tree[ind].left;
int right = tree[ind].right;
if (left == st && right == ed)
{
return tree[ind].num;
}
else
{
LL ans = 0;
int mid = tree[ind].mid();
if (ed <= mid)
ans = query(st, ed, ll(ind));
else if (st > mid)
ans = query(st, ed, rr(ind));
else
{
ans += query(st, mid, ll(ind));
ans += query(mid + 1, ed, rr(ind));
}
return ans;
}
}
}seg;
int main()
{
int cases = 1;
while (scanf("%d", &n) != EOF)
{
for (int i = 1; i <= n; i++)
scanf("%I64d", &a[i]);
seg.build(1, n, 1);
scanf("%d", &m);
printf("Case #%d:\n", cases++);
while (m--)
{
scanf("%d %d %d", &b, &c, &d);
if (c > d)//坑
swap(c, d);
if (b == 0)
{
seg.update(c, d, 1);
}
else
{
seg.query(c, d, 1);
printf("%I64d\n", seg.query(c, d, 1));
}
}
printf("\n");
}
return 0;
}
来源:CSDN
作者:mfcheer
链接:https://blog.csdn.net/u014427196/article/details/43063261