社区讨论

蒟蒻求调

P15361「WYZOI R2」拜年参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@mlpg9j5o
此快照首次捕获于
2026/02/17 01:30
前天
此快照最后确认于
2026/02/17 10:10
前天
查看原帖
可删除并查集不怎么会,调。
CPP
#include <bits/stdc++.h>

#define int long long
#define up(i ,x ,y) for(int i = x ; i <= y ; i ++)
#define dn(i ,x ,y) for(int i = x ; i >= y ; i --)
#define inf 1e18

using namespace std;

inline int read() {int x = 0 ; char c = getchar() ; bool w = 0;while(c < '0' || c > '9') {	w |= (c == '-') ,c = getchar();} while(c >= '0' && c <= '9') {x = (x << 1) + (x << 3) + (c ^ 48) ,c = getchar();} return w ? -x : x;} 
inline void write(int x) {if(x < 0) x = -x ,putchar('-');if(x > 9) write(x / 10);putchar(x % 10 | 48);}
inline void writesp(int x) {write(x) ,putchar(' ');}
inline void writeln(int x) {write(x) ,putchar('\n');}

const int N = 4e5 + 10 ,M = 5e6 + 10;
int n ,a[3][N] ,fa[M];
bool vis[3][N];
const int fx[] = {-1 ,0 ,1 ,0} ,fy[] = {0 ,-1 ,0 ,1};

vector < pair <int ,int> > vec[N];
inline int get(int i ,int j) {return (i - 1) * n + j;}
inline int find(int x) {return x == fa[x] ? x : fa[x] = find(fa[x]);}
int idx;

namespace lolcrying {
    
    inline void del(int x){fa[x] = ++ idx;}
    
    inline void merge(int xx){

        for(auto j : vec[xx]) vis[j.first][j.second] = 1;
                
        for(auto j : vec[xx]) {
            int x = j.first ,y = j.second;
                    
            up(dir ,0 ,3) {
                int ax = x + fx[dir] ,ay = y + fy[dir];
                if(ax <= 2 && ax >= 1 && ay >= 1 && ay <= n && vis[ax][ay]) {
                    fa[find(get(x ,y))] = find(get(ax ,ay)); 
                }
            }
        }  
    } signed main(){

        n = read(); idx = 0 ;
        
        up(i ,1 ,2 * n) vec[i].clear();
        up(i ,1 ,2) up(j ,1 ,n) vis[i][j] = 0 ,a[i][j] = read() ,vec[a[i][j]].push_back({i ,j}) ,fa[get(i ,j)] = get(i ,j);

        int ans = 0  ,pos = 0 ;
        dn(i ,2 * n ,1) {
            merge(i);
            if(find(get(1 ,1)) != find(get(2 ,n))) continue;
            else {
                pos = i ; break;
            }
        }

        idx = 2 * n;
        up(i ,1 ,2 * n) fa[i] = ++ idx;
        up(i ,2 * n + 1 ,6 * n + 10) fa[i] = i;
        up(i ,1 ,2) up(j ,1 ,n) vis[i][j] = 0 ;
        
        int res = 0;
        up(i ,1 ,pos){
            while(res <= 2 * n && (find(1) != find(2 * n))) {
                ++ res;
                merge(res);
            }

// cout << "* ";                writeln(res);
            ans += (2 * n - res + 1);

            for(auto j : vec[i]) {
                del(get(j.first ,j.second));
            }

           // cout << "! "; writesp(find(1)) ,writeln(find(2 * n));
        } 
        
        writeln(ans);
        
        return 0 ;
    }
 
} signed main(){
    
    int T = read();
	
    while(T --) lolcrying :: main();
	
    return 0;
	
}

回复

0 条回复,欢迎继续交流。

正在加载回复...