标签:维数 put open vector std pre ace :link 主席树
IOI强行交互可还行,我Luogu的代码要改很多才能交到UOJ去……
发现问题是对边权做限制的连通块类问题,考虑\(Kruskal\)重构树进行解决。
对于图上的边\((u,v)(u<v)\),我们建两棵\(Kruskal\)重构树,一棵按照\(u\)从大到小加边表示人形时的活动区域,一棵按照\(v\)从小到大加边表示狼形时的活动区域。
那么我们的每组询问就变为了:给出两段区间,询问是否存在一个点同时覆盖这两个区间(即人形转换为狼形的地点)。这个是经典的二维数点问题,使用主席树计算即可。
话说感觉一堆namespace的码风看起来好舒服啊~
#include<bits/stdc++.h>
//This code is written by Itst
using namespace std;
inline int read(){
int a = 0;
char c = getchar();
bool f = 0;
while(!isdigit(c)){
if(c == ‘-‘)
f = 1;
c = getchar();
}
while(isdigit(c)){
a = (a << 3) + (a << 1) + (c ^ ‘0‘);
c = getchar();
}
return f ? -a : a;
}
const int MAXN = 2e5 + 10;
int N , M , Q;
namespace ST{
#define mid ((l + r) >> 1)
struct node{
int l , r , sum;
}Tree[MAXN * 50];
int root[MAXN] , cnt;
void insert(int& x , int p , int l , int r , int tar){
x = ++cnt;
Tree[x] = Tree[p];
++Tree[x].sum;
if(l == r)
return;
if(mid >= tar)
insert(Tree[x].l , Tree[p].l , l , mid , tar);
else
insert(Tree[x].r , Tree[p].r , mid + 1 , r , tar);
}
bool query(int x , int y , int l , int r , int L , int R){
if(Tree[x].sum == Tree[y].sum)
return 0;
if(l >= L && r <= R)
return 1;
bool f = 0;
if(mid >= L)
f |= query(Tree[x].l , Tree[y].l , l , mid , L , R);
if(!f && mid < R)
f |= query(Tree[x].r , Tree[y].r , mid + 1 , r , L , R);
return f;
}
}
namespace Itst{
#define P pair < int , int >
int fa[2][MAXN << 1] , ch[2][MAXN << 1][2] , jump[2][MAXN << 1][21] , que[2][MAXN << 1] , dfn[2][MAXN] , cnt[2] , ts[2] , rg[2][MAXN << 1][2];
void init(){
cnt[0] = cnt[1] = N;
for(int i = 1 ; i <= N ; ++i)
fa[0][i] = fa[1][i] = i;
}
int find(int ind , int x){
return fa[ind][x] == x ? x : (fa[ind][x] = find(ind , fa[ind][x]));
}
inline void link(int ind , int x , int y){
x = find(ind , x);
y = find(ind , y);
if(x == y)
return;
++cnt[ind];
fa[ind][x] = fa[ind][y] = fa[ind][cnt[ind]] = cnt[ind];
ch[ind][cnt[ind]][0] = x;
ch[ind][cnt[ind]][1] = y;
}
void dfs(int ind , int x , int p){
jump[ind][x][0] = p;
for(int i = 1 ; jump[ind][x][i - 1] ; ++i)
jump[ind][x][i] = jump[ind][jump[ind][x][i - 1]][i - 1];
if(x <= N){
rg[ind][x][0] = rg[ind][x][1] = dfn[ind][x] = ++ts[ind];
que[ind][x] = x;
return;
}
dfs(ind , ch[ind][x][0] , x);
dfs(ind , ch[ind][x][1] , x);
que[ind][x] = ind ? min(que[ind][ch[ind][x][0]] , que[ind][ch[ind][x][1]]) : max(que[ind][ch[ind][x][0]] , que[ind][ch[ind][x][1]]);
rg[ind][x][0] = rg[ind][ch[ind][x][0]][0];
rg[ind][x][1] = rg[ind][ch[ind][x][1]][1];
}
int up(int ind , int x , int l , int r){
for(int i = 19 ; i >= 0 ; --i)
if(jump[ind][x][i] && que[ind][jump[ind][x][i]] >= l && que[ind][jump[ind][x][i]] <= r)
x = jump[ind][x][i];
return x;
}
void work(){
dfs(0 , cnt[0] , 0);
dfs(1 , cnt[1] , 0);
vector < P > p;
for(int i = 1 ; i <= N ; ++i)
p.push_back(P(dfn[0][i] , dfn[1][i]));
sort(p.begin() , p.end());
for(int i = 1 ; i <= N ; ++i)
ST::insert(ST::root[i] , ST::root[i - 1] , 1 , N , p[i - 1].second);
while(Q--){
int S = read() + 1 , E = read() + 1 , L = read() + 1 , R = read() + 1;
E = up(0 , E , 1 , R);
S = up(1 , S , L , N);
puts(ST::query(ST::root[rg[0][E][1]] , ST::root[rg[0][E][0] - 1] , 1 , N , rg[1][S][0] , rg[1][S][1]) ? "1" : "0");
}
}
}
namespace init{
struct Edge{
int a , b;
}Ed[MAXN << 1];
bool cmp1(Edge a , Edge b){
return a.b < b.b;
}
bool cmp2(Edge a , Edge b){
return a.a > b.a;
}
void main(){
N = read();
M = read();
Q = read();
Itst::init();
for(int i = 1 ; i <= M ; ++i){
Ed[i].a = read() + 1;
Ed[i].b = read() + 1;
if(Ed[i].a > Ed[i].b)
swap(Ed[i].a , Ed[i].b);
}
sort(Ed + 1 , Ed + M + 1 , cmp1);
for(int i = 1 ; i <= M ; ++i)
Itst::link(0 , Ed[i].a , Ed[i].b);
sort(Ed + 1 , Ed + M + 1 , cmp2);
for(int i = 1 ; i <= M ; ++i)
Itst::link(1 , Ed[i].a , Ed[i].b);
Itst::work();
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in" , "r" , stdin);
//freopen("out" , "w" , stdout);
#endif
init::main();
return 0;
}
Luogu4899 IOI2018 Werewolf 主席树、Kruskal重构树
标签:维数 put open vector std pre ace :link 主席树
原文地址:https://www.cnblogs.com/Itst/p/10261199.html