1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| #include "werewolf.h" #include<bits/stdc++.h> using namespace std;
void read(int &x) { x=0;int f=1;char ch=getchar(); for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f; for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f; }
void print(int x) { if(x<0) putchar('-'),x=-x; if(!x) return ;print(x/10),putchar(x%10+48); } void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}
#define lf double #define ll long long
#define pii pair<int,int > #define vec vector<int >
#define pb push_back #define mp make_pair #define fr first #define sc second
#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++)
const int maxn = 4e5+10; const int inf = 1e9; const lf eps = 1e-8; const int mod = 1e9+7;
int n,m;
struct dat {int u,v,w;};
struct kruskal_rebuild_tree { struct edge {int to,nxt;}e[maxn<<1]; dat a[maxn]; int fa[maxn],cnt,val[maxn],f[maxn][20],mn[maxn],mx[maxn],s[maxn][2],dfn_cnt;
int cmp1(dat a,dat b) {return a.w<b.w;} int cmp2(dat a,dat b) {return a.w>b.w;}
int find(int x) {return fa[x]==x?x:fa[x]=find(fa[x]);}
void dfs(int x) { if(!s[x][0]) return mn[x]=mx[x]=++dfn_cnt,void(); dfs(s[x][0]),dfs(s[x][1]); mn[x]=mn[s[x][0]],mx[x]=mx[s[x][1]]; } void solve(int op) { sort(a+1,a+m+1,[&](dat a,dat b){return op?(a.w<b.w):(a.w>b.w);}); for(int i=1;i<=n*2;i++) fa[i]=i;cnt=n; for(int i=1;i<=m;i++) { int u=find(a[i].u),v=find(a[i].v); if(u==v) continue; val[++cnt]=a[i].w; f[u][0]=f[v][0]=cnt; fa[u]=fa[v]=cnt;s[cnt][0]=u,s[cnt][1]=v; } for(int i=1;i<20;i++) for(int j=1;j<=cnt;j++) f[j][i]=f[f[j][i-1]][i-1]; dfs(cnt);puts(""); }
pii get(int x,int v,int op) { for(int i=19;~i;i--) { if(!f[x][i]) continue; if((op&&val[f[x][i]]>=v)||(!op&&val[f[x][i]]<=v)) x=f[x][i]; } return mp(mn[x],mx[x]); } }s,t;
int tot,pps[maxn],ans[maxn]; struct qqq {int x,l,r,op,id;}r[maxn];
int cmp(qqq a,qqq b) {return a.x<b.x;}
struct segment_tree { int t[maxn]; void add(int x) {for(;x<=n;x+=x&-x) t[x]++;} int qry(int x,int ans=0) {for(;x;x-=x&-x) ans+=t[x];return ans;} int get(int l,int r) {return qry(r)-(l>1?qry(l-1):0);} }T;
vec check_validity(int nn,vec x,vec y,vec ss,vec e,vec l,vec rr) { n=nn,m=x.size(); for(int i=0;i<m;i++) { x[i]++,y[i]++; s.a[i+1]=(dat){x[i],y[i],max(x[i],y[i])}; t.a[i+1]=(dat){x[i],y[i],min(x[i],y[i])}; }s.solve(1),t.solve(0); int q=ss.size(); for(int i=0;i<q;i++) { ss[i]++,e[i]++,l[i]++,rr[i]++; pii a=t.get(ss[i],l[i],1),b=s.get(e[i],rr[i],0); if(b.fr>1) r[++tot]=(qqq){b.fr-1,a.fr,a.sc,-1,i}; r[++tot]=(qqq){b.sc,a.fr,a.sc,1,i}; } sort(r+1,r+tot+1,cmp);int p=0; for(int i=1;i<=n;i++) pps[s.mn[i]]=t.mn[i]; for(int i=1;i<=n;i++) { T.add(pps[i]); while(p<tot&&r[p+1].x==i) p++,ans[r[p].id]+=r[p].op*T.get(r[p].l,r[p].r); } vec res; for(int i=0;i<q;i++) res.pb((bool)ans[i]); return res; }
|