码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj4299: Codechef FRBSUM

时间:2015-10-14 09:02:30      阅读:369      评论:0      收藏:0      [点我收藏+]

标签:

---恢复内容开始---

可持久化线段树

http://www.lydsy.com/JudgeOnline/problem.php?id=4299

技术分享
/**************************************************************
    Problem: 4299
    User: 1349367067
    Language: C++
    Result: Accepted
    Time:5592 ms
    Memory:40728 kb
****************************************************************/
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m;
int a[100011]={},b[100011]={};
int g[100011]={},num=0;
int nxt[100011]={};
struct Node
{
       int x;
       Node *l,*r;
       Node()
       {
             l=r=NULL;
             x=0;
       }
}nod[100011*32];
Node *T[100011]={};
int lower(int x)
{
    int l,r,mid,t;
    l=1;r=n;t=0;
    while (l<=r)
    {
          mid=(l+r)/2;
          if (b[mid]<=x) {t=mid;l=mid+1;}
          else r=mid-1;
    }
    return t;
}
Node *ins(Node *now,int l,int r,int to,int x)
{
     Node *now_;
     num++;now_=&nod[num];
     if (now!=NULL)
        now_->x=now->x+x;
     else
        now_->x=x;
     if (l==r) return now_;
     int mid=(l+r)/2;
     if (to<=mid) 
        if (now!=NULL)
        {
           now_->l=ins(now->l,l,mid,to,x);
           if (now->r!=NULL)
           now_->r=now->r;
        }
        else
        {
           now_->l=ins(NULL,l,mid,to,x);
        }
     else
        if (now!=NULL)
        {
           now_->r=ins(now->r,mid+1,r,to,x);
           if (now->l!=NULL)
           now_->l=now->l;
        }
        else
        {
           now_->r=ins(NULL,mid+1,r,to,x);
        }
     return now_;
}
int L,R;
int ask(Node *x,int l,int r)
{
    if (x==NULL) return 0;
    if (l>=L&&r<=R) return x->x;
    int mid,t;
    mid=(l+r)/2;t=0;
    if (L<=mid) t+=ask(x->l,l,mid);
    if (R>mid) t+=ask(x->r,mid+1,r);
    return t;
}
int query()
{
    int j;
    for (int i=0;;i=j)
        if ((j=ask(T[lower(i+1)],1,n))==i)
           return i+1;
}
void init()
{
     scanf("%d",&n);
     for (int i=1;i<=n;i++) scanf("%d",&a[i]);
     for (int i=1;i<=n;i++) b[i]=a[i];
     sort(b+1,b+n+1);
     for (int i=1;i<=n;i++)
     {
         a[i]=lower(a[i]);
         nxt[i]=g[a[i]];
         g[a[i]]=i;
     }
     for (int i=1;i<=n;i++)
     {
         T[i]=T[i-1];
         for (int j=g[i];j;j=nxt[j])
         {
             T[i]=ins(T[i],1,n,j,b[i]);
         }
          
     }
     scanf("%d",&m);
     for (int i=1;i<=m;i++)
     {
         scanf("%d%d",&L,&R);
         printf("%d\n",query());
     }
}
int main()
{
    init();
    return 0;
}
View Code

 

bzoj4299: Codechef FRBSUM

标签:

原文地址:http://www.cnblogs.com/3ZStarve/p/4876393.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!