ARTWORK
Description
A template for an artwork is a white grid of n × m squares. The artwork will be created by painting q horizontal and vertical black strokes. A stroke starts from square (x1, y1), ends at square (x2,y2)(x1=x2(x2,y2)(x1=x2 or y1=y2)y1=y2) and changes the color of all squares (x, y) to black where x1 ≤ x ≤ x2 and y1 ≤ y ≤ y2. The beauty of an artwork is the number of regions in the grid. Each region consists of one or more white squares that are connected to each other using a path of white squares in the grid, walking horizontally or vertically but not diagonally. The initial beauty of the artwork is 1. Your task is to calculate the beauty after each new stroke. Figure A.1 illustrates how the beauty of the artwork varies in Sample Input 1.

Input
The first line of input contains three integers n, m and q (1 ≤ n, m ≤ 1000, 1 ≤ q ≤ 104). Then follow q lines that describe the strokes. Each line consists of four integersx1,y1,x2x1,y1,x2 and y2(1 ≤ x1 ≤ x2 ≤ n, 1 ≤ y1 ≤ y2 ≤ m). Either x1 = x2 or y1=y2y1=y2(or both).
Output
For each of the q strokes, output a line containing the beauty of the artwork after the stroke.
Sample Input
4 6 5 2 2 2 6 1 3 4 3 2 5 3 5 4 6 4 6 1 6 4 6
Sample Output
1 3 3 4 3
代码为:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int n,m,q; struct node{ int x1,y1,xx,yy; } que[10005]; int num[1000005]; int f[1000005]; int find(int x) { return f[x]==x?x:f[x]=find(f[x]); } int cnt; void merge(int x,int y) { int fx=find(x); int fy=find(y); if(fx==fy) return; cnt--; f[fx]=fy; } int Get_id(int x,int y) { return x*m+y; } int ans[10005]; int dfsx[4]={0,0,1,-1}; int dfsy[4]={1,-1,0,0}; bool in(int x,int y) { return x>=0&&y>=0&&x<n&&y<m; } void work(int x,int y) { for(int k=0;k<4;k++) { int tx=x+dfsx[k]; int ty=y+dfsy[k]; if(!in(tx,ty)||num[Get_id(tx,ty)]) continue; merge(Get_id(tx,ty),Get_id(x,y)); } } int main() { cin>>m>>n>>q; cnt=n*m; for(int i=0;i<n*m;i++) f[i]=i,num[i]=0; for(int i=0;i<q;i++) { cin>>que[i].y1>>que[i].x1>>que[i].yy>>que[i].xx; que[i].x1--; que[i].y1--; que[i].xx--; que[i].yy--; for(int x=que[i].x1;x<=que[i].xx;x++) { for(int y=que[i].y1;y<=que[i].yy;y++) { if(num[Get_id(x,y)]==0) cnt--; num[Get_id(x,y)]++; } } } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(num[Get_id(i,j)]) continue; work(i,j); } } for(int i=q-1;i>=0;i--) { ans[i]=cnt; for(int x=que[i].x1;x<=que[i].xx;x++) for(int y=que[i].y1;y<=que[i].yy;y++) { num[Get_id(x,y)]--; if(num[Get_id(x,y)]==0) { cnt++; work(x,y); } } } for(int i=0;i<q;i++) cout<<ans[i]<<endl; return 0; }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· C# 13 中的新增功能实操
· Ollama本地部署大模型总结
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(4)
· langchain0.3教程:从0到1打造一个智能聊天机器人
· 用一种新的分类方法梳理设计模式的脉络