QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#733959 | #7105. Pixel Art | mana | RE | 0ms | 3492kb | C++20 | 5.9kb | 2024-11-10 22:27:55 | 2024-11-10 22:27:55 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int fa[100005];//fa是父亲 siz是所在并查集大小
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){
swap(x, y);
}
fa[y] = x;
return true;
}
i64 n, m, k, s;
i64 res[100005][2], pre[100005];
vector<array<i64, 4>> g;
vector<pair<i64,i64>> v1[100005];
vector<pair<i64,i64>> v2[100005];
vector<pair<i64,i64>> v3[100005];
vector<i64> v[100005];//分别存横边左顶点,竖边上顶点,竖边下顶点,和所有边
vector<pair<i64,i64>> v4[100005];
unordered_map<i64,i64> mp1;
unordered_map<i64,i64> mp2;
//思路:按照左/上端点进行排序,并查集维护连通块,按排序遍历每条边,合并
void init(){
g.resize(k + 1);
mp1.clear();
mp2.clear();
s = 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;
v1[i].clear();
v2[i].clear();
v3[i].clear();
v[i].clear();
}
for(int i = 0; i <= k + 1; i++){
fa[i] = i;
v4[i].clear();
}
}
void solve(){
i64 temp, summ, sub, x, y;
cin >> n >> m >> k;
init();
for (int i = 1; i <= k; i++){
cin >> g[i][0];
cin >> g[i][1];
cin >> g[i][2];
cin >> g[i][3];
}
sort(g.begin() + 1, g.begin() + 1 + k);
for (int i = 1; i <= k; i++){
if(g[i][0] == g[i][2]){//横边
res[g[i][0]][0] += g[i][3] - g[i][1] + 1;
v1[g[i][0]].push_back(make_pair(g[i][1], i));
}
else{//竖边
pre[g[i][0]]++;
pre[g[i][2]+1]--;
v2[g[i][0]].push_back(make_pair(g[i][1], i));
v3[g[i][2]].push_back(make_pair(g[i][1], i));
if(mp1[g[i][1]] == 0){
s++;
mp1[g[i][1]] = s;
mp2[s] = g[i][1];
}
v4[mp1[g[i][1]]].push_back(make_pair(g[i][0], i));
}
v[g[i][0]].push_back(i);
}
for(int i = 1; i <= s; i++){
sort(v4[i].begin(),v4[i].end());
}
for(int i = 1; i <= n; i++){
if(v3[i].size()){
sort(v3[i].begin(),v3[i].end());
}
}
summ = 0;
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 <= k; i++){
if(g[i][0] == g[i][2]){//横边
//横边与横边
sub = lower_bound(v1[g[i][0] - 1].begin(), v1[g[i][0] - 1].end(), make_pair(g[i][1], 0ll)) - v1[g[i][0] - 1].begin();
if(sub > 0){
y = v1[g[i][0] - 1][sub - 1].second;
if(g[y][3] >= g[i][1]){
merge(y, i);
}
}
for(int j = sub; j < v1[g[i][0] - 1].size(); j++){
x = v1[g[i][0] - 1][j].first, y = v1[g[i][0] - 1][j].second;
if(x > g[i][3]){
break;
}
merge(y, i);
}
if(g[i-1][0] == g[i-1][2] && g[i-1][0] == g[i][0] && g[i-1][3] == g[i][1] - 1){
merge(i-1, i);
}
//横边和竖边
sub = lower_bound(v2[g[i][0] + 1].begin(), v2[g[i][0] + 1].end(), make_pair(g[i][1], 0ll)) - v2[g[i][0] + 1].begin();
for(int j = sub; j < v2[g[i][0] + 1].size(); j++){
x = v2[g[i][0] + 1][j].first, y = v2[g[i][0] + 1][j].second;
if(x > g[i][3]){
break;
}
merge(y, i);
}
sub = lower_bound(v3[g[i][0] - 1].begin(), v3[g[i][0] - 1].end(), make_pair(g[i][1], 0ll)) - v3[g[i][0] - 1].begin();
for(int j = sub; j < v3[g[i][0] - 1].size(); j++){
x = v3[g[i][0] - 1][j].first, y = v3[g[i][0] - 1][j].second;
if(x > g[i][3]){
break;
}
merge(y, i);
}
if(mp1[g[i][1] - 1] > 0){
sub = upper_bound(v4[mp1[g[i][1] - 1]].begin(), v4[mp1[g[i][1] - 1]].end(), make_pair(g[i][0],10000000ll)) - v4[mp1[g[i][1] - 1]].begin();
if(sub > 0){
y = v4[mp1[g[i][1] - 1]][sub - 1].second;
if(g[y][2] >= g[i][0]){
merge(y, i);
}
}
}
if(mp1[g[i][3] + 1] > 0){
sub = upper_bound(v4[mp1[g[i][3] + 1]].begin(), v4[mp1[g[i][3] + 1]].end(), make_pair(g[i][0],10000000ll)) - v4[mp1[g[i][3] + 1]].begin();
if(sub > 0){
y = v4[mp1[g[i][3] + 1]][sub - 1].second;
if(g[y][2] >= g[i][0]){
merge(y, i);
}
}
}
}
else{//竖边
sub = lower_bound(v4[mp1[g[i][1]]].begin(), v4[mp1[g[i][1]]].end(), make_pair(g[i][0], 0ll)) - v4[mp1[g[i][1]]].begin();
if(sub > 0){
y = v4[mp1[g[i][1]]][sub - 1].second;
if(g[y][2] == g[i][0] - 1){
merge(y, i);
}
}
if(mp1[g[i][1] - 1] > 0){
sub = lower_bound(v4[mp1[g[i][1]] - 1].begin(), v4[mp1[g[i][1]] - 1].end(), make_pair(g[i][0], 0ll)) - v4[mp1[g[i][1]] - 1].begin();
if(sub > 0){
y = v4[mp1[g[i][1] - 1]][sub - 1].second;
if(g[y][2] >= g[i][0]){
merge(y, i);
}
}
for(int j = sub; j < v4[mp1[g[i][1]] - 1].size(); j++){
x = v4[mp1[g[i][1]] - 1][j].first, y = v4[mp1[g[i][1]] - 1][j].second;
if(x > g[i][2]){
break;
}
merge(y, i);
}
}
}
}
summ = 0;
for(int i = 1; i <= k; i++){
if(find(i) == i){
summ++;
}
}
res[n][1] = summ;
for(int i = n - 1; i >= 1; i--){
for(int j = 0; j < v[i+1].size(); j++){
if(find(v[i+1][j]) == v[i+1][j]){
summ--;
}
}
res[i][1] = summ;
}
for(int i = 1; i <= n; i++){
cout << res[i][0] << ' ' << res[i][1] << endl;
}
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: 3492kb
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
Runtime Error
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 1 100 1 200 1 50 1 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 1 98...