求助
查看原帖
求助
817297
lwyyds楼主2023/1/26 15:20
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<random>
#include<cstring>
#include<vector>
#include<cmath>
#include<map>
#include<set>
#define lson h*2
#define rson h*2+1
using namespace std;
typedef long long ll;
const int max1 = 230025;
const int inf = 2147483647;
ll n, m, k, ans, su1, su2, su3, su4, su5, base, wer,arr[max1], vis[max1],brr[max1];
string str[max1], s, s1, s2;
char ss[53][53];
char ch;
map<int, int>p;
struct node {
	ll max, ans, l, r;
}tree[4*max1];
ll read() {
	ll f = 1, x = 0; char ch = getchar();
	while (ch < '0' || ch>'9') {
		if(ch=='-')f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = x * 10 + ch - 48;
		ch = getchar();
	}
	return x * f;
}
void build(ll h, ll l, ll r) {
	tree[h].l = l;
	tree[h].r = r;
	if (l == r) {
		tree[h].max=tree[h].ans = arr[l];
		vis[l] = h;
	}
	else {
		ll mid = (l + r) / 2;
		build(lson, l, mid);
		build(rson, mid + 1, r);
		tree[h].max = max(tree[lson].max, tree[rson].max);
		tree[h].ans = tree[lson].ans + tree[rson].ans;
	}
}
void modify(ll h,ll l,ll r) {
	if (tree[h].l == tree[h].r) {
		tree[h].max=tree[h].ans = sqrt(tree[h].ans);
		return;
	}
	if (tree[lson].r >= l && tree[lson].max > 1) {
		modify(lson, l, r);
	}
	if (tree[rson].l <= r && tree[rson].max > 1) {
		modify(rson, l, r);
	}
	tree[h].ans = tree[lson].ans + tree[rson].ans;
	tree[h].max = max(tree[lson].max, tree[rson].max);
}
ll ask(ll h, ll l, ll r) {
	ll all = 0;
	if (tree[h].l >= l && tree[h].r <= r) {
		return tree[h].ans;
	}
	if (tree[lson].r >= l) {
		all += ask(lson, l, r);
	}
	if (tree[rson].l <= r) {
		all += ask(rson, l, r);
	}
	return all;
}
int main() {
	//ios::sync_with_stdio(false);
	//cin.tie(0);
	//cout.tie(0);
	while (cin>>n) {
		//cout << "Case #" << ++k << ':' << endl;
		for (int i = 1; i <= n; i++) {
			arr[i]=read();
		}
		memset(tree, 0, sizeof(tree));
		build(1, 1, n);
		m=read();
		for (int i = 1; i <= m; i++) {
			su1 = read(); su2 = read(); su3 = read();
			if (su2 > su3)swap(su2, su3);
			if (su1 == 0)modify(1, su2, su3);
			if (su1 == 1)cout << ask(1, su2, su3) << endl;
		}
	}
	return 0;
}
2023/1/26 15:20
加载中...