GSS7 Can you answer these queries IV

题目:给出一个数列,原数列和值不超过1e18,有两种操作:

0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数

1 x y:询问区间[x,y]的和

分析:

  昨天初看时没什么想法,于是留了个坑。终于在今天补上了。

  既然给出了1e18这个条件,那么有什么用呢?于是想到了今年多校一题线段树区间操作时,根据一些性质能直接下沉到每个节点,这里可以吗?考虑1e18开方6次就下降到1了,因此每个节点最多被修改6次。于是我们每个节点(区间)记录一个该区间的最大值,每次修改时,先判断该区间是否最大的数已经等于1,等于的话,就不用继续往下修改了。不然的话,继续往下下沉。下沉到最终的区间时发现最大的数还大于1时,进行单点修改操作。在单点修改时同样如此判断。修改后update一下sum以及最大值就行。

  交的时候RE了几次,后来看了一下题目下面的讨论,发现

2011-12-27 18:15:29 Riatre Foo 
The only trick is in operations x > y.HORRIBLE

  于是修改了一下,变成TLE了,囧。后来发现是I64d问题,改成lld就过了。代码中有输入的外挂(貌似快了不多) 

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
typedef unsigned long long ull; #define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
#define All(vec) vec.begin(),vec.end()
#define MP make_pair
#define PII pair<int,int>
#define PQ priority_queue
#define cmax(x,y) x = max(x,y)
#define cmin(x,y) x = min(x,y)
#define Clear(x) memset(x,0,sizeof(x))
/* #pragma comment(linker, "/STACK:1024000000,1024000000") int size = 256 << 20; // 256MB
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p) ); */ /******** program ********************/ const int MAXN = 1e5+5; ll a[MAXN]; struct segTree{
int l,r;
ll mx,sum;
inline int mid(){
return (l+r)>>1;
}
}tree[MAXN<<2]; inline void update(int rt){
tree[rt].mx = max(tree[rt<<1].mx,tree[rt<<1|1].mx);
tree[rt].sum = tree[rt<<1].sum+tree[rt<<1|1].sum;
} void build(int l,int r,int rt){
tree[rt].l = l;
tree[rt].r = r;
if(l==r){
tree[rt].sum = tree[rt].mx = a[l];
return;
}
int mid = tree[rt].mid();
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
update(rt);
} void modify(int rt){
if(tree[rt].mx==1LL)return;
if(tree[rt].l==tree[rt].r){
tree[rt].sum = tree[rt].mx = ll( sqrt(tree[rt].mx+0.0) );
return;
}
modify(rt<<1);
modify(rt<<1|1);
update(rt);
} void modify(int l,int r,int rt){
if(tree[rt].mx==1LL)return;
if(l<=tree[rt].l&&tree[rt].r<=r){
modify(rt);
return;
}
int mid = tree[rt].mid();
if(r<=mid) modify(l,r,rt<<1);
else if(l>mid) modify(l,r,rt<<1|1);
else{
modify(l,r,rt<<1);
modify(l,r,rt<<1|1);
}
update(rt);
} ll ask(int l,int r,int rt){
if(l<=tree[rt].l&&tree[rt].r<=r)
return tree[rt].sum;
int mid = tree[rt].mid();
if(r<=mid) return ask(l,r,rt<<1);
else if(l>mid) return ask(l,r,rt<<1|1);
else return ask(l,r,rt<<1) + ask(l,r,rt<<1|1);
} inline void LL(ll &x){
x = 0;
char ch;
while(isdigit(ch=getchar())==0);
x = ch-'0';
while(isdigit(ch=getchar()))
x = x*10+ch-'0';
} inline void Int(int &x){
x = 0;
char ch;
while(isdigit(ch=getchar())==0);
x = ch-'0';
while(isdigit(ch=getchar()))
x = x*10+ch-'0';
} int main(){ #ifndef ONLINE_JUDGE
freopen("sum.in","r",stdin);
//freopen("sum.out","w",stdout);
#endif int n,m,op,x,y;
int ncase = 0;
while(cin>>n){
rep1(i,n){
//scanf("%lld\n",&a[i]);
LL(a[i]);
}
build(1,n,1);
RD(m);
printf("Case #%d:\n",++ncase);
while(m--){
//RD3(op,x,y);
Int(op); Int(x); Int(y);
if(x>y)swap(x,y);
if(op) printf("%lld\n",ask(x,y,1));
else modify(x,y,1);
}
puts("");
} return 0;
}

  

