博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
kuangbin专题七 HDU4027 Can you answer these queries? (线段树)
阅读量:5822 次
发布时间:2019-06-18

本文共 4019 字,大约阅读时间需要 13 分钟。

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.

InputThe 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 2 63.
  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.
OutputFor 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.Sample Input

101 2 3 4 5 6 7 8 9 1050 1 101 1 101 1 50 5 81 4 8

Sample Output

Case #1:1976 这道题目测区间修改,但是不像加或减,开方不好处理,但是发现一个数开7 8次就变为1了,所以可以修改叶节点,然后求和就可以了。如果区间全是1,就不需要往下更新了
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 using namespace std;15 #define FO freopen("in.txt","r",stdin);16 #define rep(i,a,n) for (int i=a;i
=a;i--)18 #define pb push_back19 #define mp make_pair20 #define all(x) (x).begin(),(x).end()21 #define fi first22 #define se second23 #define SZ(x) ((int)(x).size())24 #define debug(x) cout << "&&" << x << "&&" << endl;25 #define lowbit(x) (x&-x)26 #define mem(a,b) memset(a, b, sizeof(a));27 typedef vector
VI;28 typedef long long ll;29 typedef pair
PII;30 const ll mod=1000000007;31 const int inf = 0x3f3f3f3f;32 ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){ if(b&1)res=res*a%mod;a=a*a%mod;}return res;}33 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}34 //head35 36 const int maxn=100010;37 ll sum[maxn<<2];38 int n,q;39 40 void pushup(int rt) {41 sum[rt]=sum[rt<<1]+sum[rt<<1|1];42 }43 44 void build(int rt,int L,int R) {45 if(L==R) {46 scanf("%lld",&sum[rt]);47 return;48 }49 int mid=(L+R)>>1;50 build(rt<<1,L,mid);51 build(rt<<1|1,mid+1,R);52 pushup(rt);53 }54 55 void updata(int rt,int L,int R,int l,int r) {56 if(L==R) {57 sum[rt]=sqrt(sum[rt]);58 return;59 }60 if(L>=l&&R<=r&&sum[rt]==(R-L+1)) return;61 int mid=(L+R)>>1;62 if(l<=mid) updata(rt<<1,L,mid,l,r);63 if(r>mid) updata(rt<<1|1,mid+1,R,l,r); 64 pushup(rt);65 }66 67 ll query(int rt,int L,int R,int l,int r) {68 if(L>=l&&R<=r) return sum[rt];69 ll ans=0;70 int mid=(L+R)>>1;71 if(l<=mid) ans+=query(rt<<1,L,mid,l,r);72 if(r>mid) ans+=query(rt<<1|1,mid+1,R,l,r);73 pushup(rt);74 return ans;75 }76 77 int main() {78 int cur=1;79 while(~scanf("%d",&n)) {80 build(1,1,n);81 scanf("%d",&q);82 int op,ll,rr;83 printf("Case #%d:\n",cur++);84 while(q--) {85 scanf("%d%d%d",&op,&ll,&rr);86 int l=min(ll,rr),r=max(ll,rr);87 if(op) printf("%lld\n",query(1,1,n,l,r));88 else updata(1,1,n,l,r);89 }90 printf("\n");91 }92 }

 

 

转载于:https://www.cnblogs.com/ACMerszl/p/9865921.html

你可能感兴趣的文章
[LeetCode] House Robber II 打家劫舍之二
查看>>
基于.net开发chrome核心浏览器【四】
查看>>
手机适配浏览,所引发的一场“头脑风暴”
查看>>
程序员的价值观——经验是无价之宝(转)
查看>>
加密算法(扩展知识:Base64编码)
查看>>
sencha touch/Ext Js 6 + 自定义扩展的用法
查看>>
DELL磁盘阵列控制卡(RAID卡)MegaCli常用管理命令汇总
查看>>
线程同步总结
查看>>
谈论Hibernate级联删除——JPA根据Hibernate实现许多级联删除CascadeType.DELETE_ORPHAN
查看>>
在相同的主机上创建一个duplicate数据库
查看>>
Python 2.7.9 Demo - 三元表达式
查看>>
c++ stl algorithm: std::find, std::find_if
查看>>
在应用总结八大零售点的数据分析
查看>>
css3新增属性API
查看>>
java多线程学习(两)——创建一个线程
查看>>
linux内核包转发过程(三)NIC帧接收分析
查看>>
top对僵尸进程的处理
查看>>
Deep Learning and the Triumph of Empiricism
查看>>
关于IOS应用程序视图
查看>>
[LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
查看>>