D - Can you answer these queries? (线段树+剪枝)

题目链接:https://vjudge.net/contest/332656#problem/D

思路:因为根号运算n衰减的很快,所以在极少数的操作内它就会变成1,所以当整个区间内的值都变成1时直接返回,反之暴力更新叶子结点就好

  1 #include <math.h>
  2 #include <stdio.h>
  3 #include <iostream>
  4 #include <algorithm>
  5 #include <string>
  6 #include <string.h>
  7 #include <vector>
  8 #include <map>
  9 #include <random>
 10
 11
 12 #define LL long long
 13
 14 const int maxn = 1e5 + 10;
 15
 16
 17 LL arr[maxn];
 18 struct segment_tree {
 19     int l,r;
 20     LL val;
 21 }tree[maxn<<2];
 22
 23
 24 void pushup(int nod ) {
 25     tree[nod].val = (tree[nod<<1].val + tree[(nod<<1)+1].val);
 26 }
 27
 28
 29 void build(int l,int r,int nod) {
 30     tree[nod].l = l;
 31     tree[nod].r = r;
 32     if (l == r) {
 33         tree[nod].val = arr[l];
 34         return ;
 35     }
 36     int mid = (l + r) >> 1;
 37     build(l,mid,nod<<1);
 38     build(mid+1,r,(nod<<1)+1);
 39     pushup(nod);
 40 }
 41
 42 void modify(int x,int y,int k=1) {
 43     int l = tree[k].l,r = tree[k].r;
 44     if (l == r) {
 45         tree[k].val = std::sqrt(tree[k].val);
 46         return ;
 47     }
 48     if (x <= l && y >= r && tree[k].val == r-l+1)
 49         return ;
 50     int mid = (l + r ) >> 1;
 51     if (x <= mid)
 52         modify(x,y,k<<1);
 53     if (y > mid) {
 54         modify(x,y,(k<<1)+1);
 55     }
 56     pushup(k);
 57 }
 58
 59 LL query(int x,int y,int k=1) {
 60     int l = tree[k].l,r = tree[k].r;
 61     if (x <= l && y >= r) {
 62         return tree[k].val;
 63     }
 64     int mid = (l + r) >> 1;
 65     LL sum = 0;
 66     if (x <= mid)
 67         sum += query(x,y,k<<1);
 68     if (y > mid) {
 69         sum += query(x,y,(k<<1)+1);
 70     }
 71     return sum;
 72 }
 73
 74 int main() {
 75     int n;
 76     int t = 1;
 77     while (~scanf("%d",&n)) {
 78         for (int i=1;i<=n;i++) {
 79             scanf("%lld",&arr[i]);
 80         }
 81         build(1,n,1);
 82         int m;
 83         scanf("%d",&m);
 84         printf("Case #%d:\n",t++);
 85         int x,y,z;
 86         while (m--) {
 87             scanf("%d%d%d",&z,&x,&y);
 88             if (x > y)
 89                 std::swap(x,y);
 90             if (!z) {
 91                 modify(x,y);
 92             }
 93             else {
 94                 printf("%lld\n",query(x,y));
 95             }
 96         }
 97         printf("\n");
 98     }
 99     return 0;
100 }

原文地址:https://www.cnblogs.com/-Ackerman/p/11637854.html

时间: 2024-11-08 21:44:22

D - Can you answer these queries? (线段树+剪枝)的相关文章

HDU4027 Can you answer these queries 线段树区间求和+剪枝

给了你n,然后n个数字在一个数组中,接下来m个询问,每个询问三个数字 t,x,y,若t==0,那么修改区间[x,y]的每一个值,变为原来每个位置上的数 开根号取整,若t==1,那么对区间[x,y]求和 由于n,m,很大,所以树状数组铁定超时,若直接用线段树来做区间修改,那么也是超时,这类题目没别的方法了,静心剪枝,发现题目给的数据范围为2^63,有没有发现,2^63开根号 绝对不需要开10次,就能到1,到1以后就不需要再开了,意思就是若有某个区间[x,y]每一个点的值都为1时,这一段区间事实上是

HDU 4027 Can you answer these queries? (线段树+区间点修改)

题意:给你 n 个数,m个询问(c,x,y) c==0 把x,y区间的值变为原来的平方根(向下取整) c==1 计算x,y区间的和. 利用1的开方永远为1剪枝.. #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> //#include<map> #include<cmath> #include<iostream> #include

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

题目 线段树 简单题意: 区间(单点?)更新,区间求和 更新是区间内的数开根号并向下取整 这道题不用延迟操作 //注意: //1:查询时的区间端点可能前面的比后面的大: //2:优化:因为每次更新都是开平方,同一个数更新有限次数就一直是1了,所以可以这样优化 #include <stdio.h> #include<math.h> #define N 100010 #define LL __int64 #define lson l,m,rt<<1 #define rson

HDU 4027 Can you answer these queries? 线段树裸题

题意: 给定2个操作 0.把区间的每个数sqrt 2.求和 因为每个数的sqrt次数很少,所以直接更新到底,用个标记表示是否更新完全(即区间内的数字只有0,1就不用再更新了) #include<stdio.h> #include<iostream> #include<algorithm> #include<vector> #include<cmath> #include<queue> #include<set> #incl

hdu4027-Can you answer these queries?(线段树)

Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 8330    Accepted Submission(s): 1904 Problem Description A lot of battleships of evil are arranged in a line before

HDU 4027 Can you answer these queries?(线段树)

Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 9216    Accepted Submission(s): 2106 Problem Description A lot of battleships of evil are arranged in a line before

hdu 4027 Can you answer these queries? 线段树区间开根号,区间求和

Can you answer these queries? Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5195 Description A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapo

HDU-Can you answer these queries? (线段树+区间修改)

Problem Description A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of ou

HDU4027Can you answer these queries?(线段树区间和,坑)

Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 8674 Accepted Submission(s): 1971 Problem Description A lot of battleships of evil are arranged in a line before the b