首页 > 其他分享 >题解 ARC153B【Grid Rotations】

题解 ARC153B【Grid Rotations】

时间:2023-01-16 15:45:58浏览次数:57  
标签:rt sz lc int 题解 Rotations pc Grid rc

一次操作是把四个子矩形分别旋转 \(180^\circ\)。不难发现,可以横纵坐标分别考虑,则该操作变为横坐标的两段区间分别翻转、纵坐标的两段区间分别翻转。

区间翻转操作、最后输出数列是 文艺平衡树 的基本操作,搞两棵平衡树维护即可。

时间复杂度 \(\Theta(nm+(n+q)\log n+(m+q)\log m)\)。

实际上有解法可以做到 \(\Theta(nm+q)\),但本解法足以通过本题。

// Problem: B - Grid Rotations
// Contest: AtCoder - AtCoder Regular Contest 153
// URL: https://atcoder.jp/contests/arc153/tasks/arc153_b
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//By: OIer rui_er
#include <bits/stdc++.h>
#define rep(x,y,z) for(int x=(y);x<=(z);x++)
#define per(x,y,z) for(int x=(y);x>=(z);x--)
#define debug(format...) fprintf(stderr, format)
#define fileIO(s) do{freopen(s".in","r",stdin);freopen(s".out","w",stdout);}while(false)
using namespace std;
typedef long long ll;
const int N = 5e5+5;

int n, m, q;
vector<int> ppr, ppc;
vector<string> s;
template<typename T> void chkmin(T& x, T y) {if(x > y) x = y;}
template<typename T> void chkmax(T& x, T y) {if(x < y) x = y;}
struct Node {
	int val, rnd, sz, lc, rc, rev;
	Node(int a=0, int b=0) : val(a), rnd(rand()), sz(b), lc(0), rc(0), rev(0) {}
	~Node() {}
};
struct FHQTreap {
	Node t[N];
	int sz, rt, L, M, R;
	int newNode(int x) {
		t[++sz] = Node(x, 1);
		return sz;
	}
	void pushup(int u) {
		t[u].sz = t[t[u].lc].sz + t[t[u].rc].sz + 1;
	}
	void pushdown(int u) {
		if(!t[u].rev) return;
		if(t[u].lc) t[t[u].lc].rev ^= 1;
		if(t[u].rc) t[t[u].rc].rev ^= 1;
		swap(t[u].lc, t[u].rc);
		t[u].rev = 0; 
	}
	void split(int u, int lim, int& x, int& y) {
		if(!u) x = y = 0;
		else {
			pushdown(u);
			if(t[t[u].lc].sz < lim) {
				x = u;
				split(t[u].rc, lim-t[t[u].lc].sz-1, t[u].rc, y);
			}
			else {
				y = u;
				split(t[u].lc, lim, x, t[u].lc);
			}
			pushup(u);
		}
	}
	int merge(int u, int v) {
		if(!u || !v) return u | v;
		if(t[u].rnd < t[v].rnd) {
			pushdown(u);
			t[u].rc = merge(t[u].rc, v);
			pushup(u);
			return u;
		}
		else {
			pushdown(v);
			t[v].lc = merge(u, t[v].lc);
			pushup(v);
			return v;
		}
	}
	void reverse(int l, int r) {
		split(rt, l-1, L, R);
		split(R, r-l+1, M, R);
		t[M].rev ^= 1;
		rt = merge(L, merge(M, R));
	}
	void get(int u, vector<int>& a) {
		pushdown(u);
		if(t[u].lc) get(t[u].lc, a);
		a.push_back(t[u].val);
		if(t[u].rc) get(t[u].rc, a);
	}
}pr, pc;

int main() {
	scanf("%d%d", &n, &m);
	s.resize(n);
	rep(i, 0, n-1) cin>>s[i];
	rep(i, 0, n-1) pr.rt = pr.merge(pr.rt, pr.newNode(i));
	rep(j, 0, m-1) pc.rt = pc.merge(pc.rt, pc.newNode(j));
	for(scanf("%d", &q); q; q--) {
		int x, y;
		scanf("%d%d", &x, &y);
		pr.reverse(1, x); pr.reverse(x+1, n);
		pc.reverse(1, y); pc.reverse(y+1, m);
	}
	pr.get(pr.rt, ppr);
	pc.get(pc.rt, ppc);
	rep(i, 0, n-1) {
		rep(j, 0, m-1) putchar(s[ppr[i]][ppc[j]]);
		puts("");
	}
	return 0;
}

标签:rt,sz,lc,int,题解,Rotations,pc,Grid,rc
From: https://www.cnblogs.com/ruierqwq/p/arc153b.html

相关文章

  • CF1133B 题解
    原题链接这道题其实很简单要让两数和为\(k\)的倍数,需要满足以下两条件之一:两数都是\(k\)的倍数两数的余数和为\(k\)因此,我们可以先统计出余数再按上述条......
  • delphi 在cxgrid表中设置下拉菜单,以及更新多表引用的数据表
    我写的博客内容,都是在实际生产中遇到的问题,针对性很强,记录下来有两个目的,一是当成笔记,二是丰富Delphi的网上资料,让遇到相同问题的朋友,少走弯路. 如下图,我希望所......
  • AtCoder Beginner Contest 285 题解
    比赛链接:https://atcoder.jp/contests/abc285总体来说不算难。A-C略。\(D\)因为起点终点不同,起点之间、终点之间两两不同,所以有环的情况是错的,其他都是对的。写起来的......
  • 6.PyQt5【布局组件】网格布局-QGridLayout
    一、前言本节我们介绍布局组件中的网格布局QGridLayout。二、学习目标1.QGridLayout网格布局的应用三、知识点1.【QGridLayout网格布局的应用】网格布局也称栅格布局......
  • P3524 [POI2011]IMP-Party 题解
    题目传送门更好的阅读体验前置芝士团设\(V\)为\(G\)子图,当\(V\)中任意两点都有边相连,则\(V\)为\(G\)的一个团。此图为本题样例最大团:\(\{1,3,4,5\}\)......
  • Winform DataGridViewTextBoxCell 编辑添加右键菜单,编辑选中文本
    如上是我们使用DataGridView时,编辑单元格右键会出现系统菜单。现在我们添加自己的右键菜单,并可以操作选中文字。DataGridViewTextBoxCell:DataGridViewTextBoxCell类是......
  • Atcoder ARC 061 题解
    C-ManyFormulas题意​ 给出一个长度为10的由数字组成的字符串,你可以把'+'插入到任意位置,将字符串分割,形成一个算式。你有很多分割的方案,现在你需要将所有出现的算式的......
  • 寒假第二周训练部分题解代码
    1周一1.1C输入两个数a,b表示a和b信仰同一个宗教。求有多少种不同的宗教信仰。先看第二个样例10423454858画图可得:![[Pastedimage20230115154805.png]]......
  • P8944 题解
    非矩乘做法。理论上常数应该小点,但没跑进最优解第一页。可以当个好写做法看。首先发现变换后的答案分布仅与变换前的答案分布有关。所以来研究一次变换中单点的变化。设......
  • 【题解】P6578 [Ynoi2019] 魔法少女网站
    卡了一晚上终于过了。好家伙,又是想题想一半不会是吧,小垃圾是不是想退役/fad小黑子->小垃圾->垃圾酱->垃圾摇滚/xk但是真的有垃圾摇滚这东西/kk思路操作分块......