BZOJ 3221 花神游历各国 (线段树)
2015-04-13 21:03
239 查看
题目链接:BZOJ 3221
最多进行5次操作,数将变为1,之后就不会改变。可以记录一个值col表示数是否需要开平方的操作。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
using namespace std;
#define LL long long
const int maxn = 100000 + 10;
int N, M;
int a[maxn];
struct node{
int l, r, col;
LL sum;
}t[maxn * 4];
inline int read(){
int x = 0, f = 1; char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-')f = -1; ch = getchar();}
while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0'; ch = getchar();}
return x * f;
}
void input(){
N = read();
for(int i = 1; i <= N; ++i){
a[i] = read();
}
}
void pushup(int x){
if(t[x << 1].col || t[x << 1 | 1].col)t[x].col = 1;
else t[x].col = 0;
t[x].sum = t[x << 1].sum + t[x << 1 | 1].sum;
}
void built(int x, int l, int r){
t[x].l = l; t[x].r = r;
if(l == r){
t[x].sum = (LL)a[l];
if(t[x].sum <= 1)t[x].col = 0;
else t[x].col = 1;
return ;
}
int mid = (l + r) >> 1;
built(x << 1, l, mid); built(x << 1 | 1, mid + 1, r);
pushup(x);
}
LL ask(int x, int l, int r){
if(t[x].l >= l && t[x].r <= r){
return t[x].sum;
}
int mid = (t[x].l + t[x].r) >> 1;
LL tot = 0;
if(l <= mid)tot += ask(x << 1, l, r);
if(r > mid)tot += ask(x << 1 | 1, l, r);
return tot;
}
void modify(int x, int l, int r){
if(!t[x].col)return ;
if(t[x].l >= l && t[x].r <= r && t[x].l == t[x].r){
t[x].sum = (LL)sqrt(t[x].sum);
if(t[x].sum <= 1)t[x].col = 0;
else t[x].col = 1;
return ;
}
int mid = (t[x].l + t[x].r) >> 1;
if(l <= mid)modify(x << 1, l, r);
if(r > mid)modify(x << 1 | 1, l, r);
pushup(x);
}
void solve(){
built(1, 1, N);
M = read();
for(int i = 1; i <= M; ++i){
int op = read(), L = read(), R = read();
if(op == 1){
printf("%lld\n", ask(1, L, R));
}
else{
modify(1, L, R);
}
}
}
int main(){
input();
solve();
return 0;
}
最多进行5次操作,数将变为1,之后就不会改变。可以记录一个值col表示数是否需要开平方的操作。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
using namespace std;
#define LL long long
const int maxn = 100000 + 10;
int N, M;
int a[maxn];
struct node{
int l, r, col;
LL sum;
}t[maxn * 4];
inline int read(){
int x = 0, f = 1; char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-')f = -1; ch = getchar();}
while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0'; ch = getchar();}
return x * f;
}
void input(){
N = read();
for(int i = 1; i <= N; ++i){
a[i] = read();
}
}
void pushup(int x){
if(t[x << 1].col || t[x << 1 | 1].col)t[x].col = 1;
else t[x].col = 0;
t[x].sum = t[x << 1].sum + t[x << 1 | 1].sum;
}
void built(int x, int l, int r){
t[x].l = l; t[x].r = r;
if(l == r){
t[x].sum = (LL)a[l];
if(t[x].sum <= 1)t[x].col = 0;
else t[x].col = 1;
return ;
}
int mid = (l + r) >> 1;
built(x << 1, l, mid); built(x << 1 | 1, mid + 1, r);
pushup(x);
}
LL ask(int x, int l, int r){
if(t[x].l >= l && t[x].r <= r){
return t[x].sum;
}
int mid = (t[x].l + t[x].r) >> 1;
LL tot = 0;
if(l <= mid)tot += ask(x << 1, l, r);
if(r > mid)tot += ask(x << 1 | 1, l, r);
return tot;
}
void modify(int x, int l, int r){
if(!t[x].col)return ;
if(t[x].l >= l && t[x].r <= r && t[x].l == t[x].r){
t[x].sum = (LL)sqrt(t[x].sum);
if(t[x].sum <= 1)t[x].col = 0;
else t[x].col = 1;
return ;
}
int mid = (t[x].l + t[x].r) >> 1;
if(l <= mid)modify(x << 1, l, r);
if(r > mid)modify(x << 1 | 1, l, r);
pushup(x);
}
void solve(){
built(1, 1, N);
M = read();
for(int i = 1; i <= M; ++i){
int op = read(), L = read(), R = read();
if(op == 1){
printf("%lld\n", ask(1, L, R));
}
else{
modify(1, L, R);
}
}
}
int main(){
input();
solve();
return 0;
}
相关文章推荐
- 【BZOJ3211】花神游历各国(线段树)
- BZOJ 3211: 花神游历各国(势能分析线段树)
- bzoj3211 花神游历各国 线段树
- BZOJ 3211-花神游历各国(线段树)
- BZOJ3038 && BZOJ3211 上帝造题的七分钟2 && 花神游历各国 (线段树 + 开方标记)
- BZOJ 3211 花神游历各国 树状数组(线段树)+优化
- [bzoj3211]花神游历各国 线段树
- BZOJ 3211 花神游历各国 线段树
- BZOJ3038 && BZOJ3211 上帝造题的七分钟2 && 花神游历各国 (线段树 + 开方标记)
- 树线段BZOJ 3211(花神游历各国-线段树区间开方)
- [BZOJ3211]花神游历各国(线段树)
- [BZOJ3211]花神游历各国(线段树+区间开根)
- 【BZOJ】【P3211】【花神游历各国】【题解】【线段树】
- 【线段树】【bzoj 3211】花神游历各国
- 【线段树】bzoj3038 上帝造题的七分钟2 / bzoj3211 花神游历各国
- BZOJ 3211: 花神游历各国( 线段树 )
- 线段树【 bzoj3132 】【p4145 】上帝造题的七分钟2 / 花神游历各国
- BZOJ 3211 花神游历各国 - 线段树
- bzoj3211花神游历各国 线段树
- 【BZOJ 3211】花神游历各国 &【 线段树区间开根】