This question is exactly the same as HDU3874.
HDU3874 solution report
Of course, the data is enhanced.
The only difference is that each value is 10 ^ 9, so we need to discretization and directly implement STL. Then the process below will be exactly the same as that of 3874.
Note that the median value is greater than int.
This kind of question is very much recently, and it seems that more efforts have been made to strengthen this exercise.
#include <iostream> #include <cstdio> #include <algorithm> #include <string> #include <cmath> #include <cstring> #include <queue> #include <set> #include <vector> #include <stack> #include <map> #include <iomanip> #define PI acos(-1.0) #define Max 2505 #define inf 1<<28 #define LL(x) ( x << 1 ) #define RR(x) ( x << 1 | 1 ) #define REP(i,s,t) for( int i = ( s ) ; i <= ( t ) ; ++ i ) #define ll __int64 #define mem(a,b) memset(a,b,sizeof(a)) #define mp(a,b) make_pair(a,b) #define PII pair<int,int> using namespace std; inline void RD(int &ret) { char c; do { c = getchar(); } while(c < '0' || c > '9') ; ret = c - '0'; while((c=getchar()) >= '0' && c <= '9') ret = ret * 10 + ( c - '0' ); } #define N 30005 int a[N] ; int b[N] ; int pos[N] ; int n ; int vis[N] ; struct kdq{ int s , e , id ; }Q[N * 10] ; bool cmp(const kdq&a ,const kdq&b){ if(a.e == b.e)return a.s < b.s ; return a.e < b.e ; } ll c[N] ; ll ans[N * 10] ; void update(int po ,int num){ for (int i = po ; i <= n ; i += i & (-i) )c[i] += num ; } ll sum(int po){ ll ans = 0 ; for (int i = po ; i >= 1 ; i -= i & (-i))ans += c[i] ; return ans ; } void init(){ mem(c ,0) ; mem(vis ,0) ; } int main() { int T ; cin >> T ; while( T -- ){ init() ; cin >> n ; for (int i = 1 ; i <= n ; i ++ ){ RD(a[i]) ; b[i] = a[i] ; } int m ; cin >> m ; for (int i = 0 ; i < m ; i ++ ){ RD(Q[i].s) ; RD(Q[i].e) ; Q[i].id = i ; } sort(Q , Q + m ,cmp) ; sort(b + 1, b + n + 1) ; int dd = unique(b + 1, b + n + 1) - b ; for (int i = 1 ; i <= n ; i ++ ){ pos[i] = lower_bound(b ,b + n ,a[i]) - b + 1 ; } int now = 1 ; for (int i = 0 ; i < m ; i ++ ){ while(Q[i].e >= now){ if(vis[pos[now]]){ update(vis[pos[now]] ,-a[now]) ; } vis[pos[now]] = now ; update(vis[pos[now]] , a[now]) ; now ++ ; } ans[Q[i].id] = sum(Q[i].e) - sum(Q[i].s - 1) ; } for (int i = 0 ; i < m ;i ++ ){ printf("%I64d\n",ans[i]) ; } } return 0 ; } #include <iostream>#include <cstdio>#include <algorithm>#include <string>#include <cmath>#include <cstring>#include <queue>#include <set>#include <vector>#include <stack>#include <map>#include <iomanip>#define PI acos(-1.0)#define Max 2505#define inf 1<<28#define LL(x) ( x << 1 )#define RR(x) ( x << 1 | 1 )#define REP(i,s,t) for( int i = ( s ) ; i <= ( t ) ; ++ i )#define ll __int64#define mem(a,b) memset(a,b,sizeof(a))#define mp(a,b) make_pair(a,b)#define PII pair<int,int>using namespace std;inline void RD(int &ret) { char c; do { c = getchar(); } while(c < '0' || c > '9') ; ret = c - '0'; while((c=getchar()) >= '0' && c <= '9') ret = ret * 10 + ( c - '0' );}#define N 30005int a[N] ;int b[N] ;int pos[N] ;int n ;int vis[N] ;struct kdq{ int s , e , id ;}Q[N * 10] ;bool cmp(const kdq&a ,const kdq&b){ if(a.e == b.e)return a.s < b.s ; return a.e < b.e ;}ll c[N] ;ll ans[N * 10] ;void update(int po ,int num){ for (int i = po ; i <= n ; i += i & (-i) )c[i] += num ;}ll sum(int po){ ll ans = 0 ; for (int i = po ; i >= 1 ; i -= i & (-i))ans += c[i] ; return ans ;}void init(){ mem(c ,0) ; mem(vis ,0) ;}int main() { int T ; cin >> T ; while( T -- ){ init() ; cin >> n ; for (int i = 1 ; i <= n ; i ++ ){ RD(a[i]) ; b[i] = a[i] ; } int m ; cin >> m ; for (int i = 0 ; i < m ; i ++ ){ RD(Q[i].s) ; RD(Q[i].e) ; Q[i].id = i ; } sort(Q , Q + m ,cmp) ; sort(b + 1, b + n + 1) ; int dd = unique(b + 1, b + n + 1) - b ; for (int i = 1 ; i <= n ; i ++ ){ pos[i] = lower_bound(b ,b + n ,a[i]) - b + 1 ; } int now = 1 ; for (int i = 0 ; i < m ; i ++ ){ while(Q[i].e >= now){ if(vis[pos[now]]){ update(vis[pos[now]] ,-a[now]) ; } vis[pos[now]] = now ; update(vis[pos[now]] , a[now]) ; now ++ ; } ans[Q[i].id] = sum(Q[i].e) - sum(Q[i].s - 1) ; } for (int i = 0 ; i < m ;i ++ ){ printf("%I64d\n",ans[i]) ; } } return 0 ;}