Can you answer these queries IV(线段树开根优化)

Can you answer these queries IV

You are given a sequence A of N(N <= 100,000) positive integers. There sum will be less than 1018. On this sequence you have to apply M (M <= 100,000) operations:

(A) For given x,y, for each elements between the x-th and the y-th ones (inclusively, counting from 1), modify it to its positive square root (rounded down to the nearest integer).

(B) For given x,y, query the sum of all the elements between the x-th and the y-th ones (inclusively, counting from 1) in the sequence.

Input

Multiple test cases, please proceed them one by one. Input terminates by EOF.

For each test case:

The first line contains an integer N. The following line contains N integers, representing the sequence A1…AN.
The third line contains an integer M. The next M lines contain the operations in the form “i x y”.i=0 denotes the modify operation, i=1 denotes the query operation.

Output

For each test case:

Output the case number (counting from 1) in the first line of output. Then for each query, print an integer as the problem required.

Print an blank line after each test case.

See the sample output for more details.

思路

题意就是说要维护对某一个连续子段开根和查询总和的操作。由于1e18大概开根六七次就会变成1,所以后面很多操作都在做无用功,所以当子段和等于子段长度时直接略去即可。

#include <bits/stdc++.h>
typedef long double ld;
typedef long long ll;
#define pb push_back
#define mk make_pair
#define mt make_tuple
#define eb emplace_back
#define pob pop_back
#define rz resize
#define mem(a,b) memset(a,b,sizeof(a))
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define debug(a) cout<<#a<<"="<<a<<endl;
#define xx first
#define yy second
#define qwe(i,a,b) for(int i=a;i<=b;i++)
#define ewq(i,a,b) for(int i=a;i>=b;i--)
inline ll rr(){ll f=1,x=0;char ch;do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');return f*x;}
using namespace std;
// ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const ll INF=0x3f3f3f3f,inf=0x3f3f3f3f3f3f3f;
const ll mod[2]={int(1e9 + 7), 10007};
const int base[2]={29,31};
const int maxn=1e5+6;
ll qpow(ll x,ll n){ll ans=1;while(n>0){if(n&1)ans=ans*x%mod[1];x=x*x%mod[1];n>>=1;}return ans;}

int n,m,_;
ll a[maxn*4];
inline void update(int p) {
    a[p]=a[p<<1]+a[p<<1|1];
}
void build(int s,int t,int p) {
    if(s == t) {a[p]=rr();return ;}
    int mid=(s+t)>>1;
    build(s,mid,p<<1),build(mid+1,t,p<<1|1);
    update(p);
}
void modify(int le,int ri,int s,int t,int p) {
    if(le<=s && t<=ri) if(a[p]==t-s+1) return ;
    if(s == t) {a[p]=(ll)sqrt(a[p]);return ;}
    int mid=(s+t)>>1;
    if(le<=mid) modify(le,ri,s,mid,p<<1);
    if(ri >mid) modify(le,ri,mid+1,t,p<<1|1);
    update(p);
}
ll query(int le,int ri,int s,int t,int p) {
    ll sum=0;
    if(le<=s && t<=ri) {return a[p];}
    int mid=(s+t)>>1;
    if(le<=mid) sum+=query(le,ri,s,mid,p<<1);
    if(ri >mid) sum+=query(le,ri,mid+1,t,p<<1|1);
    return sum;
}
int main(int argc, char const *argv[]) {
    while (~scanf("%d",&n)) {
        std::cout << "Case #" << ++_ << ":\n";
        build(1,n,1);
        m=rr();
        while (m--) {
            int op=rr(),x=rr(),y=rr();
            if(x>y) swap(x,y);
            if(op) {
                std::cout << query(x,y,1,n,1) << '\n';
            }else {
                modify(x,y,1,n,1);
            }
        }
        std::cout << '\n';
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值