The Leak of The Memory in C++ 1.2

伸展树模版真的好长好长。。。

cut a b c:把第a-1个数伸展到根节点,把第b+1个数伸展到a的右子树,然后把ch[ch[root][1][0]]拿掉,放在剩下的树的第c个节点下。

flip a b:把第a-1个数伸展到根节点,把第b+1个数伸展到a的右子树,然后翻转ch[ch[root][1][0]];

由于会出现操作两边的情况,所以加了两个-1节点。

注意:

1,输出的时候要注意空格和换行。

2,在拿掉子树的时候要注意push_up();

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
#define maxn 330000
#define mem(a,b) memset(a,b,sizeof(a))
#define root10 ch[ch[root][1]][0]
#define root1 ch[root][1]
int pre[maxn],ch[maxn][2],root,tot;
int size[maxn];
int rev[maxn];
int key[maxn];
int n;
void Treaval(int x) {
    if(x) {
        Treaval(ch[x][0]);
        printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d ,key = %2d \n",x,ch[x][0],ch[x][1],pre[x],size[x],key[x]);
        Treaval(ch[x][1]);
    }
}
void debug() {printf("%d\n",root);Treaval(root);}
//以上Debug
void init()
{
    root=tot=0;
    mem(pre,0);
    mem(ch,0);
    mem(size,0);
    mem(rev,0);
}
void newnode(int &x,int k,int father)
{
    x=++tot;
    pre[x]=father;
    size[x]=1;
    ch[x][0]=ch[x][1]=0;
    rev[x]=0;
    key[x]=k;
}
void push_down(int x)
{
    if(rev[x])
    {
        rev[x]=0;
        rev[ch[x][0]]^=1;
        rev[ch[x][1]]^=1;
        swap(ch[x][0],ch[x][1]);
    }
}
void push_up(int x)
{
    size[x]=size[ch[x][0]]+size[ch[x][1]]+1;
}
void rot(int x,int kind)
{
    int y=pre[x];
    push_down(y);
    push_down(x);
    ch[y][!kind]=ch[x][kind];
    pre[ch[x][kind]]=y;
    if(pre[y])ch[pre[y]][ch[pre[y]][1]==y]=x;
    pre[x]=pre[y];
    ch[x][kind]=y;
    pre[y]=x;
    push_up(y);
    push_up(x);
}
void splay(int x,int goal)
{
    push_down(x);
    while(pre[x]!=goal)
    {
        if(pre[pre[x]]==goal)
        {
            push_down(pre[x]);
            push_down(x);
            rot(x,ch[pre[x]][0]==x);
        }
        else
        {
            int y=pre[x];
            push_down(pre[y]);
            push_down(y);
            push_down(x);
            int kind=ch[pre[y]][0]==y;
            if(ch[y][kind]==x)
            {
                rot(x,!kind);
                rot(x,kind);
            }
            else
            {
                rot(y,kind);
                rot(x,kind);
            }
        }
    }
    push_up(x);
    if(goal==0)root=x;
}
void buildtree(int &x,int l,int r,int father)
{
    if(l>r)return ;
    int mid=(l+r)/2;
    newnode(x,mid,father);
    buildtree(ch[x][0],l,mid-1,x);
    buildtree(ch[x][1],mid+1,r,x);
    push_up(x);
}
int get_kth(int x,int k)
{
    push_down(x);
    int p=size[ch[x][0]];
    if(p+1==k)return x;
    else if(k<=p)return get_kth(ch[x][0],k);
    else get_kth(ch[x][1],k-p-1);
}
int get_min(int r){
    push_down(r);
    while(ch[r][0]){
        r=ch[r][0];
        push_down(r);
    }
    return r;
}
int get_max(int r){
    push_down(r);
    while(ch[r][1]){
        r=ch[r][1];
        push_down(r);
    }
    return r;
}
void cut(int a,int b,int c)
{
    int x=get_kth(root,a);
    int y=get_kth(root,b+2);
    splay(x,0);
    splay(y,root);
    int t=root10;
    root10=0;
    push_up(root1);
    push_up(root);
    int z=get_kth(root,c+1);
    splay(z,0);
    int p=get_min(root1);
    splay(p,root);
    root10=t;
    pre[root10]=root1;
    push_up(root1);
    push_up(root);
}
void flip(int a,int b)
{
    int x=get_kth(root,a);
    int y=get_kth(root,b+2);
    splay(x,0);
    splay(y,root);
    rev[root10]^=1;
}
vector<int>vec;
void print(int x)
{
    if(x==0)return;
    push_down(x);
    print(ch[x][0]);
    if(key[x]!=-1)vec.push_back(key[x]);
    print(ch[x][1]);
}
int main()
{
    int m,a,b,c;
    char str[110];
    int i;
    while(scanf("%d%d",&n,&m)&&(n+1||m+1))
    {
        init();
        newnode(root,-1,0);
        newnode(ch[root][1],-1,root);
        size[root]=2;
        buildtree(root10,1,n,root1);
        push_up(root1);
        push_up(root);
        for(i=1;i<=m;i++)
        {
            scanf("%s",str);
            if(str[0]==‘C‘)
            {
                scanf("%d%d%d",&a,&b,&c);
                cut(a,b,c);
            }
            else
            {
                scanf("%d%d",&a,&b);
                flip(a,b);
            }
        }
        vec.clear();
        print(root);
        for(i=0;i<vec.size();i++)
        {
            cout<<vec[i];
            if(i!=vec.size()-1)cout<<" ";
            else cout<<endl;
        }
    }
    return 0;
}

