Intended Problem : Problem
My Method :
I have calculated Which Number will present how many times in the new array. Then calculate the sum..
#include <bits/stdc++.h> using namespace std; /*------- Constants---- */ #define LL long long #define ull unsigned long long #define mod 1000000007 #define MEMSET_INF 63 #define MEM_VAL 1061109567 #define forn(i,n) for( int i=0 ; i < n ; i++ ) #define mp(i,j) make_pair(i,j) #define lop(i,a,b) for( int i = (a) ; i < (b) ; i++) #define pb(a) push_back((a)) #define gc getchar_unlocked #define PI acos(-1.0) #define inf 1<<30 #define lc ((n)<<1) #define rc ((n)<<1 |1) #define db(x) cout << #x << " -> " << x << endl; #define DI(n) int n;sc(n) #define DII(a,b) int a,b;sc(a);sc(b) #define DIII(a,b,c) int a,b,c;sc(a);sc(b);sc(c) #define min(a,b) ((a)>(b) ? (b) : (a) ) #define max(a,b) ((a)>(b) ? (a):(b)) /*---- short Cuts ------- */ #define ms(ara_name,value) memset(ara_name,value,sizeof(ara_name)) typedef pair<int, int> ii; typedef vector<int > vi ; /*------ template functions ------ */ #ifndef getchar_unlocked #define getchar_unlocked getchar #endif template<class T> inline void sc(T &x){ register int c = gc(); x = 0; int neg = 0; for(;((c<48 | c>57) && c != '-');c = gc()); if(c=='-') {neg=1;c=gc();} for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;} if(neg) x=-x; } template <class T> inline T bigmod(T p,T e,T M){ LL ret = 1; for(; e > 0; e >>= 1){ if(e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);} template <class T> inline T modinverse(T a,T M){return bigmod(a,M-2,M);} /*************************** END OF TEMPLATE ****************************/ const int N = 2e5+5; LL ar[N]; vector<LL> v; LL prv[N], nxt[N]; LL occr[N]; LL tp[N]; LL csum[N]; int main() { DI(tc); while(tc--){ DI(n); DI(k); lop(i,1,n+1) { sc(ar[i]); v.pb(ar[i]); } sort(v.begin(), v.end()); v.resize(unique(v.begin() , v.end()) - v.begin()); for(int i = 1; i<= n; i ++ ) { int j = i-1; while(j>0 && ar[j] <= ar[i]) j = prv[j]; prv[i] = j; } for(int i = n; i > 0;i -- ) { int j = i + 1; while(j <= n && ar[j] < ar[i] ) j = nxt[j]; nxt[i] = j; } for(LL i = 1;i <= n; i ++ ) { LL p = (i-prv[i] ) * (nxt[i] -i ); occr[i] = p; int iL = lower_bound(v.begin(), v.end() , ar[i])- v.begin(); tp[iL] += p; } LL sum = 0; for(int i =0; i < v.size() ; i ++ ) { if(v[i] > k ) sum += tp[i]; } cout << sum << endl; v.clear(); ms(tp,0); } }