算法1
使用一个set维护当前整个序列中还能够开更号的数的下标
mlogn
时间复杂度
参考文献
C++ 代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
using namespace std;
typedef long long LL;
const int N = 100010;
struct Node{
int l, r;
LL sum;
}tr[N * 4];
set<int> pos;
LL w[N];
vector<int> eds;
int n, m;
void pushup(int u)
{
tr[u].sum = tr[u << 1].sum + tr[u << 1 | 1].sum;
}
void build(int u, int l, int r)
{
tr[u] = {l, r};
if(l == r)
{
tr[u].sum = w[l];
return;
}
int mid = (l + r) / 2;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
void modify(int u, int x)
{
if(tr[u].l == tr[u].r)
{
tr[u].sum = sqrt(tr[u].sum);
if(tr[u].sum <= 1) eds.push_back(x);
return;
}
int mid =(tr[u].l + tr[u].r) / 2;
if(x <= mid) modify(u << 1, x);
else modify(u << 1 | 1, x);
pushup(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) / 2;
LL res = 0;
if(l <= mid) res += query(u << 1, l, r);
if(r > mid) res += query(u << 1 | 1, l, r);
return res;
}
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; ++ i)
{
scanf("%lld", &w[i]);
if(w[i] > 1ll) pos.insert(i);
}
build(1, 1, n);
scanf("%d", &m);
while(m --)
{
int op, l, r;
scanf("%d %d %d", &op, &l, &r);
if(op == 1) printf("%lld\n", query(1, l, r));
else
{
auto iter = pos.lower_bound(l);
eds.clear();
while(iter != pos.end() && *iter <= r)
{
modify(1, *iter);
++ iter;
}
for(auto &t: eds) pos.erase(t);
}
}
return 0;
}