Description

There is n pillars, their heights are (A1,A2,A3,…An. You can jump at the top of the pillars. But you will lose abs(a[j]-a[i])*abs(j-i) power when you jump from i-th pillar to j-th pillar. At first you have m power. Can you jump from s-th pillar to e-th pillar?

Input

The input consists of several test cases.
Every test case is two integer n(2<=n<200), q(1=<q<=10000).
The second line contain n integer A1, A2, A3,..An.
The next q line contain there integer s, e, m .

Output

If you can jump from s to e, with less or equal m power output “Yes”, else output “No”

Sample Input

Raw
3 3
1 2 3
1 3 2
1 2 1

Sample Output

Raw
Yes
Yes
No

Hint

     Abs(a-b) mean the absolute value of a-b.

Source

     SWUST 7th ACM/ICPC Programming Contest - 2011

///这道题开始以为用dfs搜一遍就可以了,但忽略了这道题是有q组数据,毫无意外的tle,后来在同学的提醒下想到了预处理所有区间,O(1)
查询即可,附上超时代码和ac代码,并以此为鉴。

///超时代码
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#define INF 0x7f7f7f7f
#define maxn 1005
using namespace std;
typedef long long LL;
int a[210];
bool vis[210],flag;

void dfs(int s,int e,int m)
{
    if(flag)
        return ;
    if(s==e&&m>=0)
    {
        flag=true;
        return ;
    }
    for(int i=s+1;i<=e;i++)
    {
        dfs(i,e,m-abs(a[i]-a[s])*abs(i-s));
    }
}

int main()
{
    int n,q,s,e,m;
    while(~scanf("%d%d",&n,&q))
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        while(q--)
        {
            flag=false;
            scanf("%d%d%d",&s,&e,&m);
            dfs(s,e,m);
            if(flag)
                printf("Yes\n");
            else
                printf("No\n");
        }
    }
    return 0;
}

///ac代码,动态规划
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#define INF 0x7f7f7f7f
#define N 205
using namespace std;


int main()
{
    int n,q,dp[N][N];
    while(scanf("%d%d",&n,&q)!=EOF)
    {
        int a[N];
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                dp[i][j]=abs(a[i]-a[j])*abs(i-j);///dp保存的是从i到j需要的代价
            for(int k=0;k<n;k++)
                for(int i=0;i<n;i++)
                    for(int j=0;j<n;j++)
                        if(dp[i][k]+dp[k][j]<dp[i][j])
                            dp[i][j]=dp[i][k]+dp[k][j];///最重要的状态转移,容易想到,代表中间可能经过k点,到达终点
            while(q--)
            {
                int s,e,m;
                scanf("%d%d%d",&s,&e,&m);
                if(dp[s-1][e-1]<=m)
                    printf("Yes\n");
                else
                    printf("No\n");
            }
    }
    return 0;
}