Can you answer these queries? HDU - 4027
本题是一个看起来像是区间修改的单点修改线段树,因为区间修改不好维护。这个也太难为我了,我想了想好像我不会这种题目,只能暴力求解了,但是问题来了,单点修改的话,绝对超时,但是我不信,写了一发,wa的很开心,然后就想怎么减枝,仔细想了想发现当一个区间的最大值小于等于1的时候就不用修改的,那就很舒服,因为每次是开根号,所以很快就会变成1或0了,时间复杂度是O(能过),值得注意的是x和y的大小是不确定的
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<deque>
#include<map>
#include<stdlib.h>
#include<set>
#include<iomanip>
#include<stack>
#define ll long long
#define ms(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x & -x
#define fi first
#define ull unsigned long long
#define se second
#define lson (rt<<1)
#define rson (rt<<1|1)
#define endl "\n"
#define bug cout<<"----acac----"<<endl
#define IOS ios::sync_with_stdio(false), cin.tie(0),cout.tie(0)
using namespace std;
const int maxn = 1e5 + 10;
const int maxm = 1.5e5 + 50;
const double eps = 1e-18;
const double inf = 0x3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const double pi = 3.141592653589;
struct node
{
ll l, r, sum,ma;
}a[maxn<<4];
void build(int l,int r,int rt)
{
a[rt].l = l;
a[rt].r = r;
if (l == r)
{
scanf("%lld", &a[rt].sum);
a[rt].ma = a[rt].sum;
return ;
}
int mid = (l + r) >> 1;
build(l, mid, lson);
build(mid + 1, r, rson);
a[rt].sum = a[lson].sum + a[rson].sum;
a[rt].ma = max(a[lson].ma, a[rson].ma);
}
void update(int L, int R, int rt)
{
if (a[rt].ma <= 1)return;
if (a[rt].l ==a[rt] .r)
{
a[rt].sum = sqrt(a[rt].sum);
a[rt].ma = a[rt].sum;
return;
}
int mid = (a[rt].l + a[rt].r) >> 1;
if (L <= mid)
{
update(L, R, lson);
}
if (R > mid)
{
update(L, R, rson);
}
a[rt].sum = a[lson].sum + a[rson].sum;
a[rt].ma = max(a[lson].ma, a[rson].ma);
}
ll query(int L, int R,int rt)
{
if (L <= a[rt].l && R >= a[rt].r)
{
return a[rt].sum;
}
ll ans = 0;
int mid = (a[rt].l + a[rt].r) >> 1;
if (L <= mid)
{
ans += query(L, R, lson);
}
if (R > mid)
{
ans += query(L, R, rson);
}
return ans;
}
int main()
{
int cas = 1;
int n;
while (~scanf("%d", &n))
{
build(1, n, 1);
int m;
scanf("%d", &m);
printf("Case #%d:\n", cas++);
for (int i = 1; i <= m; i++)
{
int T, x, y;
scanf("%d%d%d", &T, &x, &y);
if (y < x)swap(x, y);
if (T == 0)
{
update(x, y, 1);
}
else
{
printf("%lld\n", query(x, y,1));
}
}
printf("\n");
}
return 0;
}