题目描述
分析
这个题贼妙啊
一看这个描述就知道肯定使用线段树来处理,但是涉及到区间开方就会比较头疼,如果我们把区间修改退化成单调修改的话,最坏情况下的复杂度会达到
O
(
n
m
l
o
g
n
)
O(nmlogn)
O(nmlogn),显然会爆掉
我们去分析一下,每个数的范围都是在
1
0
9
10^9
109之内,也就是说最多连续操作
30
30
30次左右就会变成1,这个时候再去开方就显得没有意义了
所以我们去记录每个区间的最大值,如果最大值为
0
0
0或
1
1
1的话,那么我们去给这个区间打上标记,下次修改的时候直接跳过即可
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
struct Nodd{
int l,r;
ll sum;
int add;
}tr[N << 2];
int n,m;
void push(int u){
tr[u].sum = tr[u << 1].sum + tr[u << 1 | 1].sum;
if((tr[u << 1].add == 2) || (tr[u << 1 | 1].add == 2)) tr[u].add = 2;
else tr[u].add = 1;
}
void build(int u,int l,int r){
tr[u] = {l,r,0,0};
if(l == r){
read(tr[u].sum);
if(tr[u].sum <= 1) tr[u].add = 1;
else tr[u].add = 2;
return;
}
int mid = l + r >> 1;
build(u << 1,l,mid),build(u << 1 | 1,mid + 1,r);
push(u);
}
void down(int u){
if(tr[u].add <= 1) return;
if(tr[u].l == tr[u].r){
tr[u].sum = sqrt(tr[u].sum);
if(tr[u].sum <= 1) tr[u].add = 1;
return;
}
down(u << 1);
down(u << 1 | 1);
push(u);
}
void modify(int u,int l,int r){
if(tr[u].add <= 1) return;
if(tr[u].l >= l && tr[u].r <= r){
down(u);
return;
}
int mid = tr[u].l + tr[u].r >> 1;
if(l <= mid) modify(u << 1,l,r);
if(r > mid) modify(u << 1 | 1,l,r);
push(u);
}
ll query(int u,int l,int r){
if(tr[u].l >= l && tr[u].r <= r) return tr[u].sum;
int mid = tr[u].l + tr[u].r >> 1;
ll ans = 0;
if(l <= mid) ans = query(u << 1,l,r);
if(r > mid) ans += query(u << 1 | 1,l,r);
return ans;
}
int main() {
read(n);
build(1,1,n);
read(m);
while(m--){
int op,l,r;
read(op),read(l),read(r);
if(op == 1) printf("%lld\n",query(1,l,r));
else modify(1,l,r);
}
return 0;
}
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/