1. 程式人生 > 其它 >P4145 上帝造題的七分鐘2 / 花神遊歷各國 (線段樹區間開方)

P4145 上帝造題的七分鐘2 / 花神遊歷各國 (線段樹區間開方)

技術標籤:線段樹

題目連結:點選這裡

題目大意:
給定一個長度為 n n n 的序列 a 1 , a 2 , . . . , a n a_1,a_2,...,a_n a1,a2,...,an ,對序列進行區間開方和區間查詢

題目分析:
因為 1 ≤ a i ≤ 1 e 12 1 \le a_i \le 1e12 1ai1e12 1 e 12 1e12 1e12 連續開方 6 6 6 次就會變成 1 1 1 ,所以修改次數實際上是很少的然後我們用線段樹維護區間和和區間最值,如果區間最值為 1 1 1 就不開方了否則就對每一個權值不為 1 1 1 的點進行開方

具體細節見程式碼:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
#define Inf 0x3f3f3f3f3f3f3f3f
#define int ll
using namespace std; ll read() { ll res = 0,flag = 1; char ch = getchar(); while(ch<'0' || ch>'9') { if(ch == '-') flag = -1; ch = getchar(); } while(ch>='0' && ch<='9') { res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0'; ch = getchar(); } return res*flag; }
const int maxn = 5e5+5; const int mod = 1e9+7; const double pi = acos(-1); const double eps = 1e-8; struct sgt{ int val,maxx; }a[maxn<<2]; int n,m,sco[maxn]; char s[3]; void pushup(int root) { a[root].val = a[root<<1].val+a[root<<1|1].val; a[root].maxx = max(a[root<<1].maxx,a[root<<1|1].maxx); } void build(int root,int l,int r) { if(l == r) { a[root].val = a[root].maxx = read(); return ; } int mid = l+r>>1; build(root<<1,l,mid); build(root<<1|1,mid+1,r); pushup(root); } void updat(int root,int l,int r,int ql,int qr) { if(l>qr || r<ql) return ; if(l == r) { a[root].val = sqrt(a[root].val); a[root].maxx = sqrt(a[root].maxx); return ; } int mid = l+r>>1; if(a[root<<1].maxx != 1) updat(root<<1,l,mid,ql,qr); if(a[root<<1|1].maxx != 1) updat(root<<1|1,mid+1,r,ql,qr); pushup(root); } int query(int root,int l,int r,int ql,int qr) { if(l>qr || r<ql) return 0; if(l>=ql && r<=qr) return a[root].val; int mid = l+r>>1; return query(root<<1,l,mid,ql,qr)+query(root<<1|1,mid+1,r,ql,qr); } signed main() { n = read(); build(1,1,n); m = read(); while(m--) { int opt = read(),x = read(),y = read(); if(x > y) swap(x,y); if(opt == 0) updat(1,1,n,x,y); else printf("%lld\n",query(1,1,n,x,y)); } return 0; }