luogu P3567 [POI2014]KUR-Couriers

二次联通门 : luogu P3567 [POI2014]KUR-Couriers

MMP

指针 RE + MLE + WA.....

不得已...向黑恶的数组实力低头

/*
    指针

*/
#include <algorithm>
#include <cstdio>

#define Max 2000009

void read (int &now)
{
    now = 0;
    register char word = getchar ();
    while (word < ‘0‘ || word > ‘9‘)
        word = getchar ();
    while (word >= ‘0‘ && word <= ‘9‘)
    {
        now = now * 10 + word - ‘0‘;
        word = getchar ();
    }
}

inline int min (int a, int b)
{
    return a < b ? a : b;
}

inline int max (int a, int b)
{
    return a > b ? a : b;
}

struct Segment_Tree_Data
{
    Segment_Tree_Data *Left, *Right;

    int key;

    Segment_Tree_Data ()
    {
        key = 0;
        Left = Right = NULL;
    }
};

Segment_Tree_Data *Root[Max];

int N, M;

class Lasting_Segment_Tree_Type
{
    private :

        int Query (Segment_Tree_Data *&Last, Segment_Tree_Data *&now, int l, int r)
        {
            if (l == r)
                return l;
            register int Mid = l + r >> 1;
            if (now->Left->key - Last->Left->key > Need)
                return Query (Last->Left, now->Left, l, Mid);
            else if (now->Left->key - Last->Right->key > Need)
                return Query (Last->Right, now->Right, Mid + 1, r);
            return 0;
        }

        int Need;

    public :

        void Build (Segment_Tree_Data *&now, int l, int r)
        {
            now = new Segment_Tree_Data;
            if (l == r)
                return ;
            register int Mid = l + r >> 1;
            Build (now->Left, l, Mid);
            Build (now->Right, Mid + 1, r);
        }

        void Updata (Segment_Tree_Data *Last, Segment_Tree_Data *&now, int pos, int l, int r)
        {
            now = new Segment_Tree_Data;
            now->key = Last->key + 1;
            if (l == r)
                return ;
            register int Mid = l + r >> 1;
            if (pos <= Mid)
            {
                now->Right = Last->Right;
                Updata (Last->Left, now->Left, pos, l, Mid);
            }
            else
            {
                now->Left = Last->Left;
                Updata (Last->Right, now->Right, pos, Mid + 1, r);
            }
        }

        int Query_Section (int l, int r)
        {
            Need = r - l + 1 >> 1;
            return Query (Root[l - 1], Root[r], 1, N);
        }
};

Lasting_Segment_Tree_Type Tree;

int rank[Max];
int number[Max];

int main (int argc, char *argv[])
{
    read (N);
    read (M);
    Tree.Build (Root[0], 1, N);
    for (int i = 1; i <= N; i++)
    {
        read (number[i]);
        Root[i] = Root[i - 1];
        Tree.Updata (Root[i - 1], Root[i], number[i], 1, N);
    }
    for (int x, y; M--; )
    {
        read (x);
        read (y);
        printf ("%d\n", Tree.Query_Section (x, y));
    }
    return 0;
}

数组...

/*
    luogu P3567 [POI2014]KUR-Couriers

    主席树 + 权值线段树 + 离散化

    每次查询的时候向大于r-l+1 / 2的一边走

    若查不到则返回0     

*/
#include <algorithm>
#include <cstdio>

#define Max 800006

void read (int &now)
{
    now = 0;
    register char word = getchar ();
    while (word < ‘0‘ || word > ‘9‘)
        word = getchar ();
    while (word >= ‘0‘ && word <= ‘9‘)
    {
        now = now * 10 + word - ‘0‘;
        word = getchar ();
    }
}

struct Segment_Tree_Date
{
    int Left, Right;
    int key;

};

int Root[Max];

Segment_Tree_Date tree[Max << 3];

class Lasting_Segment_Tree_Type
{

    private :

        int Tree_Count;

    public :

        void Build (int &now, int l, int r)
        {
            now = ++Tree_Count;
            if (l == r)
                return ;
            register int Mid = l + r >> 1;
            Build (tree[now].Left, l, Mid);
            Build (tree[now].Right, Mid + 1, r);
        }

        void Updata (int Last, int &now, int l, int r, int pos)
        {
            now = ++Tree_Count;
            tree[now].key = tree[Last].key + 1;
            if (l == r)
                return ;
            int Mid = l + r >> 1;
            if (pos <= Mid)
            {
                tree[now].Right = tree[Last].Right;
                Updata (tree[Last].Left, tree[now].Left, l, Mid, pos);
            }
            else
            {
                tree[now].Left = tree[Last].Left;
                Updata (tree[Last].Right, tree[now].Right, Mid + 1, r, pos);
            }
        }

        int Query (int Last, int now, int l, int r, int Need)
        {
            if (l == r)
                return l;
            register int Mid = l + r >> 1;
            if (tree[tree[now].Left].key - tree[tree[Last].Left].key > Need)
                return Query (tree[Last].Left, tree[now].Left, l, Mid, Need);
            else if (tree[tree[now].Right].key - tree[tree[Last].Right].key > Need)
                return Query (tree[Last].Right, tree[now].Right, Mid + 1, r, Need);
            return 0;
        }
};

