首页 > 其他分享 >分治理 + 模拟退火(因为博客归档有问题,这两个就放一起了,以后有机会搬一下)

分治理 + 模拟退火(因为博客归档有问题,这两个就放一起了,以后有机会搬一下)

时间:2022-10-15 07:11:05浏览次数:86  
标签:rt int top 博客 read 模拟退火 归档 heigh define

分治

本篇重点讲三个东西,线段树分治,点分治,以及CDQ分治。

TOP1 线段树分治

  • 这个算法主要是针对于一些对在线算法很不友好的题,其模型大概是维护一张图,其中的边在某个固定时刻存在,其余时刻不存在,并在某些时刻让你针对于那个时刻的图做一些询问。
  • 确实一听就很恶心,你总不能全靠\(LCT\)
  • 所以这个基于时间轴来分治的线段树分治就有了,其实如果看过线段树优化建图的话,这个就非常好理解了,具体来讲就是把操作和询问离线下来,对于每条边所覆盖的时间区间打上标记,在从根向那个时间搜的时候,一边dfs一边就把当时有的边建了出来,在搜到叶子节点时处理答案,回溯的时候将建的边撤销,这时就得用上可撤销并查集了(其实就是不路径压缩的按秩合并存一下连边前状态)。然后这个线段树分治就做完了。
  • 当然,我们离线下来的轴不一定是时间,类似的只要是基于某个轴的操作都可以进行。
  • 例题推荐的话P5787
    这个针灸纯板子,就是用了一个扩展域并查集查二分图的\(trick\)
here


#include <bits/stdc++.h>


#define LL long long
#define Re register int
#define LD double
#define mes(x, y) memset(x, y, sizeof(x))
#define cpt(x, y) memcpy(x, y, sizeof(x))
#define fuc(x, y) inline x y
#define fr(x, y, z)for(Re x = y; x <= z; ++ x)
#define fp(x, y, z)for(Re x = y; x >= z; -- x)
#define delfr(x, y, z)for(Re x = y; x < z; ++ x)
#define delfp(x, y, z)for(Re x = y; x > z; -- x)
#define frein(x) freopen(#x ".in", "r", stdin)
#define freout(x) freopen(#x ".out", "w", stdout)
#define ki putchar('\n')
#define fk putchar(' ')
#define WMX aiaiaiai~~
#define mk(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define re(x) return x
#define sec second
#define fst first
using namespace std;
namespace kiritokazuto{
    auto read = [](){
        LL x = 0;
        int f = 1;
        char c;
        while (!isdigit(c = getchar())){ if (c == '-')f = -1; }
        do{ x = (x << 1) + (x << 3) + (c ^ 48); } while (isdigit(c = getchar()));
        return x * f;
    };
    template <typename T> fuc(void, write) (T x){
        if (x < 0)putchar('-'), x = -x;
        if (x > 9)write(x / 10);putchar(x % 10 | '0');
    }

}

using namespace kiritokazuto;
const int maxn = 2e6 + 10, Inf = 0x3f3f3f3f, Mod = 5000007;


int n, m, k;
int heigh[maxn << 1];
struct EDGE{int from, to;}wmx[maxn];
struct STK{int x, y, hig;}stk[maxn];
int top = 0;
int fa[maxn << 1];
struct Set_Union {
    fuc(void, init)(){fr(i, 1, n << 1)fa[i] = i, heigh[i] = 1;}
    fuc(int , find)(int x) {while(x != fa[x])x = fa[x]; return x;}
    fuc(void, merge)(int x, int y) {
        x = find(x), y = find(y);
        if(heigh[x] > heigh[y])swap(x, y);
        fa[x] = y;
        stk[++top] = (STK){x, y, heigh[x] == heigh[y]};
        heigh[y] += (heigh[x] == heigh[y]);
    }
}F;
struct Seg_Tree {
    #define lsp(rt) (rt << 1)
    #define rsp(rt) (rt << 1 | 1)
    #define mid ((l + r) >> 1)
    vector <int> vec[maxn];
    fuc(void, insert)(int rt, int l, int r, int L, int R, int val) {
        if(L <= l && r <= R) {return vec[rt].pb(val), void();}
        if(L <= mid)insert(lsp(rt), l, mid, L, R, val);
        if(R > mid)insert(rsp(rt), mid + 1, r, L, R, val);
    }
    fuc(void, query)(int rt, int l, int r) {
        int is = 1, sz = vec[rt].size();
        int lst = top;

        delfr(i, 0, sz) {
            int pos = vec[rt][i];
            int from = wmx[pos].from, to = wmx[pos].to;
            int ffrom = F.find(from), fto = F.find(to);
            if(ffrom != fto) {
                F.merge(from, to + n);
                F.merge(to, from + n);
            }else {
                is = 0;
                fr(j, l, r) {
                    puts("No");
                }
                break;
            }
        }
        if(is && l == r) puts("Yes");
        if(is && l != r) query(lsp(rt), l, mid), query(rsp(rt), mid + 1, r);
        while(top > lst) {
            int x = stk[top].x;
            int y = stk[top].y;
            heigh[y] -= stk[top].hig;
            fa[x] = x;
            top--;
        }
    }
}T;

