题意
给出一些数,有两种操作。(1)将区间内每一个数开方(2)查询每一段区间的和
分析
普通的线段树保留修改+开方优化。可以知道当一个数为0或1时,无论开方几次,答案仍然相同。所以设置flag=1变表示这一段区间全是0/1,那么修改的时候直接暴力遍历线段树结点。因为一个数被开方下取整到1,只会被开方几次,所以说这样修改是的.总时间还是
CODE1
上帝造题的七分钟2
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
char cb[1<<15],*cs=cb,*ct=cb;
#define getc() (cs==ct&&(ct=(cs=cb)+fread(cb,1,1<<15,stdin),cs==ct)?0:*cs++)
template<class T>inline void read(T &res) {
char ch; int flg = 1; for(;!isdigit(ch=getc());)if(ch=='-')flg=-flg;
for(res=ch-'0';isdigit(ch=getc());res=res*10+ch-'0'); res*=flg;
}
const int MAXN = 100005;
int n, m;
struct seg {
LL sum; bool flg;
}t[MAXN<<2];
inline void upd(int i) {
t[i].sum = t[i<<1].sum + t[i<<1|1].sum;
t[i].flg = t[i<<1].flg & t[i<<1|1].flg;
}
void build(int i, int l, int r) {
t[i].flg = 0;
if(l == r) {
read(t[i].sum);
if(t[i].sum <= 1) t[i].flg = 1;
return;
}
int mid = (l + r) >> 1;
build(i<<1, l, mid);
build(i<<1|1, mid+1, r);
upd(i);
}
void modify(int i, int l, int r, int x, int y) {
if(t[i].flg) return;
if(l == r) {
t[i].sum = sqrt(t[i].sum);
if(t[i].sum <= 1) t[i].flg = 1;
return;
}
int mid = (l + r) >> 1;
if(x <= mid) modify(i<<1, l, mid, x, y);
if(y > mid) modify(i<<1|1, mid+1, r, x, y);
upd(i);
}
LL query(int i, int l, int r, int x, int y) {
if(x <= l && r <= y) return t[i].sum;
int mid = (l + r) >> 1;
if(y <= mid) return query(i<<1, l, mid, x, y);
else if(x > mid) return query(i<<1|1, mid+1, r, x, y);
else return query(i<<1, l, mid, x, y) + query(i<<1|1, mid+1, r, x, y);
}
int main () {
read(n); build(1, 1, n); read(m);
int x, y, op;
while(m--) {
read(op), read(x), read(y);
if(x > y) swap(x, y); //坑!
if(!op) modify(1, 1, n, x, y);
else printf("%lld
", query(1, 1, n, x, y));
}
}
CODE 2
花神游历各国
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
char cb[1<<15],*cs=cb,*ct=cb;
#define getc() (cs==ct&&(ct=(cs=cb)+fread(cb,1,1<<15,stdin),cs==ct)?0:*cs++)
template<class T>inline void read(T &res) {
char ch; int flg = 1; for(;!isdigit(ch=getc());)if(ch=='-')flg=-flg;
for(res=ch-'0';isdigit(ch=getc());res=res*10+ch-'0'); res*=flg;
}
const int MAXN = 100005;
int n, m;
struct seg {
LL sum; bool flg;
}t[MAXN<<2];
inline void upd(int i) {
t[i].sum = t[i<<1].sum + t[i<<1|1].sum;
t[i].flg = t[i<<1].flg & t[i<<1|1].flg;
}
void build(int i, int l, int r) {
t[i].flg = 0;
if(l == r) {
read(t[i].sum);
if(t[i].sum <= 1) t[i].flg = 1;
return;
}
int mid = (l + r) >> 1;
build(i<<1, l, mid);
build(i<<1|1, mid+1, r);
upd(i);
}
void modify(int i, int l, int r, int x, int y) {
if(t[i].flg) return;
if(l == r) {
t[i].sum = sqrt(t[i].sum);
if(t[i].sum <= 1) t[i].flg = 1;
return;
}
int mid = (l + r) >> 1;
if(x <= mid) modify(i<<1, l, mid, x, y);
if(y > mid) modify(i<<1|1, mid+1, r, x, y);
upd(i);
}
LL query(int i, int l, int r, int x, int y) {
if(x <= l && r <= y) return t[i].sum;
int mid = (l + r) >> 1;
if(y <= mid) return query(i<<1, l, mid, x, y);
else if(x > mid) return query(i<<1|1, mid+1, r, x, y);
else return query(i<<1, l, mid, x, y) + query(i<<1|1, mid+1, r, x, y);
}
int main () {
read(n); build(1, 1, n); read(m);
int x, y, op;
while(m--) {
read(op), read(x), read(y);
if(op == 2) modify(1, 1, n, x, y);
else printf("%lld
", query(1, 1, n, x, y));
}
}