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. C#中的Collection 2

    Core Generic interface IEnumerable<T>:you can interate my elemnts, no need to know the count, ...

  2. SQL SERVER 数据库表同步复制 笔记

    SQL SERVER 数据库表同步复制 笔记 同步复制可运行在不同版本的SQL Server服务之间 环境模拟需要两台数据库192.168.1.1(发布),192.168.1.10(订阅) 1.在发布 ...

  3. 关于JAVA多线程的那些事__初心者

    前言 其实事情的经过也许会复杂了点,这事还得从两个月前开始说.那天,我果断不干IT支援.那天,我立志要做一个真正的程序猿.那天,我26岁11个月.那天,我开始看Android.那天,我一边叨念着有朋自 ...

  4. android 绘图之Canvas,Paint类

    Canvas,Paint 1.在android 绘图但中经常要用到Canvas和Paint类,Canvas好比是一张画布,上面已经有你想绘制图画的轮廓了,而Paint就好比是画笔,就要给Canvas进 ...

  5. DB Cache

    1 DB Cache 是以bock为单位组织的缓冲区,不同大小的BLOCK对应不同的缓冲区参数 2 DB Cache的命中率越高,访问性能就越好 3 Cache中的数据块通过散列算法实现 4 每个链上 ...

  6. Myeclipse如何改变编码方式

    Windows---->Preferences---->myeclipse Enterprise Workbench---->File and Editors----->JSP ...

  7. 【S4】使用empty()而不是判断size()是否为0

    1.二者的作用是一样的,结果也是等价的.就是判断集合是否为空. 2.二者是等价的,为什么强调使用empty,因为empty效率更高. 3.在STL中,对于一般的集合,empty和size都是常数时间. ...

  8. Spring MVC整合Velocity

    Velocity模板(VM)语言介绍 Velocity是一个基于java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由j ...

  9. 【虚拟化实战】存储设计之六latency

    在[虚拟化实战]存储设计之五IOPS中我们讲了评估存储性能的三个关键指标.也就是Throughput,IOPs和latency.以及三者之间的关系.本文深入介绍Latency过高的原因和一些建议. L ...

  10. handler looper 和 线程

     Handler的概念: 顾名思义,handler在英语中是“操作着,处理者的意思”,而官方的文档给出的概念是,handler允许你发送或者处理Message对象或者Runable对象,这两个对象都是 ...