The Leak of The Memory in C++ 1.2,码迷,mamicode.com

时间: 2024-08-07 18:35:53

The Leak of The Memory in C++ 1.2的相关文章

the leak of the memory in c++ 03

The Leak of the Memory in C++ In this chaper I will introduce a new smart pointer which is scoped_ptr; It likes auto_ptr but better. When peopel use auto_ptr, sometimes they forget that auto_ptr alread is empty, like this; auto_ptr<Person> p1(new Pe

Memory Leak Detection in Embedded Systems

One of the problems with developing embedded systems is the detection of memory leaks; I've found three tools that are useful for this. These tools are used to detect application program errors, not kernel memory leaks. Two of these tools (mtrace and

Linux C/C++ Memory Leak Detection Tool

目录 1. 内存使用情况分析 2. 内存泄漏(memory leak) 3. Valgrind使用 1. 内存使用情况分析 0x1: 系统总内存的分析 可以从proc目录下的meminfo文件了解到当前系统内存的使用情况汇总,其中可用的物理内存 = memfree + buffers + cached当memfree不够时,内核会通过回写机制(pdflush线程)把cached和buffered内存回写到后备存储器,从而释放相关内存供进程使用,或者通过手动方式显式释放cache内存:echo 3

b.Jre Memory Leak Prevention Listener

本文我们来分析分析应用服务器的内存泄露的问题,看看Tomcat是如何应对这个问题的: 首先,来看看内存泄露这个词,内存对于java程序来说,即指JVM内存,而我们知道JVM的内存泄露是有很多种情况的: 一种情况,class泄露是perm区的内存,此种场景就是当应用服务器的类特别多的时候,perm区的容量也是由限度的,会撑爆: 另外一种就是,java对象太多,新生代,老年代等对象存储区太多,full GC都已经收拾不了这种残局: 上述的两种情况,最终都会导致OOM溢出: 而作为Tomcat来讲,如

Identify Memory Leaks in Visual CPP Applications(VLD内存泄漏检测工具)

原文地址:http://www.codeproject.com/Articles/1045847/Identify-Memory-Leaks-in-Visual-CPP-Applications 基于CPOL License Identify Memory Leaks in Visual CPP Applications Visual Leak Detector (VLD) is an easy to use memory leak detection system. The installat

vld(Visual Leak Detector) 内存泄露检测工具

初识Visual Leak Detector 灵活自由是C/C++语言的一大特色,而这也为C/C++程序员出了一个难题.当程序越来越复 杂时,内存的管理也会变得越加复杂,稍有不慎就会出现内存问题.内存泄漏是最常见的内存问题之一.内存泄漏如果不是很严重,在短时间内对程序不会有太大的 影响,这也使得内存泄漏问题有很强的隐蔽性,不容易被发现.然而不管内存泄漏多么轻微,当程序长时间运行时,其破坏力是惊人的,从性能下降到内存耗尽,甚 至会影响到其他程序的正常运行.另外内存问题的一个共同特点是,内存问题本身

How to detect and avoid memory and resources leaks in .NET applications

Despite what a lot of people believe, it's easy to introduce memory and resources leaks in .NET applications. The Garbage Collector, or GC for close friends, is not a magician who would completely relieve you from taking care of your memory and resou

The Introduction of Java Memory Leaks

One of the most significant advantages of Java is its memory management. You simply create objects and Java Garbage Collector takes care of allocating and freeing memory. However, the situation is not as simple as that, because memory leaks frequentl

vld(Visual Leak Detector) 内存泄露检测工具,Visual C++ 2008-2015

原文: https://vld.codeplex.com/ Visual Leak Detector 是一款专用于Visual C++的内存泄漏检测工具,它免费,开源,且鲁棒性高. VLD很容易使用: 1. 安装完vld后,只要告诉Visual C++哪里去找到它的头文件和库.(下载地址:https://vld.codeplex.com) 2. 然后您只要在你的c/c++工程中加上下面这一行代码,就可以使用vld了:#include <vld.h> 您的工程在Visual Studio deb