GSS4 2713. Can you answer these queries IV 线段树的更多相关文章

  1. SP2713 GSS4 - Can you answer these queries IV(线段树)

    传送门 解题思路 大概就是一个数很少次数的开方会开到\(1\),而\(1\)开方还是\(1\),所以维护一个和,维护一个开方标记,维护一个区间是否全部为\(1/0\)的标记.然后每次修改时先看是否有全 ...

  2. SPOJ2713GSS4 - Can you answer these queries IV(线段树)

    题意 Sol 讲过无数次了..很显然,一个$10^12$的数开方不超过$8$次后就会变为$1$ 因此直接暴力更改即可,维护一下这段区间是否被全改为了$1$ 双倍经验:https://www.luogu ...

  3. SPOJ GSS1_Can you answer these queries I(线段树区间合并)

    SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...

  4. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  5. GSS5 spoj 2916. Can you answer these queries V 线段树

    gss5 Can you answer these queries V 给出数列a1...an,询问时给出: Query(x1,y1,x2,y2) = Max { A[i]+A[i+1]+...+A[ ...

  6. SPOJ 1557. Can you answer these queries II 线段树

    Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...

  7. bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树

    2482: [Spoj1557] Can you answer these queries II Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 145 ...

  8. HDU 4027—— Can you answer these queries?——————【线段树区间开方,区间求和】

    Can you answer these queries? Time Limit:2000MS     Memory Limit:65768KB     64bit IO Format:%I64d & ...

  9. 【BZOJ2482】[Spoj1557] Can you answer these queries II 线段树

    [BZOJ2482][Spoj1557] Can you answer these queries II Description 给定n个元素的序列. 给出m个询问:求l[i]~r[i]的最大子段和( ...

随机推荐

  1. 转载:Flash AS3.0 加载外部资源(图片,MP3,SWF)的两种方式

    Flash AS3.0 加载外部资源(图片,MP3,SWF)的两种方式 出自:http://www.cnblogs.com/top5/archive/2012/08/04/2623464.html 关 ...

  2. 关于使用Transaction对于非数据库事务的操作

    在操作数据库的过程中,为了数据的一致性,我们可以使用Transaction,要么成功的时候全部提交,要么有任何一个操作失败立即全部回滚.不仅仅是在数据库方面,有时候操作其他的内容,比如说对于系统文件的 ...

  3. HTML第三天学习笔记

    昨天学的超链接,今天深入学习了下,发现了更多的知识点,而且关于初始新建网页时,由于是初学者,所以还是纯手写代码~ <html> <head> <title>超链接& ...

  4. 解决IE6不支持position:fixed属性

    最近在优化网站浮动广告时候遇见了IE6不支持position:fixed属性.上网收集了一下解决方案 比较好的方案就是利用css表达式进行解决 补充:CSS Expression (CSS 表达式), ...

  5. PCA和白化练习之处理图像

    第一步:下载pca_exercise.zip,里面包含有图像数据144*10000,每一列代表一幅12*12的图像块,首先随见展示200幅: 第二步:0均值处理,确保数据均值为0或者接近0 第三步:执 ...

  6. Codeforces Round #309 (Div. 1) B. Kyoya and Permutation 构造

    B. Kyoya and Permutation Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...

  7. 几种Java写webservice的比较

    Java6,Axis2.XFire.CXF 1.JWS是Java语言对WebService服务的一种实现,用来开发和发布服务.而从服务本身的角度来看JWS服务是没有语言界限的.但是Java语言为Jav ...

  8. DirectSound的应用

    假设仅仅使用PlaySound()这个API函数来表现声音效果的话,那么就无法表现出声音的混音效果,由于PlaySound在播放还有一个声音时,必定会导致现有声音的停止.因此,使用 PlaySound ...

  9. Python 初学笔记(转)

    >>> print "isn't that grand"isn't that grand #不需要转义的#为了让文字符扩展到多行,可以在一行的末尾使用反斜线符号, ...

  10. android安卓最新快捷环境搭建(转)

    现在很多视频和文章上的安卓环境搭建还是比较老的,挺麻烦.现在写快速方便的搭建: 一.下载JDK: 网址:http://www.oracle.com/technetwork/java/javase/do ...