poj 3264 & poj 3468(线段树)
poj 3264
Sample Input
6 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6
3
0
求任一区间的最大值和最小值的差
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
#define N 50005
#define mod 258280327
#define MIN 0
#define MAX 1000001
struct node
{
int val,maxn,minx;
int Left,Right;
} pnode[4*N];
int a[N];
int tmax,tmin;
void build(int i,int l,int r)
{
pnode[i].Left = l;
pnode[i].Right = r;
pnode[i].maxn = MIN;
pnode[i].minx = MAX;
if(l == r)
return;
build(i*2,l ,(l+r)/2);
build(i*2+1, (l+r)/2 + 1,r);
} void insert(int i,int index,int va)
{
if(pnode[index].Left == pnode[index].Right)
{
pnode[index].maxn = pnode[index].minx = va;
return ;
}
pnode[index].maxn = max(pnode[index].maxn,va);
pnode[index].minx = min(pnode[index].minx,va);
int mid = (pnode[index].Left+pnode[index].Right)/2;
if (mid >= i)
insert(i,index*2,va);
else
insert(i,index*2+1, va);
} void query(int u,int l,int r,int a,int b)
{
// if(pnode[u].minx >= tmin && pnode[u].maxn < tmax)
// return;
if(a == l && b == r)
{
if(tmax < pnode[u].maxn)
tmax = pnode[u].maxn;
if(tmin > pnode[u].minx)
tmin = pnode[u].minx;
return ;
} int mid = (l + r)>>1;
if (mid >= b)
query(u*2,l, mid, a, b);
else if (mid < a)
query(u*2+1,mid+1, r, a, b);
else
{
query(u*2,l, mid, a, mid);
query( u*2+1,mid+1, r, mid+1, b);
}
} int main()
{
int n,m;
scanf("%d%d",&n,&m);
build(1,1,n);
for(int i = 1; i <= n; i++)
{
scanf("%d",&a[i]);
insert(i,1,a[i]);
} for(int i = 1; i <= m; i++)
{
int x,y;
tmax = -MAX;
tmin = MAX;
scanf("%d%d",&x,&y);
query(1,1,n,x,y);
printf("%d\n",tmax-tmin);
} return 0;
}
poj 3468
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
①对区间i - j 的数全加上c ; ②求区间的和
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define N 100005
#define mod 258280327
#define MIN 0
#define MAX 1000001 struct node
{
ll val,els;
int Left,Right;
} pnode[4*N]; int a[N];
int tmax,tmin;
void build(int i,int l,int r)
{
pnode[i].Left = l;
pnode[i].Right = r;
pnode[i].val = 0;
pnode[i].els = 0;
if(l == r)
return;
build(i*2,l ,(l+r)/2);
build(i*2+1, (l+r)/2 + 1,r);
} void insert(int i,int index,int va)
{
if(pnode[index].Left == pnode[index].Right)
{
pnode[index].val = va;
return ;
}
pnode[index].val+=va;
int mid = (pnode[index].Left+pnode[index].Right)/2;
if (mid >= i)
insert(i,index*2,va);
else
insert(i,index*2+1, va);
} void add(int u,int l,int r,ll c,int a,int b)
{
if(a == l && b == r)
{
pnode[u].els += c;
return ;
}
pnode[u].val += (b-a+1)*c; //让大于a,b的部分加上
if(l == r)
return ;
int mid = (l + r)>>1;
if (mid >= b)
add(u*2,l, mid, c, a, b);
else if (mid < a)
add(u*2+1,mid+1, r, c, a, b);
else
{
add(u*2,l, mid , c,a, mid);
add(u*2+1,mid+1, r,c, mid+1, b);
}
} long long query(int u,int l,int r,int a,int b)
{
if(a == l && b == r)
{
return pnode[u].val + (pnode[u].Right - pnode[u].Left + 1)*pnode[u].els;
}
pnode[u].val += (pnode[u].Right - pnode[u].Left + 1)*pnode[u].els;
//当取了a,b的附加值后,将其附加值往下放
add(u*2,pnode[u].Left,(pnode[u].Left + pnode[u].Right)/2,pnode[u].els,pnode[u].Left,(pnode[u].Left + pnode[u].Right)/2);
add(u*2+1,(pnode[u].Left+pnode[u].Right)/2+1,pnode[u].Right,pnode[u].els,(pnode[u].Left+pnode[u].Right)/2+1,pnode[u].Right);
pnode[u].els = 0;
int mid = (l + r)>>1;
if (mid >= b)
return query(u*2,l, mid, a, b);
else if (mid < a)
return query(u*2+1,mid+1, r, a, b);
else
{
return query(u*2,l, mid, a, mid)+query( u*2+1,mid+1, r, mid+1, b);
}
} int main()
{
int n,m,l,r,c;
char ch;
while(scanf("%d%d",&n,&m)!= EOF)
{
build(1,1,n);
for(int i = 1; i <= n; i++)
{
scanf("%d",&a[i]);
insert(i,1,a[i]);
} for(int i = 1; i <= m; i++)
{
getchar();
ch = getchar();
if(ch == 'Q')
{
scanf("%d%d",&l,&r);
printf("%I64d\n",query(1,1,n,l,r));
}
if(ch == 'C')
{
scanf("%d%d%d",&l,&r,&c);
add(1,1,n,c,l,r);
}
}
}
return 0;
}
poj 3264 & poj 3468(线段树)的更多相关文章
- poj 3264 Balanced Lineup(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- POJ 3264 Balanced Lineup 线段树RMQ
http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...
- [POJ] 3264 Balanced Lineup [线段树]
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34306 Accepted: 16137 ...
- poj 3264(RMQ或者线段树)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 42929 Accepted: 20184 ...
- POJ 3264 Balanced Lineup 线段树 第三题
Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...
- POJ - 3264 Balanced Lineup 线段树解RMQ
这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...
- POJ 3264 Balanced Lineup (线段树)
Balanced Lineup For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the s ...
- poj 3468(线段树)
http://poj.org/problem?id=3468 题意:给n个数字,从A1 …………An m次命令,Q是查询,查询a到b的区间和,c是更新,从a到b每个值都增加x.思路:这是一个很明显的线 ...
- hdu 1698+poj 3468 (线段树 区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 这个题意翻译起来有点猥琐啊,还是和谐一点吧 和涂颜色差不多,区间初始都为1,然后操作都是将x到y改为z,注 ...
随机推荐
- 20145237 《Java程序设计》第三周学习总结
20145237 <Java程序设计>第3周学习总结 教材学习内容总结 第四章主要讲了Java基本类型中的类类型,如何定义类.构造函数.使用标准类.基本类型打包器.数组复制.字符串等内容查 ...
- scrapy crawl 源码修改 爬虫多开
import os from scrapy.commands import ScrapyCommand from scrapy.utils.conf import arglist_to_dict fr ...
- socket_sever实现多客户端并发
#!/usr/bin/env python # -*- coding:utf-8 -*- import socketserver class mysever(socketserver.BaseRequ ...
- python 操作Memcached
启动Memcached memcached -d -m 10 -u root -l 10.211.55.4 -p 12000 -c 256 -P /tmp/memcached.pid 参数说明: -d ...
- Flask 学习 十四 测试
获取代码覆盖报告 安装代码覆盖工具 pip install coverage manage.py 覆盖检测 COV = None if os.environ.get('FLASK_COVERAGE') ...
- bzoj千题计划219:bzoj1568: [JSOI2008]Blue Mary开公司
http://www.lydsy.com/JudgeOnline/problem.php?id=1568 写多了就觉着水了... #include<cstdio> #include< ...
- PHP环境手动搭建wamp-----Apache+MySQL+PHP
首先下载分别下载Apache+MySQL+PHP. 然后分别解压到文件夹中. 1.安装Apache 1)检查80端口是否占用 说明:apache软件占用80软件,在计算机中一个端口只能被一个软件占用 ...
- plsql启动提示监听服务无法连接
话说现在用的oracle少了,本人菜鸟一个,但是我真心的没有感觉到它用的少了,今天入了一个新项目,数据库使用的还是oracle,经理二话不说的给了一些东西,说了让一句你把环境啥地 配置一下,然后走人了 ...
- SpringCloud的应用发布(四)vmvare+linux,网关代理
一.配置方式 1.代理同一个Eureka中注册的服务 2.代理url 二.访问方式:get - list 1.直接访问应用 2.代理访问应用
- OAuth2.0学习(1-7)授权方式4-客户端模式(Client Credentials Grant)
授权方式4-客户端模式(Client Credentials Grant) 客户端模式(Client Credentials Grant)指客户端以自己的名义,而不是以用户的名义,向"服务提 ...