signed main(){
    n = read(), m = read(), k = read();
    F.init();
    fr(i, 1, m) {
        int x = read(), y = read(), l = read(), r = read();
        wmx[i].from = x, wmx[i].to = y;
        T.insert(1, 1, k, l + 1, r, i);//有0时刻没用
    }
    T.query(1, 1, k);
    
}

    
  • 其余的还有地理课,\(No Rest for the Wicked\) 等

标签:rt,int,top,博客,read,模拟退火,归档,heigh,define
From: https://www.cnblogs.com/kiritokazuto/p/16793516.html

相关文章

  • 博客园添加导航目录
    本文转载自:https://blog.csdn.net/q913777031/article/details/113103404,有修改。实现的效果可以实现h1-h6标签级别的目录。点击“目录导航”按钮可以展开目录,再次点......
  • 博客导航
    前言这篇博客整理了机器学习系统相关的博客,一方面是为了方便自己和读者查阅文章,另一方面这个手动整理的目录是一个学习路线。如果您对机器学习系统感兴趣,那么希望我的这个......
  • 我的第一篇博客
    #我的第一篇博客##自我介绍自我介绍一下,我是某二本的一名研究生,算是交叉领域吧,本科学的是物联网工程,研究生方向初步为计算机视觉(cv)。一直想要把自己的学习路程和经验记......
  • 博客园xTypora的使用|快捷发布(免github)
    1.下载安装Typorahttps://pan.baidu.com/s/1ITC9OjBMrXkLtCKrKVpZrQ提取码:k7412.对Typora进行设置3.下载安装dotnet(1)下载安装.NETCoreSDK:https://www.mic......
  • 在vscode中使用博客园插件
    在vscode中使用博客园插件自动上传本地博客(1)前言好记性不如烂笔头。千古流传的经验之谈,绝不是浪得虚名。记忆是最最会骗人的东西,又是最容易被遗忘的东西。过河拆桥,......
  • 使用 HammerDB 对 Citus 和 Postgres 进行 Benchmark,每分钟200万新订单处理测试(官方
    在为​​Postgres​​运行性能基准测试时,主要建议是:“自动化!”如果您正在测量数据库性能,您可能不得不一遍又一遍地运行相同的基准测试。要么是因为你想要一个稍微不同的......
  • 博客索引
    由于这个\(css\)的某些缺陷,它吞我文章,所以就写了个这个题解CSP模拟18CSP模拟17CSP模拟14CSP模拟13CSP模拟12CSP模拟11CSP模拟10CSP模拟9CSP模拟8暑假集训1暑......
  • 达梦定时备份归档及清除
    1启动归档alterdatabasemount;alterdatabaseaddarchivelog'dest=/dmdata/arch,TYPE=local,FILE_SIZE=1024,SPACE_LIMIT=40000';alterdatabasearchivelog;alter......
  • 博客园图片无法显示
    今天粉丝反馈博客图片无法显示,看了一下,图床是可以正常访问的,不知道什么原因,后来看了博客园后台编辑博文处有一个提取图片的功能,非常好用,可以将图床上的图片全部提取到博客......
  • 关于博客密码
    博客密码是我的生日!!!因为考虑到题面要保密,所以加了密码不知道密码可以看我题库用户名要是还不知道友尽吧......