题目链接
对于这种看似比较高大上的题目。(单点修改运算与pushup运算不符合交换律。)但是,我们发现,对于一个long long咱们开平方没多久就会变成不变的1。咱们只要标记一下这个区间是不是都是1,如果都是,就直接return掉。
ps.线段树常见坑点不保证给出的区间两端点前者小于等于后者。又是一个可以让我wa一天的地
下面是ac代码(采用边读入边建树的方式):
#include <iostream>
#include <cstdio>
#include <queue>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define ll long long
using namespace std;
const int N = 1e5+5;
struct Node
{
int l, r;
ll w;
bool cur;
}tr[N<<2];
int n;
int mx;
void build(int p, int l, int r)
{
mx = max(p, mx);
tr[p].l = l; tr[p].r = r;
if (l == r)
{
scanf("%lld", &tr[p].w);
tr[p].cur = tr[p].w == 1?1:0;
return;
}
int mid = (l + r) >> 1;
build(p<<1, l, mid);
build(p<<1|1, mid+1, r);
tr[p].cur = tr[p<<1].cur & tr[p<<1|1].cur;
tr[p].w = tr[p<<1].w + tr[p<<1|1].w;
}
void change(int p, int l, int r)
{
if (tr[p].cur) return;
if (tr[p].l == tr[p].r)
{
tr[p].w = (int)sqrt((double)tr[p].w);
if (tr[p].w == 1) tr[p].cur = 1;
return;
}
int mid = (tr[p].l + tr[p].r) >> 1;
if(l <= mid) change(p<<1, l, r);
if (r > mid) change(p<<1|1, l, r);
tr[p].w = tr[p<<1].w + tr[p<<1|1].w;
tr[p].cur = tr[p<<1].cur & tr[p<<1|1].cur;
}
ll ask(int p, int l, int r)
{
if (tr[p].l >= l && tr[p].r <= r)
return tr[p].w;
int mid = (tr[p].l + tr[p].r) >> 1;
ll val = 0;
if (l <= mid)val += ask(p<<1, l, r);
if (r > mid) val += ask(p<<1|1, l, r);
return val;
}
void print()
{
for (int i = 1; i <= mx; i++)
cout << tr[i].l << " " << tr[i].r << " " << tr[i].w << " " <<tr[i].cur <<endl;
cout <<endl;
}
int main()
{
int t0 = 1;
while(~scanf("%d", &n))
{
build(1, 1, n);
// print();
int q;
scanf("%d", &q);
printf("Case #%d:\n", t0++);
while(q--)
{
int op;
int a, b;
scanf("%d%d%d", &op, &a, &b);
if (a > b) swap(a, b);
if (op)
printf("%lld\n", ask(1, a, b));
else
change(1, a, b);
}
}
return 0;
}