1. 程式人生 > >上帝造題的七分鐘2

上帝造題的七分鐘2

個數 name pre 程序 int nod 直接 long desc

Description
XLk覺得《上帝造題的七分鐘》不太過癮,於是有了第二部。
"第一分鐘,X說,要有數列,於是便給定了一個正整數數列。
第二分鐘,L說,要能修改,於是便有了對一段數中每個數都開平方(下取整)的操作。
第三分鐘,k說,要能查詢,於是便有了求一段數的和的操作。
第四分鐘,彩虹喵說,要是noip難度,於是便有了數據範圍。
第五分鐘,詩人說,要有韻律,於是便有了時間限制和內存限制。
第六分鐘,和雪說,要省點事,於是便有了保證運算過程中及最終結果均不超過64位有符號整數類型的表示範圍的限制。
第七分鐘,這道題終於造完了,然而,造題的神牛們再也不想寫這道題的程序了。"

——《上帝造題的七分鐘·第二部》
所以這個神聖的任務就交給你了。

Input
第一行一個整數n,代表數列中數的個數。
第二行n個正整數,表示初始狀態下數列中的數。
第三行一個整數m,表示有m次操作。
接下來m行每行三個整數k,l,r,k=0表示給[l,r]中的每個數開平方(下取整),k=1表示詢問[l,r]中各個數的和。

Output
對於詢問操作,每行輸出一個回答。

Sample Input
10
1 2 3 4 5 6 7 8 9 10
5
0 1 10
1 1 10
1 1 5
0 5 8
1 4 8

Sample Output
19
7
6

HINT
1:對於100%的數據,1<=n<=100000,1<=l<=r<=n,數列中的數大於0,且不超過1e12。

2:數據不保證L<=R 若L>R,請自行交換L,R,謝謝!

區間開方的題一般都是類似暴力,由於沒有加值,所以一個數被開方的次數也是固定的

線段樹記錄當前區間是否都是1,如果是,則直接跳過;如果不是,暴力開方

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
    static char buf[1000000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
    int x=0,f=1; char ch=gc();
    for (;ch<'0'||ch>'9';ch=gc())   if (ch=='-')    f=-1;
    for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<3)+(x<<1)+ch-'0';
    return x*f;
}
inline int read(){
    int x=0,f=1; char ch=getchar();
    for (;ch<'0'||ch>'9';ch=getchar())  if (ch=='-')    f=-1;
    for (;ch>='0'&&ch<='9';ch=getchar())    x=(x<<3)+(x<<1)+ch-'0';
    return x*f;
}
inline void print(int x){
    if (x<0)    putchar('-'),x=-x;
    if (x>9)    print(x/10);
    putchar(x%10+'0');
}
const int N=1e5;
struct S1{
    #define ls (p<<1)
    #define rs (p<<1|1)
    struct node{
        ll sum; bool pas;
        node(){sum=pas=0;}
    }tree[(N<<2)+10];
    friend node operator +(const node &x,const node &y){
        node z;
        z.sum=x.sum+y.sum;
        z.pas=x.pas&y.pas;
        return z;
    }
    void build(int p,int l,int r){
        if (l==r){
            scanf("%lld",&tree[p].sum);
            tree[p].pas=(tree[p].sum==1);
            return;
        }
        int mid=(l+r)>>1;
        build(ls,l,mid),build(rs,mid+1,r);
        tree[p]=tree[ls]+tree[rs];
    }
    void _sqrt(int p,int l,int r,int x,int y){
        if (l==r){
            tree[p].sum=trunc(sqrt(tree[p].sum));
            tree[p].pas=(tree[p].sum==1);
            return;
        }
        if (x<=l&&r<=y&&tree[p].pas)    return;
        int mid=(l+r)>>1;
        if (x<=mid) _sqrt(ls,l,mid,x,y);
        if (y>mid)  _sqrt(rs,mid+1,r,x,y);
        tree[p]=tree[ls]+tree[rs];
    }
    ll Query(int p,int l,int r,int x,int y){
        if (x<=l&&r<=y) return tree[p].sum;
        int mid=(l+r)>>1; ll res=0;
        if (x<=mid) res+=Query(ls,l,mid,x,y);
        if (y>mid)  res+=Query(rs,mid+1,r,x,y);
        return res;
    }
    #undef ls
    #undef rs
}ST;//Segment Tree
int main(){
    int n=read();
    ST.build(1,1,n);
    int m=read();
    for (int i=1;i<=m;i++){
        int type=read(),x=read(),y=read();
        if (x>y)    swap(x,y);
        if (type==0)    ST._sqrt(1,1,n,x,y);
        if (type==1)    printf("%lld\n",ST.Query(1,1,n,x,y));
    }
    return 0;
}

上帝造題的七分鐘2