# 51nod1712 区间求和

http://www.51nod.com/Challenge/Problem.html#problemId=1712

• $i$左侧$a_l$的个数$A$
• $i$右侧$a_r$的个数$B$
• $i$左侧$a_l$$l$之和$C$
• $i$右侧$a_r$$r$之和$D$

#include <bits/stdc++.h>
using namespace std;

int x = 0, f = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
return x * f;
}

typedef unsigned int uint;
const int N = 1000010;

int n, lim, a[N];
uint num[N], sum[N], numl[N], numr[N], suml[N], sumr[N];
uint Sum, Num;

int main() {
for(int i = 1; i <= n; ++i) {
numr[a[i]]++; sumr[a[i]] += i;
}
uint ans = 0;
for(int i = 1; i <= n; ++i) {
numl[a[i]]++; suml[a[i]] += i;
Num -= num[a[i]];
num[a[i]] = numl[a[i]] * numr[a[i]];
Num += num[a[i]];
Sum -= sum[a[i]];
sum[a[i]] = suml[a[i]] * numr[a[i]] + sumr[a[i]] * numl[a[i]];
Sum += sum[a[i]];

ans += 2 * i * a[i] * Num - a[i] * Sum;

numr[a[i]]--; sumr[a[i]] -= i;
Num -= num[a[i]];
num[a[i]] = numl[a[i]] * numr[a[i]];
Num += num[a[i]];
Sum -= sum[a[i]];
sum[a[i]] = suml[a[i]] * numr[a[i]] + sumr[a[i]] * numl[a[i]];
Sum += sum[a[i]];
}
printf("%u\n", ans);
}

posted @ 2019-09-09 21:28  henry_y  阅读(...)  评论(...编辑  收藏