惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
博客园 - 三生石上(FineUI控件)
T
The Blog of Author Tim Ferriss
宝玉的分享
宝玉的分享
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
V
Visual Studio Blog
小众软件
小众软件
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
MongoDB | Blog
MongoDB | Blog
V
V2EX
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 【当耐特】
Microsoft Azure Blog
Microsoft Azure Blog
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Engineering at Meta
Engineering at Meta
L
LangChain Blog
Martin Fowler
Martin Fowler
GbyAI
GbyAI
博客园 - 司徒正美

某岛

AtCoder Beginner Contest 409 Luogu P5325. 【模板】Min_25 筛 UOJ #188. 【UR #13】Sanrd AtCoder Beginner Contest 371 AtCoder Beginner Contest 369 RPGMaker 2k3 百科 OneShot 的考古 2024“开创拓芯”游戏创享节的相关记录 CJ 回来后的戒断反应 Luogu P10221. [省选联考 2024] 重塑时光 Luogu P5308 [COCI2018-2019#4] Akvizna wqs 二分 歌唱王国 Lean 相关 BZOJ 3153. Sone1 The 2023 ICPC World Finals Luxor 新巴别塔 Sora 的想象与思考 Facebook Hacker Cup 2023 Round 1 AtCoder Beginner Contest 322 LLaMA 2 相关 HuggingFace AI Game Jam ACL 2023 Trans 相关… Luogu P2053. [SCOI2007] 修车 Luogu P1973. [NOI2011] NOI 嘉年华 Luogu P1933. [NOI2010] 旅行路线 Luogu P1954. [NOI2010] 航空管制 Luogu P2048. [NOI2010] 超级钢琴 Luogu P2046. [NOI2010] 海拔
Luogu P2086. [NOI2012] 魔幻棋盘
2023-06-09 · via 某岛

差分维护 gcd 是一个很常见的技巧了。。
先写个线段树拿点部分分吧。。。(然后发现讨论 n,m 大小。。暴力开 o2 能过。)

#include <lastweapon/io>
// #include <lastweapon/segtree>
using namespace lastweapon;

const int N = int(5e5)+9;

/*
LL gcd(LL a,LL b)
{
    return b?gcd(b,a%b):abs(a);
}*/

inline LL gcd(LL a,LL b)
{
	if(a<0) a=-a;
	if(b<0) b=-b;
	if(!a) return b;
	if(!b) return a;
	LL t=__builtin_ctzll(a|b),tmp;
	a>>=__builtin_ctzll(a);
	do
	{
		b>>=__builtin_ctzll(b);
		if(a>b)
		{
			tmp=a,a=b,b=tmp;
		}
		b-=a;
	} while(b);
	return a<<t;
}

LL op(LL a, LL b) { return gcd(a, b); }
LL e() { return 0; }
int m, n;

const int TN = 4*N, M = 710;


#define lx (x<<1)
#define rx (lx|1)
#define ml ((l+r)>>1)
#define mr (ml+1)
#define lc lx,l,ml
#define rc rx,mr,r
#define rt 1,0,n-1

struct SegTree{

    vector<LL> T;

    void add(int x, LL d) {
        T[x] += d;
    }

    void Build(int x, int l, int r, vector<LL> &a) {
        if (l < r) {
            T[x] = 0;
            Build(lc, a), Build(rc, a);
        } else {
            T[x] = a[l];
        }
    }

    LL Query(int x, int l, int r, const int p) {
        if (l == r) {
            return T[x];
        } else {
            return T[x] + (p < mr ? Query(lc, p) : Query(rc, p));
        }
    }

    void Add(int x, int l, int r, const int a, const int b, const LL d) {
        if (b < l || r < a) return;
        if (a <= l && r <= b) {
            add(x, d);
        } else {
            Add(lc, a, b, d); Add(rc, a, b, d);
        }
    }
} S[M];


struct SegTree2{

    vector<LL> T;

    void upd(int x) {
        T[x] = gcd(T[lx], T[rx]);
    }

    void add(int x, LL d) {
        T[x] += d;
    }

    void Build(int x, int l, int r, vector<LL> &a) {
        if (l < r) {
            Build(lc, a), Build(rc, a);
            upd(x);
        } else {
            T[x] = a[l];
        }
    }

    LL Query(int x, int l, int r, const int a, const int b) {
        if (b < l || r < a) return 0;
        if (a <= l && r <= b) {
            return T[x];
        } else {
            return gcd(Query(lc, a, b), Query(rc, a, b));
        }
    }

    void Add(int x, int l, int r, const int p, const LL d) {
        if (l == r) {
            add(x, d);
        } else {
            if (p < mr) Add(lc, p, d);
            else Add(rc, p, d);
            upd(x);
        }
    }
} T[M];

bool flip;

int main() {

#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif

    RD(m, n); // if (m != 1) return 0;
    int y0, x0; RD(y0, x0); --x0; --y0; int Q; RD(Q);

    if (m > n) {
        swap(m, n);
        flip = 1;
    }

    vector<vector<LL>> a, d; a.resize(m); d.resize(m);

    if (flip) {
        REP(i, n) REP(j, m) a[j].PB(RD());
    } else {
        REP(j, m) REP(i, n) a[j].PB(RD());
    }

    REP(j, m) {
        REP(i, n-1) d[j].PB(a[j][i+1] - a[j][i]);
        d[j].PB(0);
    }

    REP(i, m) S[i].T.resize(4*n), S[i].Build(rt, a[i]);
    REP(i, m) T[i].T.resize(4*n), T[i].Build(rt, d[i]);

    DO(Q) {

        int cmd, u, l, d, r; RD(cmd, u, l, d, r);

        if (!cmd) {
            u = y0 - u; d = y0 + d; l = x0 - l; r = x0 + r;
            if (flip) swap(u, l), swap(d, r);

            LL z = 0; FOR_1(i, u, d) {
                z = gcd(z, gcd(S[i].Query(rt, l), l < r ? T[i].Query(rt, l, r-1) : 0));
            }
            printf("%lld\n", abs(z));
        } else {
            LL delta; RD(delta); //continue;
            --l; --r; --u; --d; if (flip) swap(u, l), swap(d, r);

            FOR_1(i, u, d) {
                S[i].Add(rt, l, r, delta);
                if (l) T[i].Add(rt, l-1, delta);
                T[i].Add(rt, r, -delta);
            }
        }
    }
}

Posted by xiaodao
Category: 日常