(线段树 区间查询更新) Can you answer these queries? -- hdu--4027

链接:

http://acm.hdu.edu.cn/showproblem.php?pid=4027

分析:因为这个操作是把一个数变成平方根,所以显得略棘手,不过如果仔细演算的话会发现一个2^64数的平方根开8次也就变成了 1,所以也更新不了多少次,所以可以每次更新到底。、

注意:给的X Y大小未知,会出现X > Y的情况

代码:

  1 #include<cstdio>
  2 #include<cmath>
  3 #include<cstring>
  4 #include<cstdlib>
  5 #include<algorithm>
  6 #include<iostream>
  7 #include<cmath>
  8 const int N = 100005;
  9 using namespace std;
 10
 11 #define Lson r<<1
 12 #define Rson r<<1|1
 13 #define mid  a[r].Mid()
 14
 15 struct node
 16 {
 17     int L, R;
 18     long long sum;
 19     int Mid()
 20     {
 21         return (L+R)>>1;
 22     }
 23     int len()
 24     {
 25         return (R-L+1);
 26     };
 27 } a[N<<2];
 28
 29 void BuildTree(int r, int L, int R)
 30 {
 31     a[r].L=L, a[r].R=R;
 32
 33     if(L==R)
 34     {
 35         scanf("%lld", &a[r].sum);
 36         return ;
 37     }
 38
 39     BuildTree(Lson, L, mid);
 40     BuildTree(Rson, mid+1, R);
 41
 42     a[r].sum = a[Lson].sum + a[Rson].sum;
 43 }
 44
 45 void Oper(int r, int L, int R)
 46 {
 47     if(a[r].len() == a[r].sum)
 48         return ;
 49
 50     if(a[r].L==a[r].R)
 51     {
 52         a[r].sum = (long long)sqrt(a[r].sum*1.0);
 53         return ;
 54     }
 55
 56     if(R<=mid)
 57         Oper(Lson, L, R);
 58     else if(L>mid)
 59         Oper(Rson, L, R);
 60     else
 61     {
 62         Oper(Lson, L, mid);
 63         Oper(Rson, mid+1, R);
 64     }
 65
 66     a[r].sum = a[Lson].sum + a[Rson].sum;
 67 }
 68
 69 long long  Query(int r, int L, int R)
 70 {
 71     if(a[r].L==L && a[r].R==R)
 72         return a[r].sum;
 73
 74     if(R<=mid)
 75         return  Query(Lson, L, R);
 76     else if(L>mid)
 77         return  Query(Rson, L, R);
 78     else
 79     {
 80         return Query(Lson, L, mid) + Query(Rson, mid+1, R);
 81     }
 82 }
 83
 84
 85 int main()
 86 {
 87     int n, m, t=1;
 88     while(scanf("%d", &n)!=EOF)
 89     {
 90
 91         BuildTree(1, 1, n);
 92
 93         int L, R, e;
 94
 95         scanf("%d", &m);
 96
 97         printf("Case #%d:\n", t++);
 98         while(m--)
 99         {
100             scanf("%d%d%d", &e, &L, &R);
101
102             if(L>R)
103                 swap(L, R);
104             if(e==0)
105                 Oper(1, L, R);
106             else
107             {
108                 printf("%lld\n", Query(1, L, R));
109             }
110         }
111
112         printf("\n");
113     }
114     return 0;
115 }
时间: 2024-08-07 04:31:30

(线段树 区间查询更新) Can you answer these queries? -- hdu--4027的相关文章

V - Can you answer these queries? HDU - 4027 线段树

V - Can you answer these queries? HDU - 4027 这个题目开始没什么思路,因为不知道要怎么去区间更新这个开根号. 然后稍微看了一下题解,因为每一个数开根号最多开十几次就变成1了,所以就直接单点更新,但是这个可以剪枝. 如果碰到区间长度和区间大小相同就可以不用更新了. 我也是无语了,想到刚刚的做法之后很好写了, 不过要注意以下x,y 大小可能x>y, 这个bug如果不是之前看题解的时候瞄到了,我可能找不出来了. 因为我写对拍我肯定会保证x<y 的,还是就是

Can you answer these queries? HDU - 4027

Can you answer these querites? HDU - 4027 普通的线段树题,但是有一个问题是,区间更新时,因为必须更新每个点,才能更新区间,那么线段树更新就很慢了,无法使用lazy数组.有一个小技巧是当区间和等于区间长度时,那么说明已经到最好的情况了,不用再修改了.这一步简化后就可以过了 如果写线段树可以像喝水一样自然就好了 1 #include <iostream> 2 #include <cstring> 3 #include <vector>

H - Can you answer these queries? HDU 4027 (线段树+延迟标记+开根号的速度)

H - Can you answer these queries? Time Limit:2000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4027 Description A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our

线段树 SP1716 GSS3 - Can you answer these queries III

SP1716 GSS3 - Can you answer these queries III 题意翻译 n 个数,q 次操作 操作0 x y把A_xAx 修改为yy 操作1 l r询问区间[l, r] 的最大子段和 依旧是维护最大子段和,还是再敲一遍比较好. code: #include<iostream> #include<cstdio> #define ls(o) o<<1 #define rs(o) o<<1|1 using namespace std

Can you answer these queries? HDU - 4027 有点坑

#include<iostream> #include<cstring> #include<cstdio> #include<math.h> #include<algorithm> using namespace std; typedef long long ll; const int N=1e5+10; ll a[N]; int n,m; int op,l,r; struct node { ll l,r; ll sum; }tr[N*4]; v

hdu----(5023)A Corrupt Mayor&#39;s Performance Art(线段树区间更新以及区间查询)

A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)Total Submission(s): 33    Accepted Submission(s): 11 Problem Description Corrupt governors always find ways to get dirty money. Pa

HDU 3874 Necklace (线段树单点更新+区间查询+离线操作)

Problem Description Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ball has a beautiful value. The balls with the same beautiful value look the same, so if two or more balls have the same beautiful value, we just count

线段树 + 区间更新 + 模板 ---- poj 3468

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 59798   Accepted: 18237 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of

HDU3074_Multiply game(线段树/单点更新)

解题报告 题意: 略 思路: 单点更新,区间乘积. #include <cstdio> #include <iostream> #include <cstring> #define LL long long using namespace std; LL mul[501000]; void update(int root,int l,int r,int p,int v) { int mid=(l+r)/2; if(l==r)mul[root]=v; else { if(