XTU 1205 Range
还是五月湘潭赛的题目,当时就是因为我坑。。。连个银牌都没拿到,擦。
这个题目枚举区间是不可能的,明显是要考虑每个数对全局的影响,即找到每个数最左和最右能满足是最大的位置 以及 最小的时候,相乘即为该数字影响的区间总数。当时想到的是用线段树,建树的时候求出最大和最小值,然后在每个数往里面搜索,比赛的时候敲挫了,那个时候真的线段树写的很挫,而且没考虑过一个问题,就是相同的时候怎么办,按刚刚的算法,会算重复的,所以一个好的方法是如果有相同的,往左搜的时候搜到等于该数值的时候停止,往右搜的时候搜到大于该数值的时候停止(求最大的时候),求最小的时候也是一样的处理
然后搜左边的时候优先走右子树,右子树没有 再找左子树。。同理右边先搜左孩子,没有再搜右孩子,注意些细节以及可以剪剪枝。
线段树写这个题目其实很费力,有种更好的方法用桟来做,栈在求最大最小的时候往往很给力,像这个题目,我比如在求左边最大的时候,我当前数,如果碰到栈顶数比它大于等于的,就直接停止了,入桟,否则就一直pop下去,直到遇到比它大于等于的或者桟空,这个时候得到了左边区间最大,并且把当前值放进去,因为我只保留左边区间离下个数最近的最大的数即可,如果连这个数都不能阻挡下一个数,那之前被pop掉的肯定也阻挡不了,所以扫一遍即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define LL __int64
using namespace std;
const int N = ;
int dmin[N<<],dmax[N<<];
int A[N];
int n;
void build(int rt,int l,int r)
{
if (l>=r){
dmin[rt]=A[l];
dmax[rt]=A[l];
return;
}
int mid=(l+r)>>;
build(lson);
build(rson);
dmax[rt]=max(dmax[rt<<],dmax[rt<<|]);
dmin[rt]=min(dmin[rt<<],dmin[rt<<|]);
}
int query1(int L,int R,int val,int rt,int l,int r)
{
if (L>R) return R;
if (l>=r){
if(l>=L &&l<=R && A[l]>=val){
return l;
}
else return ;
}
int mid=(l+r)>>;
if (R<=mid){
if (dmax[rt<<]>=val)
return query1(L,R,val,lson);
else return ;
}
int ret=;
if (dmax[rt<<|]>=val){
ret=query1(L,R,val,rson);
}
if (ret>) return ret;
if (dmax[rt<<]>=val)
return query1(L,R,val,lson);
else return ;
}
int query2(int L,int R,int val,int rt,int l,int r)
{
if (L>R) return L;
if (l>=r){
if (l>=L && l<=R && A[l]>=val){
return l;
}
else return n+;
}
int mid=(l+r)>>;
if (L>mid){
if (dmax[rt<<|]>=val)
return query2(L,R,val,rson);
else return n+;
}
int ret=n+;
if (dmax[rt<<]>=val){
ret=query2(L,R,val,lson);
}
if (ret<n+) return ret;
if (dmax[rt<<|]>=val)
return query2(L,R,val,rson);
else return n+;
}
int query3(int L,int R,int val,int rt,int l,int r)
{
if (L>R) return R;
if (l>=r){
if (L<=l && l<=R && A[l]<=val){
return l;
}
else return ;
}
int mid=(l+r)>>; if (R<=mid){
if (dmin[rt<<]<=val){
return query3(L,R,val,lson);
}
else return ;
}
int ret=;
if (dmin[rt<<|]<=val) ret=query3(L,R,val,rson);
if (ret>) return ret;
if (dmin[rt<<]<=val)
return query3(L,R,val,lson);
else return ;
}
int query4(int L,int R,int val,int rt,int l,int r)
{
if (L>R) return L;
if (l>=r){
if (L<=l && r<=R && A[l]<=val){
return l;
}
else return n+;
}
int mid=(l+r)>>;
if (L>mid){
if (dmin[rt<<|]<=val){
return query4(L,R,val,rson);
}
else return n+;
}
int ret=n+;
if (dmin[rt<<]<=val){
ret=query4(L,R,val,lson);
}
if (ret<n+) return ret;
else
if (dmin[rt<<|]<=val) return query4(L,R,val,rson);
return n+;
}
int main()
{
int t;
int kase=;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
for (int i=;i<=n;i++) scanf("%d",&A[i]);
build(,,n);
LL sum=;
for (int i=;i<=n;i++){
int l1=query1(,i-,A[i],,,n);
int l2=query2(i+,n,A[i]+,,,n);
int l3=query3(,i-,A[i],,,n);
int l4=query4(i+,n,A[i]-,,,n);
//cout<<"Test "<<i<<endl;
// cout<<l1<<" "<<l2<<" "<<l3<<" "<<l4<<endl;
sum+=(LL)(i-l1)*(LL)(l2-i)*(LL)A[i];
sum-=(LL)(i-l3)*(LL)(l4-i)*(LL)A[i];
}
sum+=(LL)n*(n+)/;
printf("Case %d: %I64d\n",++kase,sum);
}
return ;
}
XTU 1205 Range的更多相关文章
- XTUOJ 1205 Range
Range Time Limit : 1000 MS Memory Limit : 65536 KB Problem Description For an array, the range funct ...
- SQL Server 合并复制遇到identity range check报错的解决
最近帮一个客户搭建跨洋的合并复制,由于数据库非常大,跨洋网络条件不稳定,因此只能通过备份初始化,在初始化完成后向海外订阅端插入数据时发现报出如下错误: Msg 548, Level 16, S ...
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- [LeetCode] Range Addition 范围相加
Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...
- [LeetCode] Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
随机推荐
- 四 SpringMVC与页面之间的参数传递&高级参数的绑定&日期类型的转换
参数传递: 1 原生方式:使用Servlet API , request.getParameter("id"); 2 直接将请求参数作为Controller中的形参: publ ...
- Angular4之时间管道
{{时间戳 |date:“yyyy/MM/dd HH:mm:ss”}} “YYYY/MM/DD”不可
- Day3-G - Task HDU4864
Today the company has m tasks to complete. The ith task need xi minutes to complete. Meanwhile, this ...
- 「NOIP2014」飞扬的小鸟
传送门 Luogu 解题思路 考虑 \(\text{DP}\) 设 \(dp[i][j]\) 表示飞到 \((i, j)\) 这个点的最小触屏次数. 转移其实比较显然,但问题是每次上升时都可以点很多次 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 图片:响应式图片
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 学习Linux系统永远都不晚
作为一名机械专业毕业的学生,两年的工作经历实实在在地教会了我如何认清现实,让当初那个对机械行业无比憧憬的少年明白了自己选择的路有多艰难.由于我的父母都是工人,所以我比其他同龄人能更早地接触到工业的魅力 ...
- linux中df和du查看磁盘大小不一致解决方法
挂了一块50G到/data目录下# df -h Filesystem Size Used Avail Use% Mounted on /dev/xvdb1 50G 46G 1.2G 98% /dat ...
- JS+ES6 - 向数组的开头添加一个或更多元素
- 我用Python帮朋友做了张猪肉数据分析图,结果。。。
却发现他是这么拿我当兄弟的 事情的经过是这样的: 我开开心心的去一家烧饼店吃饭 . 抬头一看,二师兄又涨价了 叹了口气,再这么下去真的要吃不起夹肉的烧饼了 点了两个烧饼一碗馄饨 快吃完的时候, ...
- https://blog.csdn.net/yyoinge/article/details/81557604
https://blog.csdn.net/yyoinge/article/details/81557604 http://www.mamicode.com/info-detail-2346464.h ...