necklace

Necklace

Time Limit : 15000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 16   Accepted Submission(s) : 3
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.
 
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.
 
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
题意:统计区间里面不重复数字的和;
思路:离线操作+树状数组 ,离线将询问按照右边界进行排序,然后用一个数组对数组里面保存的数字进行标记,标记数组里面保存当前这个数字(标记数组下标)在原数组中所处的位置,如果该数字标记过,则将该标记数组的内容,也就是这个数字所处在原数组中的位置和标记数组的下标(当前数字)的相反数传递到树状数组;
另外在结构体中用ID保存顺序;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

__int64 c[1000005];  //树状数组
int a[1000005];  //原数组
int N=1000005;
int mp[1000005];  //记录重复数字所在位置
__int64 ans[1000005]; //记录结果;

struct question
{
    int num,l,r;
}f[1000005],p;

int cmp(const void *p1,const void *p2)
{
    struct question *a=(struct question *)p1;
    struct question *b=(struct question *)p2;
    return a->r - b->r;
}

int lowbit(int x)
{
    return x&(-x);
}
void update(int n, int x)
{
    while(n<=N)
    {
        c[n]+=x;
        n+=lowbit(n);
    }
}
__int64 getsum(int m)
{
    __int64 sum=0;
    while(m>0)
    {
        sum+=c[m];
        m-=lowbit(m);
    }
    return sum;
}
int main()
{
    int cases,i,ge,qu,is;
    scanf("%d",&cases);
    while(cases--)
    {
        memset(a,0,sizeof(a));
        memset(mp,0,sizeof(mp));
        memset(c,0,sizeof(c));
        scanf("%d",&ge);
        for(i=1;i<=ge;i++)
        {
             scanf("%d",&a[i]);
        }
        scanf("%d",&qu);
        for(i=1;i<=qu;i++)
        {
            scanf("%d %d",&f[i].l,&f[i].r);
            f[i].num=i;
        }
        qsort(f+1,qu,sizeof(f[1]),cmp);

        is=1;
        for(i=1;i<=qu;i++)
        {
            while(is<=f[i].r)
            {
                if(mp[a[is]]!=0)
                    update(mp[a[is]],-a[is]);//将以前重复出现的数字改为零
                mp[a[is]]=is;
                update(is,a[is]);
                is++;
            }
            ans[f[i].num]=getsum(f[i].r)-getsum(f[i].l-1);
        }
        for(i=1;i<=qu;i++)
        {
            printf("%I64d\n",ans[i]);
        }
    }
    return 0;
}


我也不知道为什么用long long 定义数组就不能过,必须要用__int64才能过;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值