Codeforces Round #333 (Div. 2) 602D. Lipshitz Sequence 单调栈
2016-05-20 02:13
555 查看
题意:
给n,q和n个数,q个询问
每个询问有l,r代表一个区间[l,r],问这个区间包含的所有子区间的L(h)值,L(h)代表区间内任选两个位置i
给n,q和n个数,q个询问
每个询问有l,r代表一个区间[l,r],问这个区间包含的所有子区间的L(h)值,L(h)代表区间内任选两个位置i
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<math.h> #include<queue> #include<stack> #include<string> #include<vector> #include<map> #include<set> using namespace std; #define lowbit(x) (x&(-x)) typedef long long LL; const int maxn = 100005; const int inf=(1<<28)-1; int A[maxn],stac[maxn],len,tmp[maxn]; int L[maxn],R[maxn]; int main() { int n,q; scanf("%d%d",&n,&q); for(int i=1;i<=n;++i) scanf("%d",&A[i]); while(q--) { int l,r; scanf("%d%d",&l,&r); int tot=0; for(int i=l;i<=r-1;++i) tmp[++tot]=abs(A[i+1]-A[i]); int cnt=0; for(int i=1;i<=tot;++i) { while(cnt&&tmp[stac[cnt]]<=tmp[i]) cnt--; if(cnt==0) L[i]=0; else L[i]=stac[cnt]; stac[++cnt]=i; } cnt=0; for(int i=tot;i>=1;--i) { while(cnt&&tmp[stac[cnt]]<tmp[i]) cnt--; if(cnt==0) R[i]=tot+1; else R[i]=stac[cnt]; stac[++cnt]=i; } LL ans=0; for(int i=1;i<=tot;++i) { ans+=(LL)(i-L[i])*(R[i]-i)*tmp[i]; } printf("%lld\n",ans); } return 0; }
相关文章推荐
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- codeforces 618C. Constellation
- Codeforces Round #349 (Div. 2) - C
- Codeforces Round #265 (Div. 2)
- Codeforces #310 div2 C. Case of Matryoshkas
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- codeforces 16 Problem E fish
- Codeforces Round332 部分题解
- CodeForces 603A_Alternative Thinking (DP)
- CodeForces 602B_Approximating a Constant Range_DP
- Codeforces round #247 for Div. 2
- Codeforces Round #246 (Div. 2)
- Codeforces #264(div 2)D.Gargari and Permutations
- Codeforces Round #236 (Div. 2)------A,B
- codeforces 257 div2 B