kuangbin专题七 HDU4027 Can you answer these queries? (线段树)
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
10
1 2 3 4 5 6 7 8 9 10
5
0 1 10
1 1 10
1 1 5
0 5 8
1 4 8
Sample Output
Case #1:
19
7
6 这道题目测区间修改,但是不像加或减,开方不好处理,但是发现一个数开7 8次就变为1了,所以可以修改叶节点,然后求和就可以了。如果区间全是1,就不需要往下更新了
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#define FO freopen("in.txt","r",stdin);
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define debug(x) cout << "&&" << x << "&&" << endl;
#define lowbit(x) (x&-x)
#define mem(a,b) memset(a, b, sizeof(a));
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=;
const int inf = 0x3f3f3f3f;
ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
//head const int maxn=;
ll sum[maxn<<];
int n,q; void pushup(int rt) {
sum[rt]=sum[rt<<]+sum[rt<<|];
} void build(int rt,int L,int R) {
if(L==R) {
scanf("%lld",&sum[rt]);
return;
}
int mid=(L+R)>>;
build(rt<<,L,mid);
build(rt<<|,mid+,R);
pushup(rt);
} void updata(int rt,int L,int R,int l,int r) {
if(L==R) {
sum[rt]=sqrt(sum[rt]);
return;
}
if(L>=l&&R<=r&&sum[rt]==(R-L+)) return;
int mid=(L+R)>>;
if(l<=mid) updata(rt<<,L,mid,l,r);
if(r>mid) updata(rt<<|,mid+,R,l,r);
pushup(rt);
} ll query(int rt,int L,int R,int l,int r) {
if(L>=l&&R<=r) return sum[rt];
ll ans=;
int mid=(L+R)>>;
if(l<=mid) ans+=query(rt<<,L,mid,l,r);
if(r>mid) ans+=query(rt<<|,mid+,R,l,r);
pushup(rt);
return ans;
} int main() {
int cur=;
while(~scanf("%d",&n)) {
build(,,n);
scanf("%d",&q);
int op,ll,rr;
printf("Case #%d:\n",cur++);
while(q--) {
scanf("%d%d%d",&op,&ll,&rr);
int l=min(ll,rr),r=max(ll,rr);
if(op) printf("%lld\n",query(,,n,l,r));
else updata(,,n,l,r);
}
printf("\n");
}
}
kuangbin专题七 HDU4027 Can you answer these queries? (线段树)的更多相关文章
- kuangbin专题七 HDU1540 Tunnel Warfare (前缀后缀线段树)
During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...
- kuangbin专题七 ZOJ1610 Count the Colors (灵活线段树)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
- HDU4027 Can you answer these queries? —— 线段树 区间修改
题目链接:https://vjudge.net/problem/HDU-4027 A lot of battleships of evil are arranged in a line before ...
- HDU4027 Can you answer these queries?(线段树 单点修改)
A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use ...
- HDU4027 Can you answer these queries? 线段树
思路:http://www.cnblogs.com/gufeiyang/p/4182565.html 写写线段树 #include <stdio.h> #include <strin ...
- HDU 4027 Can you answer these queries? (线段树区间修改查询)
描述 A lot of battleships of evil are arranged in a line before the battle. Our commander decides to u ...
- 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/sho ...
- HDU-4027-Can you answer these queries?线段树+区间根号+剪枝
传送门Can you answer these queries? 题意:线段树,只是区间修改变成 把每个点的值开根号: 思路:对[X,Y]的值开根号,由于最大为 263.可以观察到最多开根号7次即为1 ...
- HDU 4027 Can you answer these queries? (线段树成段更新 && 开根操作 && 规律)
题意 : 给你N个数以及M个操作,操作分两类,第一种输入 "0 l r" 表示将区间[l,r]里的每个数都开根号.第二种输入"1 l r",表示查询区间[l,r ...
随机推荐
- CSS——盒子模型
一.盒子模型: 模型如下: 如图:盒子模型包括:margin.padding.border.content四部分. margin:外边距,透明,能够显示父级的背景颜色等.表示元素与元素之间的间隔或者说 ...
- C语言学习笔记--递归函数
1. 递归函数的思想 (1)递归是一种数学上分而自治的思想,是将大型复杂问题转化为与原问题相同但规模较小的问题进行处理的一种方法 (2)递归需要有边界条件 ①当边界条件不满足时,递归继续进行 ②当边界 ...
- java执行linux命令的工具类
package com.starfast.common.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ja ...
- Hadoop YARN: 1/1 local-dirs are bad: /var/lib/hadoop-yarn/cache/yarn/nm-local-dir; 1/1 log-dirs are bad: /var/log/hadoop-yarn/containers hdfs硬盘90% yarn unhealthy
1/1 local-dirs are bad: /var/lib/hadoop-yarn/cache/yarn/nm-local-dir; 1/1 log-dirs are bad: /var/log ...
- 如何关闭打开了多个activity的activity
专门建立一个类,内部有一个静态的linklist对象,用来记录打开的activity,如果该ACTIVITY没有被打开过,在每一个activity oncreate方法中将自己的实例加入这个list. ...
- android 自定义控件之事件
首先,继承需要扩展的VIEW,然后在里面添加一个自己的事件方法,例如, oniconclick(myinterface pinterface){ minterface = pinterface; } ...
- zabbix启动web界面提示:Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
问题 zabbix启动web界面提示: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' ( ...
- ReentrantLock简单实现2
ReentrantLock: /** * ReentrantLock测试逻辑类 */ public class MyService { private Lock lock = new Reentran ...
- linux中的管道命令
很有用的一个命令,用法如下: A | B 是把A命令的输出作为B命令的输入. 比如我想查看一下我在终端输入过的命令,可以这样: history | less
- Luogu 3625 [APIO2009]采油区域
想了很久的dp,看了一眼题解之后感觉自己被安排了. 发现从一个矩形中选择三个不相交的正方形一共只有六种取法. 那么我们可以处理出四个值: $f_{i, j}$分别表示以$(i, j)$为右下角,左下角 ...