题意:n*n的矩阵,m次赋值一个子矩阵为c,最后输出答案。

n<=1e3 m<=1e5

解:倒序处理。

拆行处理。

每行内并查集维护未被赋值的地方。

这样每个地方最多被赋值一次,每次修改要访问n行,时间复杂度是O(n(n + m))

 #include <cstdio>

 inline void read(int &x) {
char c = getchar();
x = ;
while(c < '' || c > '') {
c = getchar();
}
while(c >= '' && c <= '') {
x = (x << ) + (x << ) + c - ;
c = getchar();
}
return;
} const int N = , M = ; struct Node {
int opt, c, x, y, xx, yy;
}a[M]; int n, k, m, fa[M], sv[M], num, path[M], top;
char s[]; struct ROW {
int a[N], fa[N];
ROW() {
for(int i = ; i < N; i++) {
fa[i] = i;
a[i] = ;
}
}
int find(int x) {
if(x == fa[x]) {
return x;
}
return fa[x] = find(fa[x]);
}
inline void change(int v, int l, int r) {
int p = find(r);
while(p >= l) {
a[p] = v;
fa[p] = find(p - );
p = fa[p];
}
return;
}
}row1[N], row2[N]; int main() { read(n);
read(k);
read(m); for(int i = ; i <= m; i++) {
scanf("%s", s);
if(s[] == 'P') { // print
fa[i] = i - ;
a[i].opt = ;
read(a[i].c);
read(a[i].x);
read(a[i].y);
read(a[i].xx);
read(a[i].yy);
}
else if(s[] == 'S') { // save
fa[i] = i - ;
sv[++num] = i;
}
else { // load
int x;
//scanf("%d", &x);
read(x);
fa[i] = sv[x];
}
} int x = m;
while(x) {
path[++top] = x;
x = fa[x];
} for(int e = ; e <= top; e++) {
int i = path[e];
if(!a[i].opt) {
continue;
} for(int j = a[i].x + ; j <= a[i].xx + ; j++) {
//printf(" j = %d c = %d \n", j, a[i].c);
if((a[i].x - + a[i].y) & ) {
row1[j].change(a[i].c, a[i].y + , a[i].yy + );
}
else {
row2[j].change(a[i].c, a[i].y + , a[i].yy + );
}
}
} for(int i = ; i <= n; i++) {
for(int j = ; j <= n; j++) {
if((i + j) & ) {
printf("%d ", row2[i].a[j]);
}
else {
printf("%d ", row1[i].a[j]);
}
}
puts("");
}
return ;
}

AC代码

说一下原来的题意:

有个矩阵,每次间隔染色一个子矩阵(类似国际象棋那样,左上角要染),还要支持存档/读档。最后一次输出。

 #include <cstdio>
