SPOJ 2713 线段树(sqrt)
题意:
给你n个数(n <= 100000),然后两种操作,0 x y :把x-y的数全都sqrt ,1 x y:输出 x-y的和。
思路:
直接线段树更新就行了,对于当前的这个区间,如果里面只有0或者1,那么就把他mark上,以后不用在更新了,10^18 更新不了多少次就会变成0或者1的,所以时间复杂度也没多少,每次更新能返回就返回,不能返回就一定要精确到点,然后sqrt,然后查询的话就是一个简单的段询问。
#include<stdio.h>
#include<string.h>
#include<math.h>
#define lson l ,mid ,t << 1
#define rson mid + 1 ,r ,t << 1 | 1
long long sum[440000];
long long mark[440000];
void Pushup(int t)
{
sum[t] = sum[t<<1] + sum[t<<1|1];
mark[t] = (mark[t<<1] & mark[t<<1|1]);
}
void BuidTree(int l ,int r ,int t)
{
mark[t] = sum[t] = 0;
if(l == r)
{
scanf("%lld" ,&sum[t]);
if(sum[t] == 0 || sum[t] == 1)
mark[t] = 1;
return;
}
int mid = (l + r) >> 1;
BuidTree(lson);
BuidTree(rson);
Pushup(t);
return;
}
void Update(int l ,int r ,int t ,int a ,int b)
{
if(mark[t]) return;
if(l == r)
{
sum[t] = (long long)sqrt(sum[t]*1.0);
if(sum[t] == 1 || sum[t] == 0) mark[t] = 1;
return;
}
int mid = (l + r) >> 1;
if(a <= mid) Update(lson ,a ,b);
if(b > mid) Update(rson ,a ,b);
Pushup(t);
}
long long Query(int l ,int r ,int t ,int a ,int b)
{
if(a <= l && b >= r) return sum[t];
int mid = (l + r) >> 1;
long long Ans = 0;
if(a <= mid) Ans = Query(lson ,a ,b);
if(b > mid) Ans += Query(rson ,a ,b);
return Ans;
}
int main ()
{
int i ,n ,m ,cas = 1;
int key ,a ,b;
while(~scanf("%d" ,&n))
{
BuidTree(1 ,n ,1);
scanf("%d" ,&m);
printf("Case #%d:\n" ,cas ++);
while(m--)
{
scanf("%d %d %d" ,&key ,&a ,&b);
int t;
if(a > b)
{
t = a ,a = b ,b = t;
}
if(!key)
{
Update(1 ,n ,1 ,a ,b);
}
else
{
printf("%lld\n" ,Query(1 ,n ,1 ,a ,b));
}
}
printf("\n");
}
return 0;
}
SPOJ 2713 线段树(sqrt)的更多相关文章
- SPOJ GSS3 线段树系列1
SPOJ GSS系列真是有毒啊! 立志刷完,把线段树搞完! 来自lydrainbowcat线段树上的一道例题.(所以解法参考了lyd老师) 题意翻译 n 个数, q 次操作 操作0 x y把 Ax 修 ...
- SPOJ - GSS1 —— 线段树 (结点信息合并)
题目链接:https://vjudge.net/problem/SPOJ-GSS1 GSS1 - Can you answer these queries I #tree You are given ...
- SPOJ COT3 Combat on a tree(Trie树、线段树的合并)
题目链接:http://www.spoj.com/problems/COT3/ Alice and Bob are playing a game on a tree of n nodes.Each n ...
- SPOJ 2916 Can you answer these queries V(线段树-分类讨论)
题目链接:http://www.spoj.com/problems/GSS5/ 题意:给出一个数列.每次查询最大子段和Sum[i,j],其中i和j满足x1<=i<=y1,x2<=j& ...
- GSS4 2713. Can you answer these queries IV 线段树
GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...
- 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 ...
- spoj gss2 : Can you answer these queries II 离线&&线段树
1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang ...
- SPOJ GSS1_Can you answer these queries I(线段树区间合并)
SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...
随机推荐
- PAT-1148(Werewolf )思维+数学问题
Werewolf PAT-1148 题目的要点是不管n规模多大,始终只有两个狼人 说谎的是一个狼人和一个好人 紧紧抓住这两点进行实现和分析 #include <iostream> #inc ...
- 从零搭建一个IdentityServer——单页应用身份验证
上一篇文章我们介绍了Asp.net core中身份验证的相关内容,并通过下图描述了身份验证及授权的流程: 注:改流程图进行过修改,第三方用户名密码登陆后并不是直接获得code/id_token/acc ...
- three.js cannon.js物理引擎之齿轮动画
郭先生今天继续说一说cannon.js物理引擎,并用之前已经学习过的知识实现一个小动画,知识点包括ConvexPolyhedron多边形.Shape几何体.Body刚体.HingeConstraint ...
- xss和实体编码的一点小思考
首先,浏览器渲染分以下几步: 解析HTML生成DOM树. 解析CSS生成CSSOM规则树. 将DOM树与CSSOM规则树合并在一起生成渲染树. 遍历渲染树开始布局,计算每个节点的位置大小信息. 将渲染 ...
- rest framework ViewSet
ViewSets 路由选择确定要用于一个请求哪个控制器之后,控制器负责做出请求的感并产生相应的输出. - Ruby on Rails的文档 Django的REST框架允许你的逻辑一组在一个类中的相关意 ...
- [BJWC2018] Kakuro
一.题目 点此看题 二.解法 我一开始一直想不出来,直接刚这个题实在是太复杂了,因为一开始就是不合法的. 下次遇到复杂的题一定要想 调整法 ,我再不往这个方向想我吔屎 好了言归正传,我们先找一组可行的 ...
- [HEOI2014] 人人尽说江南好
[HEOI2014] 人人尽说江南好 题目大意:一个博弈游戏,地上\(n\)堆石子,每堆石子有\(1\)个,每次可以合并任意两个石子堆\(a,b\),要求\(a + b \leq m\),问先手赢还是 ...
- Ignatius and the Princess III HDU - 1028
题目传送门:https://vjudge.net/problem/HDU-1028 思路:整数拆分构造母函数的模板题 1 //#include<bits/stdc++.h> 2 #incl ...
- Java并发编程之多线程
线程 进程/线程/协程/管程 进程:操作系统会以进程为单位,分配系统资源(CPU时间片.内存等资源),是资源分配的最小单位 进程间通信(IPC): 管道(Pipe) 命名管道(FIFO) 消息队列(M ...
- ES核心概念和原理
ES:1:倒排索引 基于Document 关键词索引实现 . 根据关键词做索引 相关度 a. 数据结构 i. 包含关键词的Document List ii. 关键词在每个doc中出现的次数 词频 TF ...