首页 > 其他分享 >线段树hdu-4027

线段树hdu-4027

时间:2023-07-19 23:24:40浏览次数:39  
标签:pr hdu int 线段 4027 each battleships line endurance

Smiling & Weeping

                 ---- 姑娘,倘若,我双手合十的愿望里有你呢

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 our secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help.
You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.
Notice that the square root operation should be rounded down to integer.   Input The input contains several test cases, terminated by EOF.
  For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
  The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 263.
  The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)
  For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.   Output For each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case. 思路:这道题目是一道中规中矩的线段树,但是有点小坑,需要牢记,以后长一下记性: if(L > R) swap(L , R);   (•́へ•́╬)不要问我怎么知道的(•́へ•́╬) 对了,对于无结束符可以使用 sacnf("%d",&n) != EOF 来判断 那么现在上代码:
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cmath>
 5 using namespace std;
 6 typedef long long ll;
 7 const int maxn = 100100;
 8 ll a[maxn] , tree[maxn<<2];
 9 int t , n;
10 #define ls(p) p<<1
11 #define rs(p) p<<1|1
12 void push_up(int p)
13 {
14     tree[p] = tree[ls(p)] + tree[rs(p)];
15 }
16 void build(int p , int pl , int pr)
17 {
18     if(pl == pr) {tree[p] = a[pl]; return ; }
19     int mid = pl+pr >> 1;
20     build(ls(p) , pl , mid);
21     build(rs(p) , mid+1 , pr);
22     push_up(p);
23 }
24 void update(int L ,int R , int p , int pl , int pr)
25 {
26     if(pl == pr) {tree[p] = sqrt(tree[p]); return ;}
27     if(tree[p] <= pr-pl+1) return ;
28     int mid = pl+pr >> 1;
29     if(L <= mid)  update(L , R , ls(p) , pl , mid);
30     if(R >  mid)  update(L , R , rs(p) , mid+1 , pr);
31     push_up(p);
32 }
33 ll query(int L , int R , int p , int pl, int pr)
34 {
35     if(L <= pl && pr <= R)
36         return tree[p];
37     int mid = pr+pl >> 1;
38     ll res = 0;
39     if(L <= mid)  res += query(L , R , ls(p) , pl , mid);
40     if(R >  mid)  res += query(L , R , rs(p) , mid+1 , pr);
41     return res;
42 }
43 int main()
44 {
45     int ind = 0;
46     while(scanf("%d",&n) != EOF)
47     {
48         int k;
49         printf("Case #%d:\n",++ind);
50         for(int i = 1; i <= n; i++)
51             scanf("%lld",&a[i]);
52         scanf("%d",&k);
53         build(1,1,n);
54         for(int i = 1; i <= k; i++)
55         {
56             int opt , L , R;
57             scanf("%d%d%d",&opt,&L,&R);
58             if(L > R)   swap(L , R);
59             if(opt == 0)
60             {
61                 update(L , R , 1 , 1 , n);
62             }
63             else
64             {
65                 printf("%lld\n",query(L , R , 1 , 1 , n));
66             }
67         }
68         printf("\n");
69     }
70 }

青山不改,绿水长流,我们下次再见ヾ( ̄▽ ̄)Bye~Bye~

标签:pr,hdu,int,线段,4027,each,battleships,line,endurance
From: https://www.cnblogs.com/smiling-weeping-zhr/p/17567051.html

相关文章

  • 在线CAD如何配合three.js绘制带线宽的线段
    前言1.在线CAD的产品经常会被集成到很多用户的网页系统内,前端开发人员只要会JavaScript,就可以对在线CAD进行集成和二次开发,今天这篇文章我们讲一下梦想CAD控件云图(H5方式)如何配合three.js绘制带线宽的线段。2.在这之前,如果还没有安装梦想CAD控件的朋友,可以查看快速入门,链接如......
  • HDU 5492 Find a path 题解
    Description在矩阵中,找一条到从\((1,1)\)到\((n,m)\)(只能向上,右走)的路径,使路径上方差最小。输出方差平方乘\(n+m-1\)的结果。对于所有数据,\(1\leqn,m,A_{i,j}\leq30\)。Solution设路径上的数为\(A_{1},A_{2},A_{3},\cdots,A_{n+m-1}\),\(\overline{A}\)为其平均数,则答......
  • 浅谈虚树优化线段树
    前言我们都知道动态开点权值线段树的空间复杂度是\(O(n\logV)\)的,但是很多题目中这样的空间是会被卡的,那我们能不能优化呢?实现看看下面这一棵树:在上图中,红色节点使我们平常会开的点。但是我们发现,其实只要维护绿色的点和红色的叶子结点。其实,绿色节点就是所有叶子结点......
  • hdu 2177 取(2堆)石子游戏 (博弈)
    题意:有两堆石子,两人轮流取石子,轮到某人时,有两种取法,要么从两堆石子中同时取出一定数量的石子,要么只从一堆中取任意数量的石子,不能不取。不能取的人判为输。普通思想:对于博弈问题,首先想到的就是sg函数。所以我们先从小到大的看局面。可以得出,对于每一种状态(x,y)x,y为石子堆。要么(x,y)本身......
  • HDU 1595 find the longest of the shortest
    题意:对于题目给定的一个图,问去掉起点到终点的最短路径上的某一条边之后起点到终点的最短距离里的最大值。思路:先计算原图的最短路径,保存最短路径。枚举最短路径每一条边,删掉该边,然后计算最短路径,保留最大的那个即可。实现:先用一个spfa求得最短路径,用一个路径数组保存路径。然后枚举......
  • hdu 1142 A Walk Through the Forest
    WA了好多次了,大概是一直没搞清题意。题意:对边<a,b>,如果a到终点的距离小于b到终点的距离,那么b就可以到a,但是a就不能到b了,所以经过这样的一种筛选的方法之后,我们要在这样的图里寻找能从起点走到终点的路径的总数。思路:先算出每一点到终点的最小距离;然后dfs记忆化搜索路径总数。#incl......
  • hdu 1150 Machine Schedule
    二部图问题:每个任务的两种模式对应一条边,那么最大的匹配数就是最多的任务不用改变模式的任务数。相当于求最小点覆盖,而最小点覆盖=最大匹配数 代码:#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>usingnamespacestd;#defineMAXN110intuN,......
  • hdu Circular Area
    计算两圆相交的面积。参考文章:http://blog.sina.com.cn/s/blog_850498e20100w6fq.html  #include<iostream>#include<cstdio>#include<cstring>#include<cmath>usingnamespacestd;#defineINF0x3fffffff#defineMAXN100001#definepiacos(-1.0)#......
  • hdu 2227 Find the nondecreasing subsequences (树状数组+dp+离散化)
    题意:给定一个长度为n(n<=100000)的整数序列,求其中的递增序列的个数。对于某些序列中的递增序列的个数是可以用dp来求解的,其状态转移方程为:dp[i]=sum(dp[j])+1,j<i&&a[j]<a[i]根据状态转移方程可以得知这样dp的时间复杂度为O(n^2),而对于题目给定的10^6的数量级来说,这样......
  • hdu Polynomial Problem
     有点杂乱无章,考虑各种情况就行了。 #include<iostream>#include<cstdio>#include<cstring>#include<cmath>usingnamespacestd;#defineINF0x3fffffff#defineMAXN100001intmain(){intn,m,x,flag,mul,ans;charstr[MAXN];whil......