Test instructions: Given a sequence of n elements, make AI, AJ, AK, make AI < AJ > AK and I < J < K three numbers as a group. Find out how many groups there are.
idea: You can use a tree-like array, each time you enter an AI, you can query to the previous input than it is smaller or larger than the number of groups, after the input is not known, so you can open the number of records down in reverse order and then build once canThe range of the other number is taken to 0, so each number is to be added once, and the number of groups is Longlong, and the Hoj server system supports LLD, not i64d. WA a few times
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream>using namespace STD; #define M 35000#define N 50050#define ll Long long#define lowbit (x) (x&-x) int tree[m];int num[n];ll tmp[n];int sum (int RT) {int s=0; while (rt>0) {S+=TREE[RT]; Rt-=lowbit (RT); } return s;} void update (int rt) {while (rt<=m-1) {tree[rt]++; Rt+=lowbit (RT); }}int Main () {int n; while (~SCANF ("%d", &n)) {memset (tree,0,sizeof (tree)); for (int i=1;i<=n;i++) {scanf ("%d", &num[i]); num[i]++; } ll Ans=0; for (int i=1;i<=n;i++) {tmp[i]= sum (num[i]-1); Update (num[i]); } memset (Tree,0,sizeof (tree)); for (int i=n;i>=1;i--) {ans+=tmp[i]* (ll) sum (num[i]-1); Update (num[i]); } printf ("%lld\n", ans); } return 0;}
Hoj 2275 Number sequence (tree-like array)