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. Android Studio Push rejected: Push to origin/Alpha1.0 was rejected

    android studio git 右键项目, git pull 刷新选择Alpha1.0同步后,再commit and push

  2. Thread message loop for a thread with a hidden window? Make AllocateHwnd safe

    Thread message loop for a thread with a hidden window? I have a Delphi 6 application that has a thre ...

  3. TMS3705A PCF7991AT 线路图

  4. 微软停服 XP系统到底伤害了谁?

    http://majihua.baijia.baidu.com/article/10386 微软现在成了招人恨的角色,因为其史上最成功的操作系统WINDOWS XP在4月8日就将停止服务,而社会上对X ...

  5. 分享:Android中利用机器码注册机制防止破解(转)

    转自:http://blog.csdn.net/huzgd/article/details/6684094 最近做一个Android应用时遇到这个问题,客户要求功能必须注册才能使用,而程序本身又不是联 ...

  6. 对cocos2d 之autorelease\ratain\release的理解

    前言: 三种情况,引出问题     new出来的对象需要释放,而释放时,如果有其他人引用了这个对象,再次使用这个对象时,则会导致无效指针报错.     于是有了引用计数的施放管理机制.       对 ...

  7. MySQL auto_increment实现

    http://www.cnblogs.com/xpchild/p/3825309.html 运维的时候,经常遇到auto_increment的疑惑: 机器异常crash,重启后id回退的问题 性能考虑 ...

  8. Fedora 23如何安装LAMP服务器

    LAMP 是开源系统上 Web 服务器的梦幻组合.LAMP 是 Linux. Apache HTTP 服务. MySQL/MariaDB 数据库和 PHP. Perl 或 Python 的简称. 下面 ...

  9. windows下搭建svn服务端、客户端

    1.安装SVN服务器subversion以及客户端TortoiseSVN,在网上下载windows版的subversion,TortoiseSVN并安装,比如我的服务端安装在了D:\Program F ...

  10. spark1.2.0安装

    standalone 安装SCALA 下载.解压.加入环境变量 安装spark1.2.0 下载.解压.加入环境变量 tar zxvf spark--bin-.tgz export SPARK_HOME ...