[SCOI2014]方伯伯的OJ,洛谷P3285,splay

本文介绍了一道在线竞技编程题目解决方案,利用伸展树动态开点技巧处理强制在线修改操作问题。通过将排名理解为权值,并采用map进行映射,实现了对节点的有效管理和更新。

正题

      方伯伯为什么要搞一个OJ呢emm

      强制在线又要加修改操作,明显想到伸展树动态开点。

      把编号理解成权值。

      一开始所有的权值就是排名。

     我们把排名连续且权值连续的看作为一个点(要是要改权值那就拿两个map映射一下)。

     每次有k操作就把一个点拆成[1,k-1],[k,k],[k+1,n]那么最多就有3m个点不会爆空间。

     同时我们要维护一个点在哪个区间,用set记录即可。

     可是无辜T了6个点,暂时想不到什么优化,留坑。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<set>
#include<map>
using namespace std;

int n,m;
struct node{
    int y,d;
    bool operator<(node q)const{
        return y<q.y;
    }
};
set<node> f;
set<node>::iterator it;
map<int , int> image,fact;
map<int , int>::iterator p;
struct tree{
    int son[2],f,t;
    int l,r;
}s[1000010];
int root=1;
int tot=1;
int k;

void update(int x){
    s[x].t=s[s[x].son[0]].t+s[s[x].son[1]].t+s[x].r-s[x].l+1;
}

int findsomething(int x){
    p=image.lower_bound(x);
    if(p==image.end() || p->first!=x)
        k=x;
    else k=p->second;
    it=f.lower_bound((node){k,0});
    return it->d;
}

void rotate(int x,int w){
    int f=s[x].f,ff=s[f].f;
    s[f].son[1-w]=s[x].son[w];
    if(s[x].son[w]!=0) s[s[x].son[w]].f=f;
    if(s[ff].son[0]==f) s[ff].son[0]=x;
    else s[ff].son[1]=x;
    s[x].f=ff;
    s[f].f=x;
    s[x].son[w]=f;
    update(f);
    update(x);
}

void splay(int x,int tar){
    while(s[x].f!=tar){
        int f=s[x].f,ff=s[f].f;
        if(ff==tar){
            if(s[f].son[0]==x) rotate(x,1);
            else rotate(x,0);
        }
        else{
            if(s[ff].son[0]==f){
                if(s[f].son[0]==x) {rotate(f,1);rotate(x,1);}
                else {rotate(x,0);rotate(x,1);}
            }
            else{
                if(s[f].son[0]==x) {rotate(x,1);rotate(x,0);}
                else {rotate(f,0);rotate(x,0);}
            }
        }
    }
    if(tar==0) root=x;
}

int get_tot(int x){
    splay(x,0);
    return s[s[x].son[0]].t+k-s[x].l+1;
}

void del(int x){
    int ip=x;
    if(s[ip].son[0]==0 && s[ip].son[1]==0) root=0;
    else if(s[ip].son[0]!=0 && s[ip].son[1]==0) {root=s[ip].son[0];s[s[ip].son[0]].f=0;}
    else if(s[ip].son[0]==0 && s[ip].son[1]!=0) {root=s[ip].son[1];s[s[ip].son[1]].f=0;}
    else{
        int p=s[ip].son[0];
        while(s[p].son[1]!=0) p=s[p].son[1];
        splay(p,ip);
        root=p;s[p].f=0;
        s[s[ip].son[1]].f=p;
        s[p].son[1]=s[ip].son[1];
        update(p);
    }
}

void add(int x){
    if(s[x].l<k && k<s[x].r){
        tot++;
        s[tot].l=k+1;s[tot].r=s[x].r;
        s[x].r=k-1;
        s[tot].son[1]=s[x].son[1];s[x].son[1]=tot;
        s[tot].f=x;if(s[tot].son[1]!=0) s[s[tot].son[1]].f=tot;
        update(tot);
        update(x);
        f.insert((node){s[tot].r,tot});
    }
    else if(k==s[x].r && s[x].l<k){
        s[x].r=k-1;
        update(x);
    }
    else if(k==s[x].l && k<s[x].r){
        s[x].l=k+1;
        update(x);
    }
    else if(s[x].l==k && k==s[x].r){
        del(x);
        return ;
    }
    f.insert((node){s[x].r,x});
}

void put_top(int x){
    splay(x,0);
    f.erase(it);
    add(x);
    if(root==0) {
    	s[x].l=s[x].r=k;
        root=x;update(x);
        f.insert((node){s[x].r,x});
    }
   	else{
    	tot++;
    	s[tot].l=s[tot].r=k;
    	s[tot].son[0]=s[tot].son[1]=0;
   		int now=root;
   		while(s[now].son[0]!=0) now=s[now].son[0];
   		s[now].son[0]=tot;
        s[tot].f=now;
    	f.insert((node){s[tot].r,tot});
    	update(tot);
    	update(now);
    	while(s[now].f!=0) {now=s[now].f;update(now);}
    }
}

