Description
晴天有非常严重的选择恐惧症,每次吃饭前他都在纠结到底吃什么。。今天又到了吃饭的时候了。
重光:我给你一个包含n个不同整数的序列a,如果它所有连续子序列的价值和是素数咱们就吃米,不然就吃面。
定义一个序列的价值为序列中所有元素的最小值。
晴天:这不是分分钟给你算出来。
嗯...十分钟过去了,晴天选择死亡。
这个任务就交给你啦。
算出所有连续子序列的价值和。
Input
第一行输入一个整数t,代表有t组测试数据。
每组数据第一行包含一个整数n,表示序列a的元素个数。
接下来一行包含n个整数,表示序列a。
0<=n<=50000,1<=ai<=50000。
Output
对于每组数据输出一个整数,表示序列a的所有连续子序列的价值和。
Sample Input
1 3 1 2 3
Sample Output
10
#include<cstdio> #include<cstring> #include<vector> #include<algorithm> typedef long long LL; using namespace std; vector<int> G; vector<int> :: iterator it; int pos[50010],b[50010]; int main() { int t; scanf("%d",&t); while(t--) { int n,i; scanf("%d",&n); for(i = 1 ; i <= n ; i++) { scanf("%d",&b[i]); pos[b[i]]=i; } sort(b+1,b+n+1); LL ans=0; G.clear(); for(i = 1; i <= n; i++) { it = upper_bound(G.begin(),G.end(),pos[b[i]]); int L,R; if(it == G.end()) { if(G.size() == 0) { L = pos[b[i]] - 1; R = n - pos[b[i]]; } else { R = n - pos[b[i]]; int p = it - 1 - G.begin(); L = pos[b[i]] - G[p] - 1; } } else { if(it == G.begin()) { L = pos[b[i]] - 1; int p = it - G.begin(); R = G[p] - pos[b[i]] - 1; } else { int p = it - G.begin(); R = G[p] - pos[b[i]] - 1; L = pos[b[i]] - G[p-1] - 1; } } ans += 1LL *(L + 1) * (R + 1) * b[i]; it = lower_bound(G.begin() , G.end() ,pos[b[i]]); G.insert(it , pos[b[i]]); } printf("%lld\n",ans); } }