結果
問題 | No.1975 Zigzag Sequence |
ユーザー | LayCurse |
提出日時 | 2021-08-21 03:19:31 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 35,294 bytes |
コンパイル時間 | 4,279 ms |
コンパイル使用メモリ | 251,924 KB |
実行使用メモリ | 28,348 KB |
最終ジャッジ日時 | 2024-09-21 07:15:38 |
合計ジャッジ時間 | 9,914 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 5 ms
16,476 KB |
testcase_01 | AC | 6 ms
15,332 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | AC | 7 ms
16,480 KB |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | AC | 150 ms
26,356 KB |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | WA | - |
ソースコード
#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("inline") #include<bits/stdc++.h> using namespace std; #define MD (1000000007U) void*wmem; char memarr[314572800]; template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){ static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] ); (*arr)=(T*)(*mem); (*mem)=((*arr)+x); } template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){ walloc1d(arr, x2-x1, mem); (*arr) -= x1; } template<class T> void malloc1d(T **arr, int x){ (*arr) = (T*)malloc(x*sizeof(T)); } template<class T> void free1d(T *arr){ free(arr); } template<class T1> void sortA_L(int N, T1 a[], void *mem = wmem){ sort(a, a+N); } template<class T1, class T2> void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem){ int i; pair<T1, T2>*arr; walloc1d(&arr, N, &mem); for(i=(0);i<(N);i++){ arr[i].first = a[i]; arr[i].second = b[i]; } sort(arr, arr+N); for(i=(0);i<(N);i++){ a[i] = arr[i].first; b[i] = arr[i].second; } } template<class T1, class T2, class T3> void sortA_L(int N, T1 a[], T2 b[], T3 c[], void *mem = wmem){ int i; pair<T1, pair<T2, T3> >*arr; walloc1d(&arr, N, &mem); for(i=(0);i<(N);i++){ arr[i].first = a[i]; arr[i].second.first = b[i]; arr[i].second.second = c[i]; } sort(arr, arr+N); for(i=(0);i<(N);i++){ a[i] = arr[i].first; b[i] = arr[i].second.first; c[i] = arr[i].second.second; } } struct Modint{ unsigned val; Modint(){ val=0; } Modint(int a){ val = ord(a); } Modint(unsigned a){ val = ord(a); } Modint(long long a){ val = ord(a); } Modint(unsigned long long a){ val = ord(a); } inline unsigned ord(unsigned a){ return a%MD; } inline unsigned ord(int a){ a %= (int)MD; if(a < 0){ a += MD; } return a; } inline unsigned ord(unsigned long long a){ return a%MD; } inline unsigned ord(long long a){ a %= (int)MD; if(a < 0){ a += MD; } return a; } inline unsigned get(){ return val; } inline Modint &operator++(){ val++; if(val >= MD){ val -= MD; } return *this; } inline Modint &operator--(){ if(val == 0){ val = MD - 1; } else{ --val; } return *this; } inline Modint operator++(int a){ Modint res(*this); val++; if(val >= MD){ val -= MD; } return res; } inline Modint operator--(int a){ Modint res(*this); if(val == 0){ val = MD - 1; } else{ --val; } return res; } inline Modint &operator+=(Modint a){ val += a.val; if(val >= MD){ val -= MD; } return *this; } inline Modint &operator-=(Modint a){ if(val < a.val){ val = val + MD - a.val; } else{ val -= a.val; } return *this; } inline Modint &operator*=(Modint a){ val = ((unsigned long long)val*a.val)%MD; return *this; } inline Modint &operator/=(Modint a){ return *this *= a.inverse(); } inline Modint operator+(Modint a){ return Modint(*this)+=a; } inline Modint operator-(Modint a){ return Modint(*this)-=a; } inline Modint operator*(Modint a){ return Modint(*this)*=a; } inline Modint operator/(Modint a){ return Modint(*this)/=a; } inline Modint operator+(int a){ return Modint(*this)+=Modint(a); } inline Modint operator-(int a){ return Modint(*this)-=Modint(a); } inline Modint operator*(int a){ return Modint(*this)*=Modint(a); } inline Modint operator/(int a){ return Modint(*this)/=Modint(a); } inline Modint operator+(long long a){ return Modint(*this)+=Modint(a); } inline Modint operator-(long long a){ return Modint(*this)-=Modint(a); } inline Modint operator*(long long a){ return Modint(*this)*=Modint(a); } inline Modint operator/(long long a){ return Modint(*this)/=Modint(a); } inline Modint operator-(void){ Modint res; if(val){ res.val=MD-val; } else{ res.val=0; } return res; } inline operator bool(void){ return val!=0; } inline operator int(void){ return get(); } inline operator long long(void){ return get(); } inline Modint inverse(){ int a = val; int b = MD; int u = 1; int v = 0; int t; Modint res; while(b){ t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } if(u < 0){ u += MD; } res.val = u; return res; } inline Modint pw(unsigned long long b){ Modint a(*this); Modint res; res.val = 1; while(b){ if(b&1){ res *= a; } b >>= 1; a *= a; } return res; } inline bool operator==(int a){ return ord(a)==val; } inline bool operator!=(int a){ return ord(a)!=val; } } ; inline Modint operator+(int a, Modint b){ return Modint(a)+=b; } inline Modint operator-(int a, Modint b){ return Modint(a)-=b; } inline Modint operator*(int a, Modint b){ return Modint(a)*=b; } inline Modint operator/(int a, Modint b){ return Modint(a)/=b; } inline Modint operator+(long long a, Modint b){ return Modint(a)+=b; } inline Modint operator-(long long a, Modint b){ return Modint(a)-=b; } inline Modint operator*(long long a, Modint b){ return Modint(a)*=b; } inline Modint operator/(long long a, Modint b){ return Modint(a)/=b; } inline int my_getchar_unlocked(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread_unlocked(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } struct MY_WRITER{ char buf[1048576]; int s; int e; MY_WRITER(){ s = 0; e = 1048576; } ~MY_WRITER(){ if(s){ fwrite_unlocked(buf, 1, s, stdout); } } } ; MY_WRITER MY_WRITER_VAR; void my_putchar_unlocked(int a){ if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a){ my_putchar_unlocked(a); } inline void wt_L(int x){ int s=0; int m=0; char f[10]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } inline void wt_L(unsigned x){ int s=0; char f[10]; while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } while(s--){ my_putchar_unlocked(f[s]+'0'); } } inline void wt_L(long long x){ int s=0; int m=0; char f[20]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } inline void wt_L(unsigned long long x){ int s=0; char f[21]; while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } while(s--){ my_putchar_unlocked(f[s]+'0'); } } inline void wt_L(Modint x){ int i; i = (int)x; wt_L(i); } int WRITER_DOUBLE_DIGIT = 15; inline int writerDigit_double(){ return WRITER_DOUBLE_DIGIT; } inline void writerDigit_double(int d){ WRITER_DOUBLE_DIGIT = d; } inline void wt_L(double x){ const int d = WRITER_DOUBLE_DIGIT; int k; int r; double v; if(x!=x || (x==x+1 && x==2*x)){ my_putchar_unlocked('E'); my_putchar_unlocked('r'); my_putchar_unlocked('r'); return; } if(x < 0){ my_putchar_unlocked('-'); x = -x; } x += 0.5 * pow(0.1, d); r = 0; v = 1; while(x >= 10*v){ v *= 10; r++; } while(r >= 0){ r--; k = floor(x / v); if(k >= 10){ k = 9; } if(k <= -1){ k = 0; } x -= k * v; v *= 0.1; my_putchar_unlocked(k + '0'); } if(d > 0){ my_putchar_unlocked('.'); v = 1; for(r=(0);r<(d);r++){ v *= 0.1; k = floor(x / v); if(k >= 10){ k = 9; } if(k <= -1){ k = 0; } x -= k * v; my_putchar_unlocked(k + '0'); } } } inline void wt_L(const char c[]){ int i=0; for(i=0;c[i]!='\0';i++){ my_putchar_unlocked(c[i]); } } inline void wt_L(string &x){ int i=0; for(i=0;x[i]!='\0';i++){ my_putchar_unlocked(x[i]); } } template<class S, class T> inline S chmax(S &a, T b){ if(a<b){ a=b; } return a; } template<class T> struct Comb{ int mem_fact; T*factri; T*ifactri; int mem_dfact; T*dfactri; int mem_pw2; int mem_pw3; int mem_pw10; int mem_rep1; T*pw2c; T*pw3c; T*pw10c; T*rep1c; int mem_ipw2; int mem_ipw3; int mem_ipw10; T*ipw2c; T*ipw3c; T*ipw10c; Comb(){ mem_fact = 0; mem_dfact = 0; mem_pw2 = mem_pw3 = mem_pw10 = mem_rep1 = 0; mem_ipw2 = mem_ipw3 = mem_ipw10 = 0; } inline void expand_fact(int k){ int i; if(k <= mem_fact){ return; } chmax(k, 2 * mem_fact); if(mem_fact == 0){ factri = (T*)malloc(k * sizeof(T)); ifactri = (T*)malloc(k * sizeof(T)); factri[0] = 1; for(i=(1);i<(k);i++){ factri[i] = i * factri[i-1]; } ifactri[k-1] = 1 / factri[k-1]; for(i=(k-1)-1;i>=(0);i--){ ifactri[i] = (i+1) * ifactri[i+1]; } } else{ factri = (T*)realloc(factri, k * sizeof(T)); ifactri = (T*)realloc(ifactri, k * sizeof(T)); for(i=(mem_fact);i<(k);i++){ factri[i] = i * factri[i-1]; } ifactri[k-1] = 1 / factri[k-1]; for(i=(k-1)-1;i>=(mem_fact);i--){ ifactri[i] = (i+1) * ifactri[i+1]; } } mem_fact = k; } inline T fac(int k){ if(mem_fact < k+1){ expand_fact(k+1); } return factri[k]; } inline T ifac(int k){ if(mem_fact < k+1){ expand_fact(k+1); } return ifactri[k]; } inline T C(int a, int b){ if(b < 0 || b > a){ return 0; } if(mem_fact < a+1){ expand_fact(a+1); } return factri[a] * ifactri[b] * ifactri[a-b]; } inline T P(int a, int b){ if(b < 0 || b > a){ return 0; } if(mem_fact < a+1){ expand_fact(a+1); } return factri[a] * ifactri[a-b]; } inline T H(int a, int b){ if(a==0 && b==0){ return 1; } if(a <= 0 || b < 0){ return 0; } if(mem_fact < a+b){ expand_fact(a+b); } return C(a+b-1, b); } inline T Multinomial(int sz, int a[]){ int i; int s = 0; T res; for(i=(0);i<(sz);i++){ s += a[i]; } if(mem_fact < s+1){ expand_fact(s+1); } res = factri[s]; for(i=(0);i<(sz);i++){ res *= ifactri[a[i]]; } return res; } inline T Multinomial(int a){ return 1; } inline T Multinomial(int a, int b){ if(mem_fact < a+b+1){ expand_fact(a+b+1); } return factri[a+b] * ifactri[a] * ifactri[b]; } inline T Multinomial(int a, int b, int c){ if(mem_fact < a+b+c+1){ expand_fact(a+b+c+1); } return factri[a+b+c] * ifactri[a] * ifactri[b] * ifactri[c]; } inline T Multinomial(int a, int b, int c, int d){ if(mem_fact < a+b+c+d+1){ expand_fact(a+b+c+d+1); } return factri[a+b+c+d] * ifactri[a] * ifactri[b] * ifactri[c] * ifactri[d]; } inline T Catalan(int n){ if(n < 0){ return 0; } if(mem_fact < 2*n+1){ expand_fact(2*n+1); } return factri[2*n] * ifactri[n] * ifactri[n+1]; } inline T Catalan(int n, int m, int k){ if(k <= 0){ return C(n+m, n); } if(n < k || m < k){ return 0; } return C(n+m, m) - C(n+m, k-1); } inline T Catalan_s(long long n, long long m, long long k){ if(k <= 0){ return C_s(n+m, n); } if(n < k || m < k){ return 0; } return C_s(n+m, m) - C_s(n+m, k-1); } inline T C_s(long long a, long long b){ long long i; T res; if(b < 0 || b > a){ return 0; } if(b > a - b){ b = a - b; } res = 1; for(i=(0);i<(b);i++){ res *= a - i; res /= i + 1; } return res; } inline T P_s(long long a, long long b){ long long i; T res; if(b < 0 || b > a){ return 0; } res = 1; for(i=(0);i<(b);i++){ res *= a - i; } return res; } inline T H_s(long long a, long long b){ if(a==0 && b==0){ return 1; } if(a <= 0 || b < 0){ return 0; } return C_s(a+b-1, b); } inline T per_s(long long n, long long k){ T d; int m; if(n < 0 || k < 0){ return 0; } if(n == k && k == 0){ return 1; } if(n == 0 || k == 0){ return 0; } if(k==1){ return 1; } if(k==2){ d = n / 2; return d; } if(k==3){ d = (n-1) / 6; m = (n-1) % 6; if(m==0){ return 3 * d * d + d; } if(m==1){ return 3 * d * d + 2 * d; } if(m==2){ return 3 * d * d + 3 * d + 1; } if(m==3){ return 3 * d * d + 4 * d + 1; } if(m==4){ return 3 * d * d + 5 * d + 2; } if(m==5){ return 3 * d * d + 6 * d + 3; } } assert(0 && "per_s should be k <= 3"); return -1; } inline void expand_dfact(int k){ int i; if(k <= mem_dfact){ return; } chmax(k, 3); chmax(k, 2 * mem_dfact); if(mem_dfact==0){ dfactri = (T*)malloc(k * sizeof(T)); dfactri[0] = dfactri[1] = 1; for(i=(2);i<(k);i++){ dfactri[i] = i * dfactri[i-2]; } } else{ dfactri = (T*)realloc(dfactri, k * sizeof(T)); for(i=(mem_dfact);i<(k);i++){ dfactri[i] = i * dfactri[i-2]; } } mem_dfact = k; } inline void expand_pw2(int k){ int i; if(k <= mem_pw2){ return; } chmax(k, 2 * mem_pw2); if(mem_pw2==0){ pw2c = (T*)malloc(k * sizeof(T)); pw2c[0] = 1; for(i=(1);i<(k);i++){ pw2c[i] = 2 * pw2c[i-1]; } } else{ pw2c = (T*)realloc(pw2c, k * sizeof(T)); for(i=(mem_pw2);i<(k);i++){ pw2c[i] = 2 * pw2c[i-1]; } } mem_pw2 = k; } inline void expand_ipw2(int k){ int i; if(k <= mem_ipw2){ return; } chmax(k, 2); chmax(k, 2 * mem_ipw2); if(mem_ipw2==0){ ipw2c = (T*)malloc(k * sizeof(T)); ipw2c[0] = 1; ipw2c[1] = ipw2c[0] / 2; for(i=(1);i<(k);i++){ ipw2c[i] = ipw2c[1] * ipw2c[i-1]; } } else{ ipw2c = (T*)realloc(ipw2c, k * sizeof(T)); for(i=(mem_ipw2);i<(k);i++){ ipw2c[i] = ipw2c[1] * ipw2c[i-1]; } } mem_ipw2 = k; } inline void expand_pw3(int k){ int i; if(k <= mem_pw3){ return; } chmax(k, 2 * mem_pw3); if(mem_pw3==0){ pw3c = (T*)malloc(k * sizeof(T)); pw3c[0] = 1; for(i=(1);i<(k);i++){ pw3c[i] = 3 * pw3c[i-1]; } } else{ pw3c = (T*)realloc(pw3c, k * sizeof(T)); for(i=(mem_pw3);i<(k);i++){ pw3c[i] = 3 * pw3c[i-1]; } } mem_pw3 = k; } inline void expand_ipw3(int k){ int i; if(k <= mem_ipw3){ return; } chmax(k, 2); chmax(k, 2 * mem_ipw3); if(mem_ipw3==0){ ipw3c = (T*)malloc(k * sizeof(T)); ipw3c[0] = 1; ipw3c[1] = ipw3c[0] / 3; for(i=(1);i<(k);i++){ ipw3c[i] = ipw3c[1] * ipw3c[i-1]; } } else{ ipw3c = (T*)realloc(ipw3c, k * sizeof(T)); for(i=(mem_ipw3);i<(k);i++){ ipw3c[i] = ipw3c[1] * ipw3c[i-1]; } } mem_ipw3 = k; } inline void expand_pw10(int k){ int i; if(k <= mem_pw10){ return; } chmax(k, 2 * mem_pw10); if(mem_pw10==0){ pw10c = (T*)malloc(k * sizeof(T)); pw10c[0] = 1; for(i=(1);i<(k);i++){ pw10c[i] = 10 * pw10c[i-1]; } } else{ pw10c = (T*)realloc(pw10c, k * sizeof(T)); for(i=(mem_pw10);i<(k);i++){ pw10c[i] = 10 * pw10c[i-1]; } } mem_pw10 = k; } inline void expand_ipw10(int k){ int i; if(k <= mem_ipw10){ return; } chmax(k, 2); chmax(k, 2 * mem_ipw10); if(mem_ipw10==0){ ipw10c = (T*)malloc(k * sizeof(T)); ipw10c[0] = 1; ipw10c[1] = ipw10c[0] / 10; for(i=(1);i<(k);i++){ ipw10c[i] = ipw10c[1] * ipw10c[i-1]; } } else{ ipw10c = (T*)realloc(ipw10c, k * sizeof(T)); for(i=(mem_ipw10);i<(k);i++){ ipw10c[i] = ipw10c[1] * ipw10c[i-1]; } } mem_ipw10 = k; } inline void expand_rep1(int k){ int i; if(k <= mem_rep1){ return; } chmax(k, 2 * mem_rep1); if(mem_rep1==0){ rep1c = (T*)malloc(k * sizeof(T)); rep1c[0] = 0; for(i=(1);i<(k);i++){ rep1c[i] = 10 * rep1c[i-1] + 1; } } else{ rep1c = (T*)realloc(rep1c, k * sizeof(T)); for(i=(mem_rep1);i<(k);i++){ rep1c[i] = 10 * rep1c[i-1] + 1; } } mem_rep1 = k; } inline T dfac(int k){ if(k >= 0){ if(mem_dfact < k+1){ expand_dfact(k+1); } return dfactri[k]; } if(k==-1){ return 1; } k = - k - 2; if(k % 4 == 1){ return 1 / (-dfac(k)); } return 1 / dfac(k); } inline T pw2(int k){ if(k >= 0){ if(mem_pw2 < k+1){ expand_pw2(k+1); } return pw2c[k]; } else{ k = -k; if(mem_ipw2 < k+1){ expand_ipw2(k+1); } return ipw2c[k]; } } inline T pw3(int k){ if(k >= 0){ if(mem_pw3 < k+1){ expand_pw3(k+1); } return pw3c[k]; } else{ k = -k; if(mem_ipw3 < k+1){ expand_ipw3(k+1); } return ipw3c[k]; } } inline T pw10(int k){ if(k >= 0){ if(mem_pw10 < k+1){ expand_pw10(k+1); } return pw10c[k]; } else{ k = -k; if(mem_ipw10 < k+1){ expand_ipw10(k+1); } return ipw10c[k]; } } inline T repunit(int k){ if(mem_rep1 < k+1){ expand_rep1(k+1); } return rep1c[k]; } } ; template<> inline Modint Comb<Modint>::C_s(long long a, long long b){ long long i; Modint res; Modint d; if(b < 0 || b > a){ return 0; } if(b > a - b){ b = a - b; } res = d = 1; for(i=(0);i<(b);i++){ res *= a - i; d *= i + 1; } return res / d; } template<class T> int coordcomp_L(int n1, T arr1[], int n2, T arr2[], int n3, T arr3[], int res1[] = NULL, int res2[] = NULL, int res3[] = NULL, void *mem = wmem){ int i; int k = 0; int nn = n1 + n2 + n3; pair<T,int>*r; walloc1d(&r, nn, &mem); for(i=(0);i<(n1);i++){ r[i].first = arr1[i]; r[i].second = i; } for(i=(0);i<(n2);i++){ r[n1+i].first = arr2[i]; r[n1+i].second = n1+i; } for(i=(0);i<(n3);i++){ r[n1+n2+i].first = arr3[i]; r[n1+n2+i].second = n1+n2+i; } sort(r, r+nn); for(i=(0);i<(nn);i++){ if(i && r[i].first != r[i-1].first){ k++; } if(r[i].second < n1){ if(res1!=NULL){ res1[r[i].second] = k; } else{ arr1[r[i].second] = k; } } else if(r[i].second < n1+n2){ if(res2!=NULL){ res2[r[i].second-n1] = k; } else{ arr2[r[i].second-n1] = k; } } else{ if(res3!=NULL){ res3[r[i].second-n1-n2] = k; } else{ arr3[r[i].second-n1-n2] = k; } } } return k+1; } template<class T> struct fenwick{ int size; int memory; T*data; void malloc(int mem); void malloc(int mem, int fg); void walloc(int mem, void **workMemory = &wmem); void walloc(int mem, int fg, void **workMemory = &wmem); void free(void); void init(int N); void add(int k, T val); T get(int k); T range(int a, int b); int kth(T k); } ; template<class S, class T1, class T2> struct rangeTree2d{ int N; int N2; int*sz; S*tot; fenwick<S>*w; T1**d1; T1*ddd1; T2*d2; inline void build(int nn, T1 dd1[], T2 dd2[], S ww[] = NULL, void **mem = &wmem){ int i; int j; int i1; int i2; int k1; int k2; int s; int s1; int s2; S*www; int*ind; N = nn; for(N2=1;N2<N;N2*=2){ ; } walloc1d(&sz,2*N2,mem); walloc1d(&tot,2*N2,mem); walloc1d(&w,2*N2,mem); walloc1d(&d1,2*N2,mem); walloc1d(&d2,nn,mem); malloc1d(&www,nn); walloc1d(&ddd1,nn); walloc1d(&ind,nn); for(i=(0);i<(N);i++){ ddd1[i] = dd1[i]; } for(i=(0);i<(N);i++){ d2[i] = dd2[i]; } if(ww==NULL){ for(i=(0);i<(N);i++){ www[i] = 1; } sortA_L(N,d2,ddd1); } else{ for(i=(0);i<(N);i++){ ind[i] = i; } sortA_L(N,d2,ddd1,ind); for(i=(0);i<(N);i++){ www[i] = ww[ind[i]]; } } for(i=(0);i<(N);i++){ sz[N2+i] = 1; walloc1d(&d1[N2+i], 1, mem); d1[N2+i][0] = ddd1[i]; w[N2+i].walloc(1, mem); w[N2+i].init(1); w[N2+i].add(0,www[i]); tot[N2+i] = www[i]; } for(i=(N);i<(N2);i++){ sz[N2+i] = 0; tot[N2+i] = 0; } for(i=(N2)-1;i>=(1);i--){ i1 = 2*i; i2 = 2*i + 1; s1 = sz[i1]; s2 = sz[i2]; sz[i] = s1 + s2; s = k1 = k2 = 0; walloc1d(&d1[i], sz[i], mem); w[i].walloc(sz[i], mem); w[i].init(sz[i]); while(k1 < s1 || k2 < s2){ if(k2==s2){ d1[i][s] = d1[i1][k1]; w[i].add(s,w[i1].range(k1,k1)); s++; k1++; continue; } if(k1==s1){ d1[i][s] = d1[i2][k2]; w[i].add(s,w[i2].range(k2,k2)); s++; k2++; continue; } if(d1[i1][k1] < d1[i2][k2]){ d1[i][s] = d1[i1][k1]; w[i].add(s,w[i1].range(k1,k1)); s++; k1++; continue; } else{ d1[i][s] = d1[i2][k2]; w[i].add(s,w[i2].range(k2,k2)); s++; k2++; continue; } } } free1d(www); } inline void add(T1 x, T2 y, S v){ int a; int b; int z; a = lower_bound(d2, d2+N, y) - d2; b = upper_bound(d2, d2+N, y) - d2; z = lower_bound(ddd1+a, ddd1+b, x) - ddd1 + N2; while(z){ a = lower_bound(d1[z], d1[z]+sz[z], x) - d1[z]; w[z].add(a, v); z /= 2; } } inline S query(T1 x1, T1 x2, T2 y1, T2 y2){ S res = 0; int a; int b; int z1; int z2; a = lower_bound(d2, d2+N, y1) - d2 + N2; b = lower_bound(d2, d2+N, y2) - d2 + N2; while(a < b){ if(a%2){ z1 = lower_bound(d1[a], d1[a]+sz[a], x1) - d1[a]; z2 = lower_bound(d1[a], d1[a]+sz[a], x2) - d1[a]; if(z1 < z2){ res += w[a].range(z1,z2-1); } a++; } if(b%2){ b--; z1 = lower_bound(d1[b], d1[b]+sz[b], x1) - d1[b]; z2 = lower_bound(d1[b], d1[b]+sz[b], x2) - d1[b]; if(z1 < z2){ res += w[b].range(z1,z2-1); } } a /= 2; b /= 2; } return res; } } ; long long cReader_ll(long long mn, long long mx, char nx){ int i; int fg = 0; int m = 1; int f = -1; long long res = 0; double tmp = 0; for(;;){ i = my_getchar_unlocked(); if(fg==0 && i=='-'){ fg++; m = -1; } else if('0' <= i && i <= '9'){ fg++; if(f == -1){ f = i - '0'; } res = 10 * res + i - '0'; tmp = 10 * tmp + i - '0'; assert(tmp < 1e20); } else{ break; } } assert(tmp / 2 <= res); assert((m==1 && fg >= 1) || (m==-1 && fg >= 2)); assert(mn <= m * res && m * res <= mx); assert(!(res == 0 && m == -1)); assert(!(res != 0 && f == 0)); assert(!(res == 0 && fg >= 2)); assert(i == nx); return m * res; } int N; int A[100000+1]; int v[100000+1]; int ind[100000+1]; fenwick<Modint> f1; fenwick<Modint> f2; Comb<Modint> comb; int solve(){ int Lj4PdHRW; int i; int j; int k; Modint res = 0; for(Lj4PdHRW=(0);Lj4PdHRW<(2);Lj4PdHRW++){ for(i=(0);i<(N);i++){ auto RZTsC2BF = ((A[i])); auto FmcKpFmN = (( i)); v[i]=RZTsC2BF; ind[i]=FmcKpFmN; } sortA_L(N,v,ind); f1.walloc(N,1); f2.walloc(N,1); k = 0; for(i=(0);i<(N);i++){ while(v[k] < v[i]){ f1.add(ind[k], comb.pw2(ind[k])); f2.add(ind[k], comb.pw2(N-1-ind[k])); k++; } res += f1.range(0,ind[i]) * f2.range(ind[i],N-1); } for(i=(0);i<(N);i++){ A[i] = -A[i]; } } return res; } int baka(){ int mask; int s; int arr[12]; Modint res = 0; for(mask=(0);mask<(1<<N);mask++){ int i; s = 0; for(i=(0);i<(N);i++){ if(((mask) &(1<<(i)))){ arr[s++] = A[i]; } } for(i=(1);i<(s-1);i++){ if(arr[i-1] < arr[i] && arr[i] > arr[i+1]){ res++; } } for(i=(1);i<(s-1);i++){ if(arr[i-1] > arr[i] && arr[i] < arr[i+1]){ res++; } } } return res; } rangeTree2d<Modint,int,int> t1; rangeTree2d<Modint,int,int> t2; int xx[100000]; int yy[100000]; Modint ww[100000]; Modint solve2(){ int i; Modint res = 0; for(i=(0);i<(N);i++){ auto XJIcIBrW = ((i)); auto jPV_0s1p = (( A[i])); auto BUotOFBp = (( comb.pw2(i))); xx[i]=XJIcIBrW; yy[i]=jPV_0s1p; ww[i]=BUotOFBp; } t1.build(N,xx,yy,ww); reverse(ww,ww+N); t2.build(N,xx,yy,ww); for(i=(0);i<(N);i++){ res += t1.query(0,i,A[i]+1,1073709056) * t2.query(i+1,N,A[i]+1,1073709056); res += t1.query(0,i,0,A[i]) * t2.query(i+1,N,0,A[i]); } return res; } template<class S, class T1, class T2> void offlineSumRangeTree2d(int N, T1 D1[], T2 D2[], S W[], int Q, T1 X1[], T1 X2[], T2 Y1[], T2 Y2[], S res[], void *mem = wmem){ int i; int k; int sz; int*dd1; int*xx1; int*xx2; T2*yy; int*ind; fenwick<S> f; for(i=(0);i<(Q);i++){ res[i] = 0; } walloc1d(&dd1, N, &mem); walloc1d(&xx1, Q, &mem); walloc1d(&xx2, Q, &mem); walloc1d(&yy, N+Q+Q, &mem); walloc1d(&ind, N+Q+Q, &mem); sz =coordcomp_L(N, D1, Q, X1, Q, X2, dd1, xx1, xx2, mem); f.walloc(sz, 1, &mem); for(i=(0);i<(Q);i++){ ind[i] = i; yy[i] = Y1[i]; } for(i=(0);i<(N);i++){ ind[Q+i] = Q+i; yy[Q+i] = D2[i]; } for(i=(0);i<(Q);i++){ ind[Q+N+i] = Q+N+i; yy[Q+N+i] = Y2[i]; } sortA_L(N+Q+Q, yy, ind, mem); for(i=(0);i<(N+Q+Q);i++){ if(ind[i] < Q){ k = ind[i]; if(xx1[k] >= xx2[k] || Y1[k] >= Y2[k]){ continue; } res[k] -= f.range(xx1[k], xx2[k]-1); } else if(ind[i] < Q+N){ k = ind[i] - Q; f.add(dd1[k], W[k]); } else{ k = ind[i] - Q - N; if(xx1[k] >= xx2[k] || Y1[k] >= Y2[k]){ continue; } res[k] += f.range(xx1[k], xx2[k]-1); } } } int q; int xx1[400000]; int yy1[400000]; int xx2[400000]; int yy2[400000]; Modint rrr[400000]; Modint solve3(){ int i; Modint res = 0; for(i=(0);i<(N);i++){ auto o3WxPXbE = ((0)); auto lQU550vz = (( i)); auto qE8LMwYZ = (( A[i]+1)); auto dKuENJNI = (( 1073709056)); xx1[i+0*N]=o3WxPXbE; xx2[i+0*N]=lQU550vz; yy1[i+0*N]=qE8LMwYZ; yy2[i+0*N]=dKuENJNI; } for(i=(0);i<(N);i++){ auto XNa8avth = ((0)); auto mlGkBPoR = (( i)); auto YlLMHsfa = (( 0)); auto sMcf5Tpe = (( A[i])); xx1[i+1*N]=XNa8avth; xx2[i+1*N]=mlGkBPoR; yy1[i+1*N]=YlLMHsfa; yy2[i+1*N]=sMcf5Tpe; } for(i=(0);i<(N);i++){ auto KaFyNJB9 = ((i+1)); auto AAsEZMFe = (( N)); auto xtzQOlbs = (( A[i]+1)); auto aFgbOQYS = (( 1073709056)); xx1[i+2*N]=KaFyNJB9; xx2[i+2*N]=AAsEZMFe; yy1[i+2*N]=xtzQOlbs; yy2[i+2*N]=aFgbOQYS; } for(i=(0);i<(N);i++){ auto IlgsnSAd = ((i+1)); auto jG1yfsum = (( N)); auto NLJcSLph = (( 0)); auto Wu3kZ3t7 = (( A[i])); xx1[i+3*N]=IlgsnSAd; xx2[i+3*N]=jG1yfsum; yy1[i+3*N]=NLJcSLph; yy2[i+3*N]=Wu3kZ3t7; } for(i=(0);i<(N);i++){ auto grBCmONb = ((i)); auto WZu7joIG = (( A[i])); auto Wv3_QJ0O = (( comb.pw2(i))); xx[i]=grBCmONb; yy[i]=WZu7joIG; ww[i]=Wv3_QJ0O; } offlineSumRangeTree2d(N, xx, yy, ww, 2*N, xx1, xx2, yy1, yy2, rrr); reverse(ww,ww+N); offlineSumRangeTree2d(N, xx, yy, ww, 2*N, xx1+2*N, xx2+2*N, yy1+2*N, yy2+2*N, rrr+2*N); for(i=(0);i<(N);i++){ res += rrr[i+0*N] * rrr[i+2*N]; res += rrr[i+1*N] * rrr[i+3*N]; } return res; } int main(){ int dtztiDQx; wmem = memarr; int i; int j; int k; Modint res = 0; N = cReader_ll(1, 100000, '\n'); for(i=(0);i<(N);i++){ if(i<N-1){ A[i] = cReader_ll(1, 1000000000,' '); } else{ A[i] = cReader_ll(1, 1000000000,'\n'); } } wt_L(solve3()); wt_L('\n'); return 0; for(dtztiDQx=(0);dtztiDQx<(2);dtztiDQx++){ for(i=(0);i<(N);i++){ auto memzHCbB = ((A[i])); auto mRLrOZDI = (( i)); v[i]=memzHCbB; ind[i]=mRLrOZDI; } sortA_L(N,v,ind); f1.walloc(N,1); f2.walloc(N,1); k = 0; for(i=(0);i<(N);i++){ while(v[k] < v[i]){ f1.add(ind[k], comb.pw2(ind[k])); f2.add(ind[k], comb.pw2(N-1-ind[k])); k++; } res += f1.range(0,ind[i]) * f2.range(ind[i],N-1); } for(i=(0);i<(N);i++){ A[i] = -A[i]; } } wt_L(res); wt_L('\n'); return 0; } template<class T> void fenwick<T>::malloc(int mem){ memory = mem; data = (T*)std::malloc(sizeof(T)*mem); } template<class T> void fenwick<T>::malloc(int mem, int fg){ memory = mem; data = (T*)std::malloc(sizeof(T)*mem); if(fg){ init(mem); } } template<class T> void fenwick<T>::walloc(int mem, void **workMemory /* = &wmem*/){ memory = mem; walloc1d(&data, mem, workMemory); } template<class T> void fenwick<T>::walloc(int mem, int fg, void **workMemory /* = &wmem*/){ memory = mem; walloc1d(&data, mem, workMemory); if(fg){ init(mem); } } template<class T> void fenwick<T>::free(void){ memory = 0; free(data); } template<class T> void fenwick<T>::init(int N){ size = N; memset(data,0,sizeof(T)*N); } template<class T> void fenwick<T>::add(int k, T val){ while(k < size){ data[k] += val; k |= k+1; } } template<class T> T fenwick<T>::get(int k){ T res = 0; while(k>=0){ res += data[k]; k = (k&(k+1))-1; } return res; } template<class T> T fenwick<T>::range(int a, int b){ if(a < 0){ a = 0; } if(b >= size){ b = size - 1; } if(b < a){ return 0; } return get(b) - get(a-1); } template<class T> int fenwick<T>::kth(T k){ int i=0; int j=size; int c; T v; while(i<j){ c = (i+j)/2; v = get(c); if(v <= k){ i=c+1; } else{ j=c; } } return i==size?-1:i; } // cLay version 20210819-1 [beta] // --- original code --- // //working_memory=300m // int N, A[1d5+1], v[], ind[]; // fenwick<Modint> f1, f2; // Comb<Modint> comb; // // int solve(){ // int i, j, k; // Modint res = 0; // rep(2){ // rep(i,N) (v[i], ind[i]) = (A[i], i); // sortA(N,v,ind); // // f1.walloc(N,1); // f2.walloc(N,1); // // k = 0; // rep(i,N){ // while(v[k] < v[i]){ // f1.add(ind[k], comb.pw2(ind[k])); // f2.add(ind[k], comb.pw2(N-1-ind[k])); // k++; // } // res += f1.range(0,ind[i]) * f2.range(ind[i],N-1); // } // // rep(i,N) A[i] = -A[i]; // } // return res; // } // // int baka(){ // int s, arr[12]; // Modint res = 0; // // rep(mask,1<<N){ // s = 0; // rep(i,N) if(BIT_ith(mask,i)) arr[s++] = A[i]; // rep(i,1,s-1) if(arr[i-1] < arr[i] && arr[i] > arr[i+1]) res++; // rep(i,1,s-1) if(arr[i-1] > arr[i] && arr[i] < arr[i+1]) res++; // } // // return res; // } // // rangeTree2d<Modint,int,int> t1, t2; // int xx[1d5], yy[1d5]; Modint ww[1d5]; // Modint solve2(){ // Modint res = 0; // rep(i,N) (xx[i], yy[i], ww[i]) = (i, A[i], comb.pw2(i)); // t1.build(N,xx,yy,ww); // reverse(ww,ww+N); // t2.build(N,xx,yy,ww); // rep(i,N){ // res += t1.query(0,i,A[i]+1,int_inf) * t2.query(i+1,N,A[i]+1,int_inf); // res += t1.query(0,i,0,A[i]) * t2.query(i+1,N,0,A[i]); // } // return res; // } // // // // template<class S, class T1, class T2> // void offlineSumRangeTree2d(int N, T1 D1[], T2 D2[], S W[], int Q, T1 X1[], T1 X2[], T2 Y1[], T2 Y2[], S res[], void *mem = wmem){ // int i, k; // int sz, *dd1, *xx1, *xx2; // T2 *yy; int *ind; // fenwick<S> f; // // rep(i,Q) res[i] = 0; // // walloc1d(&dd1, N, &mem); // walloc1d(&xx1, Q, &mem); // walloc1d(&xx2, Q, &mem); // walloc1d(&yy, N+Q+Q, &mem); // walloc1d(&ind, N+Q+Q, &mem); // sz = coordcomp(N, D1, Q, X1, Q, X2, dd1, xx1, xx2, mem); // // f.walloc(sz, 1, &mem); // rep(i,Q) ind[i] = i, yy[i] = Y1[i]; // rep(i,N) ind[Q+i] = Q+i, yy[Q+i] = D2[i]; // rep(i,Q) ind[Q+N+i] = Q+N+i, yy[Q+N+i] = Y2[i]; // sortA(N+Q+Q, yy, ind, mem); // // rep(i,N+Q+Q){ // if(ind[i] < Q){ // k = ind[i]; // if(xx1[k] >= xx2[k] || Y1[k] >= Y2[k]) continue; // res[k] -= f.range(xx1[k], xx2[k]-1); // } else if(ind[i] < Q+N) { // k = ind[i] - Q; // f.add(dd1[k], W[k]); // } else { // k = ind[i] - Q - N; // if(xx1[k] >= xx2[k] || Y1[k] >= Y2[k]) continue; // res[k] += f.range(xx1[k], xx2[k]-1); // } // } // } // // // int q, xx1[4d5], yy1[], xx2[], yy2[]; Modint rrr[]; // Modint solve3(){ // Modint res = 0; // rep(i,N) (xx1[i+0*N], xx2[i+0*N], yy1[i+0*N], yy2[i+0*N]) = (0, i, A[i]+1, int_inf); // rep(i,N) (xx1[i+1*N], xx2[i+1*N], yy1[i+1*N], yy2[i+1*N]) = (0, i, 0, A[i]); // rep(i,N) (xx1[i+2*N], xx2[i+2*N], yy1[i+2*N], yy2[i+2*N]) = (i+1, N, A[i]+1, int_inf); // rep(i,N) (xx1[i+3*N], xx2[i+3*N], yy1[i+3*N], yy2[i+3*N]) = (i+1, N, 0, A[i]); // // rep(i,N) (xx[i], yy[i], ww[i]) = (i, A[i], comb.pw2(i)); // offlineSumRangeTree2d(N, xx, yy, ww, 2*N, xx1, xx2, yy1, yy2, rrr); // reverse(ww,ww+N); // offlineSumRangeTree2d(N, xx, yy, ww, 2*N, xx1+2*N, xx2+2*N, yy1+2*N, yy2+2*N, rrr+2*N); // rep(i,N){ // res += rrr[i+0*N] * rrr[i+2*N]; // res += rrr[i+1*N] * rrr[i+3*N]; // } // return res; // } // // { // int i, j, k; // Modint res = 0; // N = cReader_ll(1, 1d5, '\n'); // rep(i,N) A[i] = cReader_ll(1, 1d9, if[i<N-1, ' ', '\n']); // // wt(solve3()); // return 0; // // // Rand rnd; // // for(;;){ // // int res1, res2; // // void *mem = wmem; // // N = rnd.get(1,11); // // rep(i,N) A[i] = rnd.get(1,10); // // res1 = solve(); // // res2 = baka(); // // printf("%d %d %d\n",N,res1,res2); // // assert(res1==res2); // // wmem = mem; // // } // // rep(2){ // rep(i,N) (v[i], ind[i]) = (A[i], i); // sortA(N,v,ind); // // f1.walloc(N,1); // f2.walloc(N,1); // // k = 0; // rep(i,N){ // while(v[k] < v[i]){ // f1.add(ind[k], comb.pw2(ind[k])); // f2.add(ind[k], comb.pw2(N-1-ind[k])); // k++; // } // res += f1.range(0,ind[i]) * f2.range(ind[i],N-1); // } // // rep(i,N) A[i] = -A[i]; // } // // wt(res); // }