bzoj4299: Codechef FRBSUM

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

可持久化线段树

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;
}

时间: 2024-10-18 05:59:40

bzoj4299: Codechef FRBSUM的相关文章

CodeChef FNCS (分块+树状数组)

题目:https://www.codechef.com/problems/FNCS 题解: 我们知道要求区间和的时候,我们用前缀和去优化.这里也是一样,我们要求第 l 个函数到第 r 个函数 [l, r] 的函数和,那么我们可以用 sum[r] - sum[l-1] 来求得. 由于这个数据量有点大,所以我们将函数分块. 例如样例: 1 3 有5个函数,那么我们分成3块.{ [1 3] , [2 5] }, { [4 5], [3 5] }, { [1 2] }.每一块对应都有一个sum ,这时如

codechef营养题 第三弹

第三弾が始まる! codechef problems 第三弹 一.Motorbike Racing 题面 It's time for the annual exciting Motorbike Race in Byteland. There are N motorcyclists taking part in the competition. Johnny is watching the race. At the present moment (time 0), Johnny has taken

codechef 营养题 第一弹

第一弾が始まる! 定期更新しない! 来源:http://wenku.baidu.com/link?url=XOJLwfgMsZp_9nhAK15591XFRgZl7f7_x7wtZ5_3T2peHh5XXoERDanUcdxw08SmRj1a5VY1o7jpW1xYv_V1kuYao1Pg4yKdfG4MfNsNAEa codechef problems 第一弹 一.Authentication Failed原题题面Several days ago Chef decided to registe

bzoj4260: Codechef REBXOR

求异或maxmin一般用trie (二进制式的trie).query中找的是满足((x>>i)&1)^A=1,那么A=((x>>i)&1)^1:maxx=max(sumx,sumi)(i=[1,x]).(YY一下异或的性质 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #def

Codechef July Challenge 2014部分题解

Dish Owner(并查集) 链接:http://www.codechef.com/JULY14/problems/DISHOWN/ 题意分析:本题主要操作就是给出0 x y时,拥有第x道菜的厨师与拥有第y道菜的厨师pk,谁拥有的所有菜的其中一道菜(不一定是x或y)的分值比较高谁就获胜,并赢得loser的所有菜.即比较的是每个人分值最高的菜,所以对于非loser的人来说,他的分值最高的菜是不变的.综合题意用并查集易解. #include <cstdio> const int Maxn=100

codechef Row and Column Operations 题解

You are given an N × N grid initially filled by zeros. Let the rows and columns of the grid be numbered from1 to N, inclusive. There are two types of operations can be applied to the grid: RowAdd R X: all numbers in the row R should be increased by X

Codechef Nuclear Reactors 题解

There are K nuclear reactor chambers labelled from 0 to K-1. Particles are bombarded onto chamber 0. The particles keep collecting in the chamber 0. However if at any time, there are more than N particles in a chamber, a reaction will cause 1 particl

codechef Cleaning Up 题解

After a long and successful day of preparing food for the banquet, it is time to clean up. There is a list of n jobs to do before the kitchen can be closed for the night. These jobs are indexed from 1 to n. Most of the cooks have already left and onl

codechef Permutation Cycles 题解

We consider permutations of the numbers 1,..., N for some N. By permutation we mean a rearrangment of the number 1,...,N. For example 2  4  5  1  7  6  3  8 is a permutation of 1,2,...,8. Of course, 1  2  3  4  5  6  7  8 is also a permutation of 1,2