HDU 5828 Rikka with Sequence(线段树 开根号)

时间:2024-08-18 13:04:14

Rikka with Sequence

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2777    Accepted Submission(s): 503

Problem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:

Yuta has an array A with n numbers. Then he makes m operations on it.

There are three type of operations:

1 l r x : For each i in [l,r], change A[i] to A[i]+x
2 l r : For each i in [l,r], change A[i] to ⌊A−−√[i]⌋
3 l r : Yuta wants Rikka to sum up A[i] for all i in [l,r]

It is too difficult for Rikka. Can you help her?

Input
The first line contains a number t(1<=t<=100), the number of the testcases. And there are no more than 5 testcases with n>1000.

For each testcase, the first line contains two numbers n,m(1<=n,m<=100000). The second line contains n numbers A[1]~A[n]. Then m lines follow, each line describe an operation.

It is guaranteed that 1<=A[i],x<=100000.

Output
For each operation of type 3, print a lines contains one number -- the answer of the query.
Sample Input
1
5 5
1 2 3 4 5
1 3 5 2
2 1 4
3 2 4
2 3 5
3 1 5
Sample Output
5
6
Author
学军中学

【分析】这个题唯一的难点就是开根号,有两个地方没有想到,想到了就会写了。首先,对于一个数,开根号可以转化成减号,详细肩带吗。其次,对于一个区间,如果最大值==最小值,那么所有的值开完根号都相等了,还有种就就是最大值-最小值==1的时候,可能存在开完根号后差值还是一,这个时候需要特判。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int BufferSize=<<;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
if(head==tail) {
int l=fread(buffer,,BufferSize,stdin);
tail=(head=buffer)+l;
}
return *head++;
}
inline int read() {
int x=,f=;char c=Getchar();
for(;!isdigit(c);c=Getchar()) if(c=='-') f=-;
for(;isdigit(c);c=Getchar()) x=x*+c-'';
return x*f;
}
const int N=1e5+;
ll sum[N<<],lz[N<<],mx[N<<],mn[N<<];
void pushUp(int rt){
sum[rt]=sum[rt<<]+sum[rt<<|];
mx[rt]=max(mx[rt<<],mx[rt<<|]);
mn[rt]=min(mn[rt<<],mn[rt<<|]);
}
void build(int rt,int l,int r){
lz[rt]=;
if(l==r){sum[rt]=read();mn[rt]=mx[rt]=sum[rt];return;}
int mid=l+r>>;
build(rt<<,l,mid);build(rt<<|,mid+,r);
pushUp(rt);
}
void pushDown(int rt,int l,int r){
if(lz[rt]!=){
int mid=l+r>>;
lz[rt<<]+=lz[rt];
lz[rt<<|]+=lz[rt];
mn[rt<<]+=lz[rt];
mx[rt<<]+=lz[rt];
mx[rt<<|]+=lz[rt];
mn[rt<<|]+=lz[rt];
sum[rt<<]+=lz[rt]*(mid-l+);
sum[rt<<|]+=lz[rt]*(r-mid);
lz[rt]=;
}
}
int x,y,t,T,n,m;
void sqrtUpdate(int rt,int l,int r){
if(x<=l&&r<=y){
if(mx[rt]==mn[rt]){
lz[rt]-=mx[rt];
mx[rt]=sqrt(mx[rt]);
mn[rt]=mx[rt];
lz[rt]+=mx[rt];
sum[rt]=mx[rt]*(r-l+);
return;
}
else if(mx[rt]==mn[rt]+){
ll x1=sqrt(mx[rt]);
ll x2=sqrt(mn[rt]);
if(x1==x2+){
lz[rt]-=(mx[rt]-x1);
sum[rt]-=(mx[rt]-x1)*(r-l+);
mx[rt]=x1;mn[rt]=x2;
return;
}
}
}
int mid=l+r>>;
pushDown(rt,l,r);
if(x<=mid)sqrtUpdate(rt<<,l,mid);
if(y>mid)sqrtUpdate(rt<<|,mid+,r);
pushUp(rt);
}
void addUpdate(int rt,int l,int r){
if(x<=l&&r<=y){
lz[rt]+=t;
sum[rt]+=1ll*(r-l+)*t;
mx[rt]+=t;mn[rt]+=t;
return ;
}
int mid=l+r>>;
pushDown(rt,l,r);
if(x<=mid)addUpdate(rt<<,l,mid);
if(y>mid)addUpdate(rt<<|,mid+,r);
pushUp(rt);
}
ll query(int rt,int l,int r){
if(x<=l&&r<=y)return sum[rt];
int mid=l+r>>;
pushDown(rt,l,r);
ll ret=;
if(x<=mid)ret+=query(rt<<,l,mid);
if(y>mid)ret+=query(rt<<|,mid+,r);
return ret;
}
int main(){
T=read();
while(T--){
n=read();m=read();
build(,,n);
while(m--){
int op;
op=read();x=read();y=read();
if(op==){
t=read();addUpdate(,,n);
}
else if(op==)sqrtUpdate(,,n);
else printf("%I64d\n",query(,,n));
}
}
return ;
}