hdu-4027 Can you answer these queries?

↘锁芯ラ 提交于 2019-11-29 05:09:55

题目链接: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;
}




标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!