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]的最大子段和( ...
随机推荐
- C#中的Collection 1
Collection定义 Collection是个关于一些变量的集合,按功能可分为Lists,Dictionaries,Sets三个大类. Lists:是一个有序的集合,支持index look up ...
- C#和JavaScript的区别
Strong and Loose Typing: 强弱比较 // C# var customer = new Customer(); //var is compiler inferred //Java ...
- 关于JAVA多线程的那些事__初心者
前言 其实事情的经过也许会复杂了点,这事还得从两个月前开始说.那天,我果断不干IT支援.那天,我立志要做一个真正的程序猿.那天,我26岁11个月.那天,我开始看Android.那天,我一边叨念着有朋自 ...
- Oracle:递归查询(树形结构数据)
今天要做一个查询功能:查询某用户所属部门,且包含该部门的所有上级部门信息.偶然找到了一个方法,特意来做个笔记.分享给和我一样的菜鸟,哈哈 查询子节点 1 select * 2 from d_arc_d ...
- css初始化代码方案
(从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期 2014-06-24) 为了消除各浏览器对css默认的设置,保持网页在各浏览器中的外观保持一致,初始化css就显得非常必要了!很多时候 ...
- cocos2d-x 2.2 资源更新AssetsManager例子代码
转自:http://www.58player.com/blog-2327-601.html // // UpgradeLayer.h // AmazeDemo // // Created by lsw ...
- myeclipse 10 载入新的项目报错Cannot return from outside a function or method
myeclipse 10 载入新的项目报错Cannot return from outside a function or method 解决方法: 方法一: window -->prefere ...
- Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索
D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...
- hdu 4499 Cannon dfs
Cannon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4499 D ...
- ECLIPSE android 布局页面文件出错故障排除Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V
在布局添加控件手动添加还是拖的添加,添加edittext后布局就不好用,其他控件好用,然后就说下面这段话 Exception raised during rendering: java.lang.Sy ...