void put_end(int x){
    splay(x,0);
    f.erase(it);
    add(x);
    tot++;
    if(root==0) {
    	s[x].l=s[x].r=k;
        root=x;update(x);
        f.insert((node){s[x].r,x});
    }
   	else{
    	tot++;
    	s[tot].l=s[tot].r=k;
    	s[tot].son[0]=s[tot].son[1]=0;
   		int now=root;
   		while(s[now].son[1]!=0) now=s[now].son[1];
   		s[now].son[1]=tot;
        s[tot].f=now;
    	f.insert((node){s[tot].r,tot});
    	update(tot);
    	update(now);
    	while(s[now].f!=0) {now=s[now].f;update(now);}
    }
}

int find_num(int x){
    int now=root;
    while(1){
        if(x<=s[s[now].son[0]].t) now=s[now].son[0];
        else if(x>s[s[now].son[0]].t+s[now].r-s[now].l+1) x-=s[s[now].son[0]].t+s[now].r-s[now].l+1,now=s[now].son[1];
        else break;
    }
    return x-s[s[now].son[0]].t+s[now].l-1;
}

int main(){
    scanf("%d %d",&n,&m);
    f.insert((node){n,1});
    s[1].son[0]=s[1].son[1]=0;
    s[1].f=0;s[1].t=n;
    s[1].l=1,s[1].r=n;
    s[0].t=0;
    int t,x,y;
    int op;
    int last=0;
    for(int i=1;i<=m;i++){
        scanf("%d %d",&t,&x);
        if(t==1) scanf("%d",&y);	
        x-=last,y-=last;
        if(t==1){
            op=findsomething(x);
            last=get_tot(op);
            printf("%d\n",last);
            image[y]=k;fact[k]=y;
            image.erase(x);
        }
        else if(t==2){
            op=findsomething(x);
            last=get_tot(op);
            printf("%d\n",last);
            put_top(op);
        }
        else if(t==3){
            op=findsomething(x);
            last=get_tot(op);
            printf("%d\n",last);
            put_end(op);
        }
        else if(t==4){
            last=find_num(x);
            p=fact.lower_bound(last);
            if(p==fact.end() || p->first!=last);
            else last=p->second;
            printf("%d\n",last);
        }
    }
}

### 解题思路 洛谷 P4160 [SCOI2009] 生日快乐 这道题的核心在于递归与分治策略。题目要求将一个矩形蛋糕切成若干块,使得每一块的长宽比的最大值最小。由于每一块的长宽比是独立的,因此可以通过递归的法,将问题分解为子问题来求解。 #### 核心思路: 1. **递归切分**:每次将蛋糕分成两部分,并递归地对这两部分进行同样的操作,直到只剩一块为止。 2. **枚举切分式**:对于每一层递归,需要枚举所有可能的切分式(横向或纵向),以及每一块的大小比例。 3. **取最大值与最小值**:每一步递归中,选择切分式使得两部分的最大长宽比尽可能小。 #### 关键点: - **长宽比处理**:为了保证长宽比的计算准确,需要确保长边在分子,短边在分母。 - **切分式枚举**:枚举所有可能的切分比例,确保没有遗漏。 - **递归终止条件**:当只剩一块时,直接返回当前长宽比。 ### 代码示例 以下是一个完整的代码实现,展示了如何通过递归法解决这个问题: ```cpp #include <iostream> #include <cstdio> #include <algorithm> using namespace std; int x, y, n; // 计算最大公约数 int gcd(int x, int y) { if (y == 0) return x; return gcd(y, x % y); } // 递归函数,用于计算最小的长宽比 double qie(int x, int y, int n) { if (x < y) swap(x, y); // 保证x是较长边 int g = gcd(x, y); if (g != 1) { x /= g; y /= g; } if (n == 1) return static_cast<double>(x) / y; // 终止条件 double ans = 10000000; for (int i = 1; i < n; ++i) { // 横向切分 ans = min(ans, max(qie(x * i, y * n, i), qie(x * (n - i), y * n, n - i))); // 纵向切分 ans = min(ans, max(qie(x * n, y * i, i), qie(x * n, y * (n - i), n - i))); } return ans; } int main() { scanf("%d%d%d", &x, &y, &n); printf("%.6lf\n", qie(x, y, n)); return 0; } ``` ### 代码解析 1. **gcd函数**:用于化简长宽比,避免浮点数计算误差。 2. **qie函数**: - 首先交换长宽,确保长边在前。 - 化简长宽比的分数,避免重复计算。 - 递归终止条件:当只剩一块时,返回长宽比。 - 枚举所有可能的切分式,取最小的长宽比。 3. **main函数**:读取输入并调用递归函数,输出结果。 ### 复杂度分析 - **时间复杂度**:由于每次递归会枚举所有可能的切分式,时间复杂度为指数级,但由于数据范围较小,可以通过递归直接解决。 - **空间复杂度**:递归深度由切分次数决定,空间复杂度较低。 ### 总结 这道题通过递归的式,将大问题分解为子问题,结合枚举所有可能的切分式,最终找到最优解。递归与分治策略是解决此类问题的核心思想。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值