Submission #905113
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
#ifdef SG
#include <debug.h>
#else
#define show(...)
#define debug(...)
#define deepen(...)
#define timer(...)
#endif
#define ARG4(_1,_2,_3,_4,...) _4
#define forn3(i,l,r) for (int i = int(l); i < int(r); ++i)
#define forn2(i,n) forn3 (i, 0, n)
#define forn(...) ARG4(__VA_ARGS__, forn3, forn2) (__VA_ARGS__)
#define ford3(i,l,r) for (int i = int(r) - 1; i >= int(l); --i)
#define ford2(i,n) ford3 (i, 0, n)
#define ford(...) ARG4(__VA_ARGS__, ford3, ford2) (__VA_ARGS__)
#define ve vector
#define pa pair
#define tu tuple
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define fs first
#define sc second
#define all(a) (a).begin(), (a).end()
#define sz(a) ((int)(a).size())
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef unsigned char uc;
typedef pa<int, int> pii;
typedef pa<int, ll> pil;
typedef pa<ll, int> pli;
typedef pa<ll, ll> pll;
typedef ve<int> vi;
const ld pi = 3.1415926535897932384626433832795l;
template<typename T> inline auto sqr (T x) -> decltype(x * x) {return x * x;}
template<typename T1, typename T2> inline bool umx (T1& a, T2 b) {if (a < b) {a = b; return 1;} return 0;}
template<typename T1, typename T2> inline bool umn (T1& a, T2 b) {if (b < a) {a = b; return 1;} return 0;}
const int N = 200100;
int p[N];
int l[N], r[N];
int rnd;
int get(int a) {
if (a == p[a])
return a;
return p[a] = get(p[a]);
}
void uni(int L, int R) {
L = get(L), R = get(R);
if (L == R)
return;
if (rnd ^= 1)
swap(L, R);
p[L] = R;
umx(r[R], r[L]);
umn(l[R], l[L]);
}
int a[N];
int n;
pii ord[N];
int main () {
cout.setf(ios::showpoint | ios::fixed);
cout.precision(20);
#ifdef SG
freopen((problemname + ".in").c_str(), "r", stdin);
// freopen((problemname + ".out").c_str(), "w", stdout);
#endif
rnd = 0;
cin >> n;
forn(i, n) {
scanf("%d", &a[i]);
a[i]--;
}
forn(i, n) {
ord[i] = mp(a[i], i);
}
sort(ord, ord + n);
reverse(ord, ord + n);
forn(i, n) {
p[i] = i;
l[i] = i, r[i] = i;
}
ll ans = 0;
forn(i, n) {
int idx = ord[i].sc, val = ord[i].fs;
if (idx > 0 && a[idx - 1] > val) {
uni(idx, idx - 1);
}
if (idx + 1 < n && a[idx + 1] > val) {
uni(idx, idx + 1);
}
int root = get(idx);
ans += 1ll * (r[root] - idx + 1) * (idx - l[root] + 1) * (val + 1);
}
cout << ans << endl;
return 0;
}
Submission Info
Submission Time
2016-10-01 21:18:47+0900
Task
B - Minimum Sum
User
vyacheslav
Language
C++14 (GCC 5.4.1)
Score
400
Code Size
2515 Byte
Status
AC
Exec Time
49 ms
Memory
4992 KB
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:88:21: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i]);
^
Judge Result
Set Name
Sample
All
Score / Max Score
0 / 0
400 / 400
Status
Set Name
Test Cases
Sample
example0, example1, example2
All
corner0, corner1, corner2, corner3, example0, example1, example2, maxrand0, maxrand1, maxrand2, rand0, rand1, rand2
Case Name
Status
Exec Time
Memory
corner0
AC
32 ms
4864 KB
corner1
AC
31 ms
4992 KB
corner2
AC
3 ms
256 KB
corner3
AC
48 ms
4864 KB
example0
AC
3 ms
256 KB
example1
AC
3 ms
256 KB
example2
AC
3 ms
256 KB
maxrand0
AC
49 ms
4864 KB
maxrand1
AC
49 ms
4992 KB
maxrand2
AC
49 ms
4864 KB
rand0
AC
3 ms
256 KB
rand1
AC
3 ms
256 KB
rand2
AC
3 ms
256 KB