int N, M;

int number[Max];
int rank[Max];

Lasting_Segment_Tree_Type Tree;

int main (int argc, char *argv[])
{
    read (N);
    read (M);
    for (int i = 1; i <= N; i++)
    {
        read (number[i]);
        rank[i] = number[i];
    }
    std :: sort (rank + 1, rank + 1 + N);
    int Size = std :: unique (rank + 1, rank + 1 + N) - rank - 1;
    Tree.Build (Root[0], 1, Size);
    for (int i = 1; i <= N; i++)
    {
        number[i] = std :: lower_bound (rank + 1, rank + 1 + Size, number[i]) - rank;
        Tree.Updata (Root[i - 1], Root[i], 1, Size, number[i]);
    }
    for (int x, y; M--; )
    {
        read (x);
        read (y);
        printf ("%d\n", rank[Tree.Query (Root[x - 1], Root[y], 1, Size, y - x + 1 >> 1)]);
    }
    return 0;
}
时间: 2025-01-06 20:27:21

luogu P3567 [POI2014]KUR-Couriers的相关文章

luogu P3567 [POI2014] Couriers

传送门 给一个数列,每次询问一个区间内有没有一个数出现次数超过一半 区间的一半就是(r-l+1)/2 对于主席树中的两棵树L,R中的同一个节点[l,r] 如果sum的差值大于k 说明在原序列下标L`R的区间中出现了多于k个权值处于[l,r]的数字 所以按照这个套路二分就行 然后这题bzoj有双倍经验 这里粘的这道题要离散就这个了 Code: 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #in

P3567 [POI2014]KUR-Couriers

P3567 [POI2014]KUR-Couriers 题目: 英文.转链接 题解: 主席树节点开个sum记录l-r这几个数字出现的次数. 查询的时候如果一段数的出现次数之和>区间长度/2,就往这一段数递归下去.当叶子节点满足>区间长度/2时,说明有数字出现次数超过了一半. #include <iostream> #include <cstdio> #include <algorithm> #define N 500005 #define find(x) (

[luogu]P3572 [POI2014]PTA-Little Bird(单调队列)

P3572 [POI2014]PTA-Little Bird 题目描述 In the Byteotian Line Forest there are nn trees in a row. On top of the first one, there is a little bird who would like to fly over to the top of the last tree. Being in fact very little, the bird might lack the s

【BZOJ】【3524】【POI2014】Couriers

可持久化线段树 裸可持久化线段树,把区间第K大的rank改成num即可……(往儿子走的时候不减少) 苦逼的我……MLE了一次(N*30),RE了一次(N*10)……数组大小不会开…… 最后开成N*20的过了 1 /************************************************************** 2 Problem: 3524 3 User: Tunix 4 Language: C++ 5 Result: Accepted 6 Time:5752 ms

P3567 [POI2014]KUR-Couriers 主席树

这个题比一般主席树还要简单,但是用来练习主席树再好不过了,在这里我再放一下主席树板子. 代码: #include<iostream> #include<cstdio> #include<cmath> #include<ctime> #include<queue> #include<algorithm> #include<cstring> using namespace std; #define duke(i,a,n) fo

屯题计划

感觉lxt一天到晚就是在浪啊浪, 毫无斗志, 颓废得不得了, 每天看小说玩手机到三四点然后整个人都是乱七八糟的. 不行不行我要振作起来了! 从最开始学语言到现在都快两年过去了 T T , 代码能力还是渣成这样简直不能看 T T 觉得应该学学zj爷们屯题. 加油! (题目是直接从劼很久以前的几篇屯题计划里边搬过来的我之前做过的题就删掉了 [BZOJ1822][JSOI2010]Frozen Nova 冷冻波 很水的网络流加计算几何, 然而计算几何部分非常莫名奇妙, 题目应该是判断一个圆是否与一个线

3524: [Poi2014]Couriers -- 主席树

3524: [Poi2014]Couriers Time Limit: 20 Sec  Memory Limit: 256 MB Description 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. Input 第一行两个数n,m.第二行n个数,a[i].接下来m行,每行两个数l,r,表示询问[l,r]这个区间. Output m行,每行对应一个答案. Sample

BZOJ 3524: [Poi2014]Couriers

3524: [Poi2014]Couriers Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1905  Solved: 691[Submit][Status][Discuss] Description 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. Input 第一行两个数n,m.第二行n个数,a[i].接下来m行,

BZOJ 3524: [Poi2014]Couriers( 主席树 )

卡我空间.... 这道题应该是主席树入门题...无修改 , 离散化都不用...出题人业界良心啊 一开始的空白树我 build 出来结果就多了整整 2n 个 Node , 然后就 MLE 了... ( 双倍经验 , 另一道见上图 ) ---------------------------------------------------------------------------------------------- #include<cstdio> #include<cstring&g