zoukankan      html  css  js  c++  java
  • hdu 3333 Turing Tree

    Problem Description:
    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.
    View Code
      1 #include <cstdio>
      2 #include <cstring>
      3 #include <algorithm>
      4 using namespace std;
      5 #define lson l,m,rt<<1
      6 #define rson m+1,r,rt<<1|1
      7 #define maxn 30005
      8 struct node
      9 {
     10     __int64 sum;
     11 }setree[maxn<<2];
     12 int pre[maxn];
     13 __int64 sorted[maxn];
     14 __int64 num[maxn],ans[100005];
     15 struct op
     16 {
     17     int l,r,id;
     18 }mes[200005];
     19 void build(int l,int r,int rt)
     20 {
     21     setree[rt].sum=0;
     22     if(l==r)
     23     return;
     24     int m=(l+r)>>1;
     25     build(lson);
     26     build(rson);
     27 }
     28 void pushup(int rt)
     29 {
     30     setree[rt].sum=setree[rt<<1].sum+setree[rt<<1|1].sum;
     31 }
     32 void update(int l,int r,int rt,int num,__int64 val)
     33 {
     34     if(l==r){
     35         setree[rt].sum=val;
     36         return;
     37     }
     38     int m=(l+r)>>1;
     39     if(num<=m)
     40     update(lson,num,val);
     41     else
     42     update(rson,num,val);
     43     pushup(rt);
     44 } 
     45 __int64 query(int l,int r,int rt,int L,int R)
     46 {
     47     if(L<=l&&r<=R)
     48     return setree[rt].sum;
     49     int m=(l+r)>>1;
     50     __int64 ans=0;
     51     if(L<=m)
     52     ans+=query(lson,L,R);
     53     if(R>m)
     54     ans+=query(rson,L,R);
     55     return ans;
     56 }
     57 bool cmp(struct op a,struct op b)
     58 {
     59     if(a.r==b.r)
     60     return a.l>b.l;
     61     return a.r<b.r;
     62 }
     63 int binsearch(int l,int r,__int64 key)
     64 {
     65     int m=(l+r)>>1;
     66     if(sorted[m]==key)
     67     return m;
     68     if(key<sorted[m])
     69     return binsearch(l,m-1,key);
     70     return binsearch(m+1,r,key);
     71 }
     72 int main()
     73 {
     74     int t;
     75     scanf("%d",&t);
     76     while(t--){
     77         int n,m,cnt=1;
     78         scanf("%d",&n);
     79         for(int i=1;i<=n;i++){
     80         scanf("%I64d",num+i);
     81         sorted[i]=num[i];
     82         }        
     83         sort(sorted+1,sorted+n+1);
     84         for(int i=2;i<=n;i++)
     85         if(sorted[i]!=sorted[i-1])
     86         sorted[++cnt]=sorted[i];
     87         build(1,n,1);
     88         memset(pre,-1,sizeof(pre));
     89         scanf("%d",&m);
     90         for(int i=0;i<m;i++){
     91             scanf("%d%d",&mes[i].l,&mes[i].r);
     92             mes[i].id=i;
     93         }
     94         sort(mes,mes+m,cmp);
     95         int k=1;
     96         for(int i=0;i<m;i++){
     97             while(k<=mes[i].r){
     98                 int pos=binsearch(1,cnt,num[k]);
     99                 if(pre[pos]==-1){
    100                     pre[pos]=k;
    101                     update(1,n,1,k,num[k]);
    102                 }
    103                 else{
    104                     int pos1=pre[pos];
    105                     update(1,n,1,pos1,0);
    106                     pre[pos]=k;
    107                     update(1,n,1,k,num[k]);
    108                 }
    109                 k++;
    110             }
    111             ans[mes[i].id]=query(1,n,1,mes[i].l,mes[i].r);
    112         }
    113         for(int i=0;i<m;i++)
    114         printf("%I64d\n",ans[i]);
    115     }
    116     return 0;
    117 }
  • 相关阅读:
    【arc072f】AtCoder Regular Contest 072 F
    maven settings解决下载不了依赖包问题
    git 命令提交本地代码到新创建的仓库
    JAVA 利用切面、注解 动态判断请求信息中字段是否需要为空
    JAVA 根据身份证号码解析出生日期、性别、年龄
    利用JAVA正则快速获取URL的文件名
    datalist
    Mybatis map接收list参数
    bootstrap-table 列宽动态拖拽改变宽度
    JAVA 枚举类遍历与switch使用
  • 原文地址:https://www.cnblogs.com/kim888168/p/2879218.html
Copyright © 2011-2022 走看看