zoukankan      html  css  js  c++  java
  • hdu 3874 Necklace

    Necklace

    Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 2004    Accepted Submission(s): 718


    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
     方法很难想,离线处理,先对右区间从小到大排序,对于每一个区间,把前面的重复的删掉,留下离右区间最近的一个,还有就是注意sum的值超过int的范围要用__int64
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    struct node
    {
         int l;
         int r;
         int id;
    }p[800000];
    __int64 ans[200005];
    bool cmp(node a,node b)
    {
         return a.r<b.r;
    }
    __int64 c[50005];
    int a[50005];
    int flag[1000005],n;
    int lowbit(int x)
    {
         return x&(-x);
    }
    void add(int x,int k)
    {
         while(x<=n)
         {
              c[x]+=k;
              x+=lowbit(x);
         }
    }
    __int64 Sum(int x)
    {
         __int64 sum=0;
         while(x>0)
         {
              sum+=c[x];
              x-=lowbit(x);
         }
         return sum;
    }
    int main()
    {
         int m,t,i;
        // freopen("in.txt","r",stdin);
        // freopen("out.txt","w",stdout);
         scanf("%d",&t);
         while(t--)
         {
              scanf("%d",&n);
              for(i=1;i<=n;i++)
              {
                   scanf("%d",&a[i]);
                   flag[a[i]]=0;
                   c[i]=0;
              }
              scanf("%d",&m);
              for(i=1;i<=m;i++)
              {
                   scanf("%d %d",&p[i].l,&p[i].r);
                   p[i].id=i;
              }
              sort(p+1,p+m+1,cmp);
              int j;i=1;
              for(j=1;j<=m;j++)
              {
    
                   while(i<=p[j].r)
                   {
                        if(flag[a[i]])
                             add(flag[a[i]],-a[i]);
                        flag[a[i]]=i;
                        add(i,a[i]);
                        i++;
                   }
                   ans[p[j].id]=Sum(p[j].r)-Sum(p[j].l-1);
              }
              for(i=1;i<=m;i++)
                   printf("%I64d
    ",ans[i]);
         }
         return 0;
    }
  • 相关阅读:
    第6 章 : 应用编排与管理:Deployment
    第5 章 : 应用编排与管理:核心原理
    第4 章 : 理解 Pod 和容器设计模式
    第3 章 : Kubernetes 核心概念
    第2 章 : 容器基本概念
    第1 章 : 第一堂“云原生”课
    阿里云原生技术公开课程-讲师记录及视频链接
    Shell中的(),{}几种语法用法-单独总结
    折腾kubernetes各种问题汇总-<1>
    Kubernetes中Deployment部署故障排除
  • 原文地址:https://www.cnblogs.com/llei1573/p/3258440.html
Copyright © 2011-2022 走看看