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");
}
}
最新文章
- ztreeDeptSelect 基于jquery和ztree的部门选择插件
- html表单样式, table隔行高亮, 长字符串自动换行
- HDU 1072(记忆化BFS)
- 4分钟apache自带ab压力测试工具使用: 2015.10.4
- background-clip,background-origin
- 查看Exchange邮件队列(queue)
- Java 期末复习提纲
- 【Java】JDBC连接MySQL
- 纯css3代码写下拉菜单效果
- 用户 IIS APPPOOL\DefaultAppPool 登录失败的解决方法
- 杭州电 1372 Knight Moves(全站搜索模板称号)
- OpenMP 入门教程
- Life in Changsha College-第一次冲刺
- Vxworks驱动程序的结构
- OSM数据下载地址
- ●POJ 2828 Buy Tickets
- Erlang的常驻模块与功能模块
- 并发库应用之十三 &; 并发集合类的应用
- 阿里云ssh断开处理办法
- 17 HTTP编程入门