QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#734863#7105. Pixel ArtmanaTL 0ms5660kbC++202.7kb2024-11-11 15:33:342024-11-11 15:33:40

Judging History

你现在查看的是最新测评结果

  • [2024-11-11 15:33:40]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:5660kb
  • [2024-11-11 15:33:34]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using i64 = int;
i64 ans;
i64 n, m, k, summ;
i64 res[100005][2], pre[100005], fa[100005];
i64 g[100005][4];
vector<i64> v[100005];vector<i64> e[100005];//分别存加入和删除
set<array<i64, 3>> st;
int find(int x){//查询父节点
    while (x != fa[x]) x = fa[x] = fa[fa[x]];
    return x;
}
bool merge(int x, int y){//合并 x 和 y 所在并查集
    x = find(x), y = find(y);
    if (x == y) return false;
    if(x != y){ ans--;}
    fa[y] = x;
    return true;
}
//思路:按照左/上端点进行排序,并查集维护连通块,按排序遍历每条边,合并
void init(){
	st.clear();
	summ = ans = 0;
	g[0][0] = g[0][1] = g[0][2] = g[0][3] = -1;
	for(int i = 0; i <= n + 1; i++){
		pre[i] = res[i][0] = res[i][1] = 0; 
		v[i].clear();
		e[i].clear();
	}
	for(int i = 0; i <= k + 1; i++){
		fa[i] = i;
	}
}
void solve(){
	i64 temp, sub, x, y;
    cin >> n >> m >> k;
    init();
    for (int i = 1; i <= k; i++){
    	cin >> g[i][0] >> g[i][1] >> g[i][2] >> g[i][3];
        if(g[i][0] == g[i][2]){//横边
        	res[g[i][0]][0] += g[i][3] - g[i][1] + 1;
        }
        else{//竖边
        	pre[g[i][0]]++;
        	pre[g[i][2]+1]--;
        }
        v[g[i][0]].push_back(i);
        e[g[i][2] + 1].push_back(i);
    }
    for(int i = 1; i <= n; i++){//统计答案s
    	summ += pre[i];
    	res[i][0] += summ;	
    	res[i][0] += res[i-1][0];
    }
    for(int i = 1; i <= n; i++){
    	for(int j = 0; j < v[i].size(); j++){
    		x = v[i][j];
    		auto it = st.lower_bound({x,0,0});
    		if(it != st.begin()){
    			--it;
    		}
    		while(it != st.end()){
    			if((*it)[0] > g[x][3]){
    				break;
    			}
    			if((*it)[1] >= g[x][1] && (*it)[0] <= g[x][3]){
    				merge((*it)[2], x);
    			}
    			it++;
    		}
    	}
    	for(int j = 0; j < e[i].size(); j++){
    		x = e[i][j];
    		st.erase({g[x][1],g[x][3],x});
    	}
    	for(int j = 0; j < v[i].size(); j++){
    		ans++;
    		x = v[i][j];
    		auto it = st.insert({g[x][1],g[x][3],x}).first;
    		auto it1 = it;
    		if(it1 != st.begin()){
    			--it1;
    			if((*it1)[1] == g[x][1] - 1){
    				merge((*it1)[2], x);
    			}
    		}
    		auto it2 = it;
    		++it2;
    		if(it2 != st.end()){
    			if((*it2)[0] == g[x][3] + 1){
    				merge((*it2)[2], x);
    			}
    		}
    	}
    	res[i][1] = ans;
    }
    for(int i = 1; i <= n; i++){
    	cout << res[i][0] << ' ' << res[i][1] << '\n';
    }
    return;
}
int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
	long long tt = 1;
	cin >> tt;
	while(tt--){
		solve();
	}
    return 0;
}


詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 5660kb

input:

3
2 5 2
1 1 1 2
2 3 2 5
2 5 2
1 1 1 3
2 3 2 5
3 3 3
1 1 1 2
3 1 3 2
1 3 2 3

output:

2 1
5 2
3 1
6 1
3 1
4 1
6 2

result:

ok 7 lines

Test #2:

score: -100
Time Limit Exceeded

input:

2130
2 5 2
1 1 1 2
2 3 2 5
2 5 2
1 1 1 3
2 3 2 5
3 3 3
1 1 1 2
3 1 3 2
1 3 2 3
3 100 51
1 2 2 2
1 4 2 4
1 6 2 6
1 8 2 8
1 10 2 10
1 12 2 12
1 14 2 14
1 16 2 16
1 18 2 18
1 20 2 20
1 22 2 22
1 24 2 24
1 26 2 26
1 28 2 28
1 30 2 30
1 32 2 32
1 34 2 34
1 36 2 36
1 38 2 38
1 40 2 40
1 42 2 42
1 44 2 44
...

output:

2 1
5 2
3 1
6 1
3 1
4 1
6 2
50 50
100 50
200 25
50 50
150 1
200 1
2 1
4 1
6 1
8 1
10 1
12 1
14 1
16 1
18 1
20 1
22 1
24 1
26 1
28 1
30 1
32 1
34 1
36 1
38 1
40 1
42 1
44 1
46 1
48 1
50 1
52 1
54 1
56 1
58 1
60 1
62 1
64 1
66 1
68 1
70 1
72 1
74 1
76 1
78 1
80 1
82 1
84 1
86 1
88 1
90 1
92 1
94 1
96 ...

result: