题意:

      给你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)的更多相关文章

  1. SPOJ GSS3 线段树系列1

    SPOJ GSS系列真是有毒啊! 立志刷完,把线段树搞完! 来自lydrainbowcat线段树上的一道例题.(所以解法参考了lyd老师) 题意翻译 n 个数, q 次操作 操作0 x y把 Ax 修 ...

  2. SPOJ - GSS1 —— 线段树 (结点信息合并)

    题目链接:https://vjudge.net/problem/SPOJ-GSS1 GSS1 - Can you answer these queries I #tree You are given ...

  3. 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 ...

  4. SPOJ 2916 Can you answer these queries V(线段树-分类讨论)

    题目链接:http://www.spoj.com/problems/GSS5/ 题意:给出一个数列.每次查询最大子段和Sum[i,j],其中i和j满足x1<=i<=y1,x2<=j& ...

  5. GSS4 2713. Can you answer these queries IV 线段树

    GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...

  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. 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 ...

  9. SPOJ GSS1_Can you answer these queries I(线段树区间合并)

    SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...

随机推荐

  1. 内核报错kernel:NMI watchdog: BUG: soft lockup - CPU#1

    1.现象描述 系统管理员电话通知,描述为一台服务器突然无法ssh连接,登录服务器带外IP地址并进入远程控制台界面后,提示Authentication error,重启后即可正常进入系统,进入后过20分 ...

  2. 剑指 Offer 29. 顺时针打印矩阵 + 蛇形矩阵 + 模拟 + 思维题

    剑指 Offer 29. 顺时针打印矩阵 Offer_29 题目描述: 题解分析: 题目的初衷是将这道题当做一个简单题处理 这道题一开始想的太复杂了,其实可以参考迷宫广度优先搜索的过程,只不过在选定一 ...

  3. PAT-1133(Splitting A Linked List)vector的应用+链表+思维

    Splitting A Linked List PAT-1133 本题一开始我是完全按照构建链表的数据结构来模拟的,后来发现可以完全使用两个vector来解决 一个重要的性质就是位置是相对不变的. # ...

  4. jquery ajax error 函数的参数及使用

    使用jquery的ajax方法向服务器发送请求的时候,可选的回调函数有success.complete.beforeSend.error函数等.error函数常用来进行错误信息的处理,这里着重提一下e ...

  5. JS table排序

    <html lang="en"> <head> <meta charset="UTF-8"> <meta http-e ...

  6. mongoDB服务器连接不上Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException:

    一大早打开node项目就报错,终端报 UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFU ...

  7. 如何使用python把json文件转换为csv文件

    @ 目录 了解json整体格式 转换格式 提取key和value 使用pandas写入csv 了解json整体格式 这里有一段json格式的文件,存着全球陆地和海洋的每年异常气温(这里只选了一部分): ...

  8. .Net Core 2.1 升级3.1 问题整理

    随着技术的不断拓展更新,我们所使用的技术也在不断地升级优化,项目的框架也在不断地升级,本次讲解 .net core 2.1  升级到3.1所需要注意的事项: 当项目框架升级后,所有的Nuget引用也会 ...

  9. Python字典与集合

    一 字典创建.访问.添加.删除.修改.内建函数.内建方法 创建,列表不能作为键,因为键不能变?字典也不能作为键 dict1 = {} dict2 = {'name':'qq','sex':'male' ...

  10. TypeScript 入门自学笔记(一)

    码文不易,转载请带上本文链接,感谢~ https://www.cnblogs.com/echoyya/p/14542005.html 目录 码文不易,转载请带上本文链接,感谢~ https://www ...