GSS4 2713. Can you answer these queries IV 线段树
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 线段树的更多相关文章
- SP2713 GSS4 - Can you answer these queries IV(线段树)
传送门 解题思路 大概就是一个数很少次数的开方会开到\(1\),而\(1\)开方还是\(1\),所以维护一个和,维护一个开方标记,维护一个区间是否全部为\(1/0\)的标记.然后每次修改时先看是否有全 ...
- SPOJ2713GSS4 - Can you answer these queries IV(线段树)
题意 Sol 讲过无数次了..很显然,一个$10^12$的数开方不超过$8$次后就会变为$1$ 因此直接暴力更改即可,维护一下这段区间是否被全改为了$1$ 双倍经验:https://www.luogu ...
- SPOJ GSS1_Can you answer these queries I(线段树区间合并)
SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...
- 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 ...
- 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[ ...
- 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 ...
- 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 ...
- HDU 4027—— Can you answer these queries?——————【线段树区间开方,区间求和】
Can you answer these queries? Time Limit:2000MS Memory Limit:65768KB 64bit IO Format:%I64d & ...
- 【BZOJ2482】[Spoj1557] Can you answer these queries II 线段树
[BZOJ2482][Spoj1557] Can you answer these queries II Description 给定n个元素的序列. 给出m个询问:求l[i]~r[i]的最大子段和( ...
随机推荐
- SQLServer2005 提示 '其他会话正在使用事务的上下文'
MSDN上看了一下说是sql server 2005不支持在分布式事务处理中存在指向本地的链接服务器(环回链接服务器) 这个是官方的回答 个人认为,应该是在事务中,使用了链接服务器访问进行跨库访问引起 ...
- 【转】Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
Android-Universal-Image-Loader 原文地址:http://blog.csdn.net/vipzjyno1/article/details/23206387 这个图片异步加载 ...
- IIS7/8 HTTP Error 500.19 错误 0x80070021
IIS7.0/8.0的错误HTTP Error 500.19 - Internal Server Error ,错误代码为0x80070021,大概原因为IIS7.0的安全设定相比前版本有很大的变更. ...
- jQuery性能优化的28个建议
我一直在寻找有关jQuery性能优化方面的小窍门,能让我那臃肿的动态网页应用变得轻便些.找了很多文章后,我决定将最好最常用的一些优化性能的建议列出来.我也做了一个jQuery性能优化的简明样式表,你可 ...
- Java面试葵花宝典
面向对象的特征有哪些方面 1. 抽象:抽象就是忽略一个主题中与当前目标2. 无关的那些方面,3. 以便更充分地注意与当前目标4. 有关的方面.抽象并不5. 打算了解全部问题,而6. 只是选择其中的一 ...
- MySQL 127.0.0.1和localhost本质区别
登录方式: [root@10-4-14-168 ~]# mysql -uroot -p Enter password: 查看权限表 mysql> SELECT user,host,passwor ...
- OC基础之方法和参数的命名规范
以前学过C/C++/Java/C#语言的童鞋可能刚开始对于OC的方法和参数的命名规范大为不爽 举例来说,如下一个OC方法: - (void)tableView:(UITableView *)table ...
- Codeforces Round #338 (Div. 2) A. Bulbs 水题
A. Bulbs 题目连接: http://www.codeforces.com/contest/615/problem/A Description Vasya wants to turn on Ch ...
- C#取真实IP地址及分析
说一哈,我也是转来的,不是想骗PV,方便自己查而已! 目前网上流行的所谓"取真实IP地址"的方法,都有bug,没有考虑到多层透明代理的情况. 多数代码类似: string IpAd ...
- 近期最久未使用页面淘汰算法———LRU算法(java实现)
请珍惜小编劳动成果,该文章为小编原创,转载请注明出处. LRU算法,即Last Recently Used ---选择最后一次訪问时间距离当前时间最长的一页并淘汰之--即淘汰最长时间没有使用的页 依照 ...