Necklace
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4191 Accepted Submission(s): 1401
Problem Description
Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ball has a beautiful value. The balls with the same beautiful value look the same, so if two or more balls have the same beautiful value, we just count it once. We define the beautiful value of some interval [x,y] as F(x,y). F(x,y) is calculated as the sum of the beautiful value from the xth ball to the yth ball and the same value is ONLY COUNTED ONCE. For example, if the necklace is 1 1 1 2 3 1, we have F(1,3)=1, F(2,4)=3, F(2,6)=6.
Now Mery thinks the necklace is too long. She plans to take some continuous part of the necklace to build a new one. She wants to know each of the beautiful value of M continuous parts of the necklace. She will give you M intervals [L,R] (1<=L<=R<=N) and you must tell her F(L,R) of them.
Now Mery thinks the necklace is too long. She plans to take some continuous part of the necklace to build a new one. She wants to know each of the beautiful value of M continuous parts of the necklace. She will give you M intervals [L,R] (1<=L<=R<=N) and you must tell her F(L,R) of them.
Input
The first line is T(T<=10), representing the number of test cases.
For each case, the first line is a number N,1 <=N <=50000, indicating the number of the magic balls. The second line contains N non-negative integer numbers not greater 1000000, representing the beautiful value of the N balls. The third line has a number M, 1 <=M <=200000, meaning the nunber of the queries. Each of the next M lines contains L and R, the query.
For each case, the first line is a number N,1 <=N <=50000, indicating the number of the magic balls. The second line contains N non-negative integer numbers not greater 1000000, representing the beautiful value of the N balls. The third line has a number M, 1 <=M <=200000, meaning the nunber of the queries. Each of the next M lines contains L and R, the query.
Output
For each query, output a line contains an integer number, representing the result of the query.
Sample Input
2 6 1 2 3 4 3 5 3 1 2 3 5 2 6 6 1 1 1 2 3 5 3 1 1 2 4 3 5
Sample Output
3 7 14 1 3 6
Source
Recommend
唉,树状数组和线段树的应用都是基于他们是“活”的,他们的更新是有顺序的,巧妙地运用顺序方能解题。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=5000;
int n;
int a[maxn+20];
int sum[4*maxn+20];
int tot;
void update(int num,int le,int ri,int x)
{
if(le==ri)
{
sum[num]++;
return;
}
int mid=(le+ri)>>1;
if(x<=mid) update(num*2,le,mid,x);
else update(num*2+1,mid+1,ri,x);
sum[num]=sum[num<<1]+sum[(num<<1)+1];
}
int query(int num,int le,int ri,int x,int y)
{
if(x<=le&&ri<=y)
{
return sum[num];
}
int ret=0;
int mid=(le+ri)>>1;
if(x<=mid) ret+=query(num*2 ,le ,mid,x,y);
if(y> mid) ret+=query(num*2+1,mid+1,ri ,x,y);
return ret;
}
int main()
{
while(~scanf("%d",&n))
{
memset(sum,0,sizeof sum);
tot=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
tot+=query (1,0,maxn,a[i]+1,maxn);
update(1,0,maxn,a[i]);
}
int mini=tot;
for(int i=1;i<=n-1;i++)
{
int sm=query(1,0,maxn,0,a[i])-1;
int la=n-1-sm;
tot=tot-sm+la;
mini=min(mini,tot);
}
printf("%d\n",mini);
}
return 0;
}