BZOJ 3673 지속 가능 하 며 by zky & & 3674 지속 가능 하 며 강화 판 검색 가능
사고방식: 지속 가능 한 라인 트 리 를 이용 하여 지속 가능 한 그룹 유지 보 수 를 실현 하면 지속 적 이 고 집합 을 찾 을 수 있다.(3674 어디 가 강 화 됐 는 지...
CODE:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define RANGE 8000010
#define MAX 200200
using namespace std;
struct SegTree{
SegTree *son[2];
int val;
void *operator new(size_t,SegTree *_,SegTree *__,int ___);
}mempool[RANGE],*C = mempool,*father[MAX],*h[MAX];
void *SegTree:: operator new(size_t,SegTree *_,SegTree *__,int ___) {
C->son[0] = _;
C->son[1] = __;
C->val = ___;
return C++;
}
int cnt,asks;
int verson[MAX],now_ver,latest;
int last_ans;
SegTree *Modify(SegTree *a,int l,int r,int x,int val)
{
if(l == r) return new(NULL,NULL,val)SegTree;
int mid = (l + r) >> 1;
if(x <= mid) return new(Modify(a->son[0],l,mid,x,val),a->son[1],0)SegTree;
return new(a->son[0],Modify(a->son[1],mid + 1,r,x,val),0)SegTree;
}
int Ask(SegTree *a,int l,int r,int x)
{
if(l == r) return a->val;
int mid = (l + r) >> 1;
if(x <= mid) return Ask(a->son[0],l,mid,x);
return Ask(a->son[1],mid + 1,r,x);
}
inline int Find(int x)
{
int y;
while(x)
x = Ask(father[now_ver],1,cnt,y = x);
return y;
}
inline void Unite(int x,int y)
{
int fx = Find(x),fy = Find(y);
if(fx == fy) return ;
++latest;
int hx = Ask(h[now_ver],1,cnt,fx);
int hy = Ask(h[now_ver],1,cnt,fy);
if(hx < hy)
swap(x,y),swap(fx,fy),swap(hx,hy);
father[latest] = Modify(father[now_ver],1,cnt,fy,fx);
h[latest] = Modify(h[now_ver],1,cnt,fx,hx + hy);
now_ver = latest;
}
inline bool Query(int x,int y)
{
return Find(x) == Find(y);
}
int main()
{
cin >> cnt >> asks;
father[0] = new(C,C,0)SegTree;
h[0] = new(C,C,1)SegTree;
for(int flag,x,y,i = 1; i <= asks; ++i) {
scanf("%d",&flag);
if(flag == 1) {
scanf("%d%d",&x,&y);
x ^= last_ans,y ^= last_ans;
Unite(x,y);
}
else if(flag == 2) {
scanf("%d",&x);
x ^= last_ans;
now_ver = verson[x];
}
else {
scanf("%d%d",&x,&y);
x ^= last_ans,y ^= last_ans;
printf("%d
",last_ans = Query(x,y));
}
verson[i] = now_ver;
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
bzoj1010 장난감 포장 [결정 단조성 최적화 dp]하나의 모범 문제라고 할 수 있다.우리는 먼저 소박한 dp방정식을 얻을 수 있다. f[i]=min(f[j]+w(j,i)),j∈[0,i). w(j,i)는 j+1~i를 포장하여 운송하는 비용의 시간 복잡도는 O(n2)라...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.