3211: 花神游历各国
Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 2475 Solved: 921
[ Submit][ Status][ Discuss]
Description
Input
Output
每次x=1时,每行一个整数,表示这次旅行的开心度
Sample Input
4
1 100 5 5
5
1 1 2
2 1 2
1 1 2
2 2 3
1 1 4
1 100 5 5
5
1 1 2
2 1 2
1 1 2
2 2 3
1 1 4
Sample Output
101
11
11
11
11
HINT
对于100%的数据, n ≤ 100000,m≤200000 ,data[i]非负且小于10^9
Source
解题思路:一般线段树做法,注意要稍微优化一下,当节点为0或1时就不需要更新了,当
左节点以及右节点都为0或1时不需要更新。
/* ***********************************************
┆ ┏┓ ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃ ┃ ┆
┆┃ ━ ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃ ┃ ┆
┆┃ ┻ ┃ ┆
┆┗━┓ 马 ┏━┛ ┆
┆ ┃ 勒 ┃ ┆
┆ ┃ 戈 ┗━━━┓ ┆
┆ ┃ 壁 ┣┓┆
┆ ┃ 的草泥马 ┏┛┆
┆ ┗┓┓┏━┳┓┏┛ ┆
┆ ┃┫┫ ┃┫┫ ┆
┆ ┗┻┛ ┗┻┛ ┆
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <bitset>
using namespace std;
#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)
#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)
#define pb push_back
#define mp make_pair
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define LL long long
#define ULL unsigned long long
#define MS0(X) memset((X), 0, sizeof((X)))
#define SelfType int
SelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}
SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}
#define Sd(X) int (X); scanf("%d", &X)
#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define all(a) a.begin(), a.end()
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
const int N = 1e5 + 5;
struct segtree
{
int l,r;
LL sum;
int flag;
}seg[4*N];
LL a[N];
void build(int x,int l,int r)
{
seg[x].l = l, seg[x].r = r;
if(l==r)
{
seg[x].sum = a[l];
if(seg[x].sum<=1)seg[x].flag = 1;
return;
}
int mid = (l+r) >> 1;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
seg[x].sum = seg[x<<1].sum + seg[x<<1|1].sum;
seg[x].flag = seg[x<<1].flag & seg[x<<1|1].flag;
}
void update(int x,int L,int R)
{
int l = seg[x].l, r = seg[x].r;
if(seg[x].flag)return;
if(l==r)
{
seg[x].sum = (LL)sqrt(seg[x].sum);
if(seg[x].sum<=1)seg[x].flag = 1;
return;
}
int mid = (l+r) >> 1;
if(R<=mid) update(x<<1,L,R);
else if(L>mid) update(x<<1|1,L,R);
else
{
update(x,L,mid);
update(x,mid+1,R);
}
seg[x].sum = seg[x<<1].sum + seg[x<<1|1].sum;
seg[x].flag = seg[x<<1].flag & seg[x<<1|1].flag;
}
LL query(int x,int L,int R)
{
int l = seg[x].l, r = seg[x].r;
if(l>=L && r<=R)
{
return seg[x].sum;
}
int mid = (l+r) >> 1;
if(R<=mid) return query(x<<1,L,R);
else if(L>mid) return query(x<<1|1,L,R);
else return query(x<<1,L,mid) + query(x<<1|1,mid+1,R);
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
int n,m;
n = read();
rep(i,1,n)a[i] = read();
build(1,1,n);
m = read();
while(m--)
{
int x,a,b;
scanf("%d%d%d",&x,&a,&b);
if(x==2)update(1,a,b);
else printf("%lld\n",query(1,a,b));
}
return 0;
}