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 ...
随机推荐
- UML设计九种图例
一.作为一种建模语言,UML的定义包括UML语义和UML表示法两个部分. UML语义:描述基于UML的精确元模型定义. UML表示法:定义UML符号的表示法,为开发者或开发工具使用这些图形符号和文本语 ...
- linux命令-mount挂载umount卸载
格式化完成之后想写数据 要先挂载 //////////////////////////////////////////////////////////////////// [root@wangshao ...
- C# 连接Mysql 字符串
Database=XXX;Data Source=XXX;User Id=XXX;Password=XXX;pooling=false;CharSet=utf8;port=3306
- [poj2318]TOYS(直线与点的位置关系)
解题关键:计算几何入门题,通过叉积判断. 两个向量的关系: P*Q>0,Q在P的逆时针方向: P*Q<0,Q在P的顺时针方向: P*Q==0,Q与P共线. 实际就是用右手定则判断的. #i ...
- ngx-bootstrap使用01 安装ngx-bootstrap和bootstrap及其使用、外部样式引入
1 版本说明 2 新建一个angular项目 ng new 项目名 --stayle=scss 代码解释:创建一个样式文件格式为SCSS的angular项目 技巧01:由于我angular-cli的版 ...
- SpringBoot05 数据操作01 -> JPA的基本使用、基本使用02
前提: 创建一个springboot项目 创建一个名为springboottest的MySQL数据库 1 jar包准备 jpa的jar包 mysql驱动的jar包 druid数据库连接池的jar包 l ...
- 大O表示法总结
大O符号用于计算机科学来描述算法的性能或复杂性.Big O特别描述了最坏的情况,可以用算法来描述所需的执行时间或使用的空间(例如在内存或磁盘上). 任何读过Programming Pearls(编程珠 ...
- python3-while与if
# Auther: Aaron Fan age_of_oldboy = 56 #定义一个while循环的起始判断值countcount = 0#当count小于3的情况下一直执行while循环whil ...
- 使用database control配置数据库时 要求在当前oracle主目录中配置监听程序
1:配置本地的环境变量 打开cmd命令界面 C:\Users\gechong>lsnrctl start 这时候报适配器错误 2.在cmd中输入 tnslsnr命令
- C++面试笔记--继承和接口
整个C++程序设计全面围绕面向对象的方式进行.类的继承特性是C++的一个非常重要的机制.继承特性可以使一个新类获得其父类的操作和数据结构,程序员只需在新类中增加原有类没有的成分. 在面试过程中,各大企 ...