花神游历各国 HYSBZ - 3211

 

Input

花神游历各国 HYSBZ - 3211

Output

每次x=1时,每行一个整数,表示这次旅行的开心度

 

Sample Input

4 1 100 5 5 5 1 1 2 2 1 2 1 1 2 2 2 3 1 1 4

Sample Output

101 11 11

Hint

 

对于100%的数据, n ≤ 100000,m≤200000 ,data[i]非负且小于10^9

 


线段树暴力修改就行了,

放一个luogu的代码;板子题,不解释

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 200005
#define inf 0x3f3f3f3f
#define INF 999999999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const int mod = 10000007;
#define Mod 20100403
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }


int n;
struct node {
	int l, r;
	ll lazy;ll sum;
	ll Max;
}tree[maxn<<2];

void pushup(int rt) {
	tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
	tree[rt].Max = max(tree[rt << 1].Max, tree[rt << 1 | 1].Max);
}

void pushdpwn(int rt) {
	if (tree[rt].lazy) {
		tree[rt << 1].sum = tree[rt].lazy; tree[rt << 1 | 1].sum = tree[rt].lazy;
		tree[rt << 1].lazy = tree[rt].lazy; tree[rt << 1 | 1].lazy = tree[rt].lazy;
		tree[rt].lazy = 0;
	}
}

void build(int l, int r, int rt) {
	tree[rt].l = l; tree[rt].r = r; tree[rt].lazy = 0;
	if (l == r) {
		rdllt(tree[rt].sum); tree[rt].Max = tree[rt].sum;
		return;
	}
	int mid = (l + r) >> 1;
	build(l, mid, rt << 1); build(mid + 1, r, rt << 1 | 1); pushup(rt);
}

void update(int l, int r, int rt) {
	if (tree[rt].l == tree[rt].r) {
		if (tree[rt].sum == 0 || tree[rt].sum == 1)return;
		tree[rt].sum = (ll)sqrt(tree[rt].sum);
		tree[rt].Max = (ll)sqrt(tree[rt].Max);
		tree[rt].lazy= (ll)sqrt(tree[rt].sum);
		return;
	}
	pushdpwn(rt);
	int mid = (tree[rt].l + tree[rt].r) >> 1;
	if (l <= mid && tree[rt << 1].Max > 1)update(l, r, rt << 1);
	if (mid < r&&tree[rt << 1 | 1].Max>1)update(l, r, rt << 1 | 1);
	pushup(rt);
}

ll query(int l, int r, int rt) {
	if (l <= tree[rt].l&&tree[rt].r <= r) {
		return tree[rt].sum;
	}
	pushdpwn(rt);
	ll ans = 0; int mid = (tree[rt].l + tree[rt].r) >> 1;
	if (l <= mid)ans += query(l, r, rt << 1);
	if (mid < r)ans += query(l, r, rt << 1 | 1);
	return ans;
}

int main()
{
	//ios::sync_with_stdio(false);
	rdint(n); build(1, n, 1);
	int m;
	rdint(m);
	int opt, l, r;
	while (m--) {
		rdint(opt); rdint(l); rdint(r); if (l > r)swap(l, r);
		if (opt == 0)update(l, r, 1);
		if (opt == 1) {
			cout << query(l, r, 1) << endl;
		}
	}
    return 0;
}

 

相关文章: