結果

問題 No.1525 Meximum Sum
ユーザー vjudge1
提出日時 2026-03-11 09:42:04
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 13 ms / 2,000 ms
コード長 2,145 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,214 ms
コンパイル使用メモリ 334,528 KB
実行使用メモリ 11,244 KB
最終ジャッジ日時 2026-03-11 09:42:10
合計ジャッジ時間 4,489 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#define fi first
#define se second
#define FOR(i,l,r) for (int i = l; i <= r; i++)
#define FORD(i,l,r) for (int i = l; i >= r; i--)
#define el cout <<"\n"
#define int long long
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define MASK(i) ((1LL)<<(i))
#define BIT(x,i) (((x)>>(i))&(1LL))
#define Debug(a,n) for (int i = 1; i <= n; i++) cout << a[i] << " "; cout << endl
using namespace std;


typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> vii;
typedef unsigned long long ull;
typedef vector<vector<int>> vvi;
int fastMax(int x, int y) { return (((y-x)>>(32-1))&(x^y))^y; }

const int N=3e6+5;
const int base=311;
const int mod=1e9+7;
const long long INF=1e18+7;
const int d4x[4] = {-1, 0, 1, 0} , d4y[4] = {0, 1, 0, -1};
const int d8x[8] = {-1, -1, 0, 1, 1, 1, 0, -1}, d8y[8] = {0, 1, 1, 1, 0, -1, -1, -1};
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;};
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;};
void add(int &x, int y) { x += y; if (x>=mod) x-=mod;}
void sub(int &x, int y) { x -= y; if (x<0) x+=mod;}
int mul(int x, int y) {return 1LL * x * y % mod;}
int calPw(int x, int y)
{
    int ans = 1;
    while(y)
    {
        if (y&1) ans = 1LL * ans * x % mod;
        x = 1LL * x * x % mod;
        y >>= 1;
    }
    return ans;
}
///KhengmepfromLTVHSFTG
int n,a[N],pos[N];
void khengmep()
{
    int L=pos[0],R=pos[0];
    ll Ans=0;
    for(int i=1;i<n;i++){
        if(R<pos[i]&&L<pos[i]){
            Ans+=i*(pos[i]-R)*(L);
        }
        if(L>pos[i]&&R>pos[i]){
            Ans+=i*(L-pos[i])*(n-R+1);
        }
        L=min(L,pos[i]);
        R=max(R,pos[i]);
    }
    cout<<Ans+n;
}
void ip()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        pos[a[i]]=i;
    }
}
signed main()
{
//    freopen("main.inp","r",stdin);
//    freopen("main.out","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(0);
    int t=1;
    //cin>>t;
    while(t--)
    {
        ip();
        khengmep();
    }
    return 0;
}
0