After inventing Turing Tree, 3xian always felt boring when solving problems about intervals, because Turing Tree could easily have the solution. As well, wily 3xian made lots of new problems about intervals. So, today, this sick thing happens again…
Now given a sequence of N numbers A1, A2, …, AN and a number of Queries(i, j) (1≤i≤j≤N). For each Query(i, j), you are to caculate the sum of distinct values in the subsequence Ai, Ai+1, …, Aj.
Input
The first line is an integer T (1 ≤ T ≤ 10), indecating the number of testcases below.
For each case, the input format will be like this:
题意: 求不同数的和。
有树状数组离线写法的话,就是将询问按照y值排序,然后你要保证每个数出现一次,那么当这个数出现的时候,你把上次出现时候的值抵消掉。
离线写法
#include
#include
#include
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 7;
struct Node {
int x,y,id;
}q[maxn];
ll a[maxn],b[maxn],c[maxn],ans[maxn];
int pre[maxn];
int n;
void init() {
for(int i = 1;i <= n;i++) {
c[i] = 0;
pre[i] = 0;
}
}
int cmp(Node a,Node b) {
return a.y < b.y;
}
int lowbit(int x) {
return x & -x;
}
void add(int x,ll v) {
while(x < maxn) {
c[x] += v;
x += lowbit(x);
}
}
ll sum(int x) {
ll res = 0;
while(x) {
res += c[x];
x -= lowbit(x);
}
return res;
}
int Bin(ll x) {
return lower_bound(b + 1,b + 1 + n,x) - b;
}
int main() {
int T;scanf("%d",&T);
while(T--) {
scanf("%d",&n);
init();
for(int i = 1;i <= n;i++) {
scanf("%lld",&a[i]);
b[i] = a[i];
}
sort(b + 1,b + 1 + n);
int m;scanf("%d",&m);
for(int i = 1;i <= m;i++) {
scanf("%d%d",&q[i].x,&q[i].y);
q[i].id = i;
}
sort(q + 1,q + 1 + m,cmp);
int j = 1;
for(int i = 1;i <= n;i++) {
int t = Bin(a[i]);
if(pre[t]) {
add(pre[t],-a[i]);
}
add(i,a[i]);
pre[t] = i;
for(;j <= m;j++) {
if(q[j].y != i) break;
ans[q[j].id] = sum(q[j].y) - sum(q[j].x - 1);
}
}
for(int i = 1;i <= m;i++) {
printf("%lld\n",ans[i]);
}
}
return 0;
}
莫队写法
#pragma GCC optimize(2)
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 7;
int n,m,t,a[maxn];
ll ans[maxn],now;
int cnt[maxn];
int b[maxn],c[maxn];
struct Node
{
int l,r,id;
}nodes[maxn];
void init() {
now = 0;
for(int i = 1;i <= n;i++) {
cnt[i] = 0;
}
}
int cmp(Node x,Node y)
{
if(x.l / t == y.l / t) return x.r < y.r;
return x.l / t < y.l / t;
}
void add(int x)
{
if(cnt[c[x]] == 0) now += a[x];
++cnt[c[x]];
}
void del(int x)
{
--cnt[c[x]];
if(cnt[c[x]] == 0) now -= a[x];
}
int Bin(int x) {
return lower_bound(b + 1,b + 1 + n,x) - b;
}
int main()
{
int T;scanf("%d",&T);
while(T--) {
scanf("%d",&n);
init();
for(int i = 1;i <= n;i++) {
scanf("%d",&a[i]);
b[i] = a[i];
}
t = sqrt(n);
sort(b + 1,b + 1 + n);
for(int i = 1;i <= n;i++) {
c[i] = Bin(a[i]);
}
scanf("%d",&m);
for(int i = 1;i <= m;i++)
{
scanf("%d %d",&nodes[i].l,&nodes[i].r);
nodes[i].id = i;
}
sort(nodes + 1,nodes + 1 + m,cmp);
int l = 1,r = 0;//l代表1~l-1都没有,r代表r+1~n都没有
for(int i = 1;i <= m;i++)
{
while(l < nodes[i].l)del(l++);
while(l > nodes[i].l)add(--l);
while(r < nodes[i].r)add(++r);
while(r > nodes[i].r)del(r--);
ans[nodes[i].id] = now;
}
for(int i = 1;i <= m;i++)printf("%lld\n",ans[i]);
}
return 0;
}