#include <algorithm> inline void read(int &x) {
char c = getchar();
x = ;
while(c < '' || c > '') {
c = getchar();
}
while(c >= '' && c <= '') {
x = (x << ) + (x << ) + c - ;
c = getchar();
}
return;
} const int N = , M = ; struct Node {
int opt, c, x, y, xx, yy;
}a[M]; int n, k, m, fa[M], sv[M], num, path[M], top;
char s[];
int sa[N * N * ], sb[N * N * ], sc[N * N * ], sd[N * N * ];
int tag1[N * N * ], tag2[N * N * ], G[N][N]; void build(int x, int y, int xx, int yy, int o) {
if(x == xx && y == yy) {
tag1[o] = ;
return;
}
int mx = (x + xx) >> ;
int my = (y + yy) >> ; sa[o] = ++num;
build(x, y, mx, my, num); if(yy > y) {
sb[o] = ++num;
build(x, my + , mx, yy, num);
}
if(xx > x) {
sc[o] = ++num;
build(mx + , y, xx, my, num);
} if(yy > y && xx > x) {
sd[o] = ++num;
build(mx +, my + , xx, yy, num);
}
return;
} inline void pushdown(int x, int y, int xx, int yy, int o) {
int mx = (x + xx) >> ;
int my = (y + yy) >> ;
if(tag1[o]) {
int t = tag1[o];
if(sa[o]) {
tag1[sa[o]] = t;
}
if(sb[o]) {
if((my - y + ) & ) {
tag2[sb[o]] = t;
}
else {
tag1[sb[o]] = t;
}
}
if(sc[o]) {
if((mx - x + ) & ) {
tag2[sc[o]] = t;
}
else {
tag1[sc[o]] = t;
}
}
if(sd[o]) {
if((mx - x + my - y + ) & ) {
tag1[sd[o]] = t;
}
else {
tag2[sd[o]] = t;
}
}
tag1[o] = ;
}
if(tag2[o]) {
int t = tag2[o];
if(sa[o]) {
tag2[sa[o]] = t;
}
if(sb[o]) {
if((my - y + ) & ) {
tag1[sb[o]] = t;
}
else {
tag2[sb[o]] = t;
}
}
if(sc[o]) {
if((mx - x + ) & ) {
tag1[sc[o]] = t;
}
else {
tag2[sc[o]] = t;
}
}
if(sd[o]) {
if((mx - x + my - y + ) & ) {
tag2[sd[o]] = t;
}
else {
tag1[sd[o]] = t;
}
}
tag2[o] = ;
}
return;
} int X, XX, Y, YY;
inline void change(int v, int f, int x, int y, int xx, int yy, int o) {
int mx = (x + xx) >> ;
int my = (y + yy) >> ;
if(X <= x && xx <= XX) {
if(Y <= y && yy <= YY) {
if(f) {
tag1[o] = v;
}
else {
tag2[o] = v;
}
return;
}
}
pushdown(x, y, xx, yy, o);
if(X <= mx && Y <= my) { // sa
change(v, f, x, y, mx, my, sa[o]);
}
if(X <= mx && my < YY) { // sb
if((my - y + ) & ) {
change(v, f ^ , x, my + , mx, yy, sb[o]);
}
else {
change(v, f, x, my + , mx, yy, sb[o]);
}
}
if(mx < XX && Y <= my) { // sc
if((mx - x + ) & ) {
change(v, f ^ , mx + , y, xx, my, sc[o]);
}
else {
change(v, f, mx + , y, xx, my, sc[o]);
}
}
if(mx < XX && my < YY) { // sd
if((mx - x + my - y + ) & ) {
change(v, f, mx + , my + , xx, yy, sd[o]);
}
else {
change(v, f ^ , mx + , my + , xx, yy, sd[o]);
}
}
return;
} void ed(int x, int y, int xx, int yy, int o) {
if(x == xx && y == yy) {
G[x][y] = tag1[o];
return;
}
int mx = (x + xx) >> ;
int my = (y + yy) >> ;
pushdown(x, y, xx, yy, o);
ed(x, y, mx, my, sa[o]);
if(yy > y) {
ed(x, my + , mx, yy, sb[o]);
}
if(xx > x) {
ed(mx + , y, xx, my, sc[o]);
}
if(xx > x && yy > y) {
ed(mx + , my + , xx, yy, sd[o]);
}
return;
} int main() { read(n);
read(k);
read(m); for(int i = ; i <= m; i++) {
scanf("%s", s);
if(s[] == 'P') { // print
fa[i] = i - ;
a[i].opt = ;
read(a[i].c);
read(a[i].x);
read(a[i].y);
read(a[i].xx);
read(a[i].yy);
}
else if(s[] == 'S') { // save
fa[i] = i - ;
sv[++num] = i;
}
else { // load
int x;
read(x);
fa[i] = sv[x];
}
} int x = m;
while(x) {
path[++top] = x;
x = fa[x];
}
std::reverse(path + , path + top + );
num = ;
build(, , n, n, );
for(int e = ; e <= top; e++) {
int i = path[e];
if(!a[i].opt) {
continue;
}
a[i].x++;
a[i].xx++;
a[i].y++;
a[i].yy++;
X = a[i].x;
Y = a[i].y;
XX = a[i].xx;
YY = a[i].yy;
if((a[i].x - + a[i].y) & ) {
change(a[i].c, , , , n, n, );
}
else {
change(a[i].c, , , , n, n, );
}
} ed(, , n, n, );
for(int i = ; i <= n; i++) {
for(int j = ; j <= n; j++) {
printf("%d ", G[i][j]);
}
puts("");
}
return ;
}

四分树

这是考场上写的,四分树每次修改貌似是O(n)的,但是卡成6s......只有75分。

话说我第一次写四分树居然是在考场上,而且一个字符都没调就一次写对了......

时间复杂度到底是多少呀......nm的话应该能过呀?莫非是我常数大?

