传送门:
https://cn.vjudge.net/problem/UVALive-5970
题意:
给N(10^4)个数,Q次询问(10^5),每次询问[l,r]不同的数的平均值。
思路:
没有修改操作,数据不大,可以用离线莫队去做,7s水过,非常sb,样例还错了
当然也可以线段树做,参考:
http://mcginn.lofter.com/post/1d50332c_92ac05a
代码:
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define ll long long
#define mod 1000000007
#define ull unsigned long long
#define mst(ss,b) memset(ss,b,sizeof(ss));
#define pl(x) cout << #x << "= " << x << endl;
const int inf = 0x3f3f3f3f;
const int N = 1e4+5;
int n;
int blocks;
ll a[N], now, num;
double ans[N];
map<ll, int>mp;
struct node{
int l, r, id;
bool operator < (const node& rhs) const{
return (l/blocks < rhs.l/blocks) || (l/blocks == rhs.l/blocks && r/blocks < rhs.r/blocks);
}
}q[N];
void add(ll x){
if(!mp[x]){
now += x;
num++;
}
mp[x]++;
}
void del(ll x){
mp[x]--;
if(!mp[x]){
now -= x;
num--;
}
}
int main(){
int T;
scanf("%d", &T);
for(int kase=1; kase<=T; kase++){
mp.clear();
now = 0; num = 0;
scanf("%d", &n);
blocks = sqrt(n);
for(int i=1; i<=n; i++)scanf("%lld", &a[i]);
int m;
scanf("%d", &m);
for(int i=1; i<=m; i++){
scanf("%d%d", &q[i].l, &q[i].r);
q[i].id = i;
}
sort(q+1, q+1+m);
int l = 1, r = 0;
for(int i=1; i<=m; i++){
while(q[i].l > l)del(a[l++]);
while(q[i].l < l)add(a[--l]);
while(q[i].r > r)add(a[++r]);
while(q[i].r < r)del(a[r--]);
ans[q[i].id] = 1.0*now/num;
// pl(q[i].id); pl(now); pl(num);
}
printf("Case %d:\n", kase);
for(int i=1; i<=m; i++)printf("%.6f\n", ans[i]);
}
return 0;
}