chessboard的更多相关文章

  1. LightOJ1171 Knights in Chessboard (II)(二分图最大点独立集)

    题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1171 Description Given an m x n ches ...

  2. CodeForces445A DZY Loves Chessboard

    A. DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input stand ...

  3. FJOI省队集训 chessboard

    (题目懒得打字了,建议到新窗口查看) 显然这玩意儿是可以按位搞的...然后就是一个裸的最小割模型? 然而这样做理论上只有30分实际上有40分. 事实上我们可以发现,每一列的取值只和上一列有关,这样我们 ...

  4. [poj2446]Chessboard

    Description 给定一个m×n的棋盘,上面有k个洞,求是否能在不重复覆盖且不覆盖到洞的情况下,用2×1的卡片完全覆盖棋盘. Input 第一行有三个整数n,m,k(0<m,n<=3 ...

  5. UESTC 1851 Kings on a Chessboard

    状压DP... Kings on a Chessboard Time Limit: 10000ms Memory Limit: 65535KB This problem will be judged ...

  6. UVa12633 Super Rooks on Chessboard(容斥 + FFT)

    题目 Source http://acm.hust.edu.cn/vjudge/problem/42145 Description Let’s assume there is a new chess ...

  7. poj 2446 Chessboard (二分匹配)

    Chessboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12800   Accepted: 4000 Descr ...

  8. 周赛-DZY Loves Chessboard 分类: 比赛 搜索 2015-08-08 15:48 4人阅读 评论(0) 收藏

    DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. DZY Loves Chessboard

    DescriptionDZY loves chessboard, and he enjoys playing with it. He has a chessboard of n rows and m ...

  10. UVa 10161 Ant on a Chessboard

    一道数学水题,找找规律. 首先要判断给的数在第几层,比如说在第n层.然后判断(n * n - n + 1)(其坐标也就是(n,n)) 之间的关系. 还要注意n的奇偶.  Problem A.Ant o ...

随机推荐

  1. 使用Gzip压缩数据,加快页面访问速度

                 在返回的json数据量大时,启用Gzip压缩,可以提高传输效率.下面为Gzip压缩对json字符串压缩并输出到页面的代码. 一.代码 /** 向浏览器输出字符串响应数据,启用 ...

  2. 洛咕3312 [SDOI2014]数表

    洛咕3312 [SDOI2014]数表 终于独立写出一道题了...真tm开心(还是先写完题解在写的) 先无视a的限制,设\(f[i]\)表示i的约数之和 不妨设\(n<m\) \(Ans=\su ...

  3. 能帮我们学习吉他的音乐软件——Guitar Pro

    Guitar Pro是一款十分好用的吉他软件,也是目前广大音乐爱好者最喜欢的多音轨的音谱编辑软件.支持MIDI.MusicXML.PTB.GTP等多种格式文件的导入/导出. Guitar Pro 7. ...

  4. Unity 音频合并

    将多个音频组合起来进行播放 代码如下: ; [SerializeField] AudioClip s1; [SerializeField] AudioClip s2; [SerializeField] ...

  5. Web项目开发流程 PC端

      一.了解.明确需求. 这个应该是第一步了,不了解需求你就不知道为什么要做,要怎么去做这个项目的工作. (1)明确需求是相当重要的,很有必要去和产品经理.设计人员去沟通,需要明白每一个按钮,每一个开 ...

  6. 毕业回馈-89c52之最小系统

    今天分享一个51单片机最小系统的电路板设计(原理图+PCB) 技术手册上面对于51单片机最小系统作如下要求: 下载电路主要有以下几种: 采用RS-232转换器下载:(R1OUT-P3.0;T1IN-P ...

  7. 基于skip-gram做推荐系统的想法

    一.人工智能之自然语言处理 自然语言处理(Natural Language Processing, NLP),是人工智能的分支科学,意图是使计算机具备处理人类语言的能力. “处理人类语言的能力”要达到 ...

  8. 实训三(cocos2dx 3.x 打包apk)

    上一篇文章<实训二(cocos2dx 2.x 打包apk)>简单的讲述的利用cocos2dx 2.x引擎在windows平台上打包apk的方法与过程,本文将介绍3.x版本引擎,如何打包ap ...

  9. Visual Studio2013安装过程

    Visual Studio是微软开发的一套基于组件的软件开发工具,我选择安装的是Visual Studio2013版本.首先, 第一步是要找到一个安装包: 我们可以直接百度MSDN,显示的第一条就是官 ...

  10. ElasticSearch 2 (24) - 语言处理系列之停用词:性能与精度

    ElasticSearch 2 (24) - 语言处理系列之停用词:性能与精度 摘要 在信息检索早期,磁盘和内存相较我们今天的使用只是很小的一部分.将索引空间保持在一个较小的水平是至关重要的,节省每个 ...