【bzoj2594】[Wc2006]水管局长数据加强版

真是神题

当时调了几天没调出来 后来没管了

当时把fread去掉就TLE,加上就RE

一直在底下跟网上的程序拍,尝试各种优化常数都没用

拍出几组不一样的,发现我是对的,醉了,网上那个是怎么过的

记一下这蛋疼的代码

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdlib>
  4 #include<algorithm>
  5 #include<cstdio>
  6 #include<map>
  7 
  8 using namespace std;
  9 
 10 //default source begin==========
 11 const int D=30000000;
 12 char in[D],out[300010*10],*I=in+D,*O=out;
 13 
 14 char gc() {
 15     if(I==in+D) fread(in,1,D,stdin),I=in;
 16     return *(I++);
 17 }
 18 #define gc gc()
 19 #define pc(x) ((*O++)=x)
 20 #define tQ template <typename Q>
 21 tQ void gt(Q&x) {
 22     static char c,f;
 23     for(f=0;c=gc,!isdigit(c);)if(c=='-') f=1;
 24     for(x=0;isdigit(c);c=gc) x=x*10+c-'0';
 25     f && (x=-x);
 26 }
 27 tQ void pt(Q x){
 28     static char stk[20];
 29     static int top;
 30     top=0;
 31     if(x==0) pc('0');
 32     for(;x;x/=10) stk[++top] = x%10+'0';
 33     for(;top;top--) pc(stk[top]);
 34 }
 35 //default source end=============
 36 
 37 const int Maxn=100100,Maxm=1000010,Maxq=100010;
 38 int n,m,Q;
 39 
 40 int ch[Maxn+Maxm][2],p[Maxn+Maxm],flip[Maxn+Maxm],mx[Maxn+Maxm],w[Maxn+Maxm];
 41 map<pair<int,int> ,struct Edge*> hash;
 42 int ddb[Maxn+Maxm];
 43 typedef long long ll;
 44 /*
 45 const unsigned int Hmod=1e6+7;
 46 struct Node{
 47     ll hs;
 48     int id;
 49     Node*next;
 50     Node(ll hs=0,int id=0,Node*next=0):hs(hs),id(id),next(next){}
 51 }da[Maxm],*fir[Hmod];
 52 
 53 int find(ll x){
 54     unsigned md=x%Hmod;
 55     for(Node*p=fir[md];p;p=p->next){
 56         if(p->hs==x) return p->id;
 57     }
 58     return NULL;
 59 }
 60 
 61 int insert(ll x){
 62     unsigned md=x%Hmod;
 63 */    
 64 
 65 #define l ch[x][0]
 66 #define r ch[x][1]
 67 void update(int x){
 68     if(!x) return;
 69     mx[x]=x;
 70     if(w[mx[l]]>w[mx[x]]) mx[x]=mx[l];
 71     if(w[mx[r]]>w[mx[x]]) mx[x]=mx[r];
 72 }
 73 void down(int x) {
 74     if(!x || !flip[x]) return;
 75     swap(l,r);
 76     flip[l]^=1;
 77     flip[r]^=1;
 78     flip[x]=0;
 79 }
 80 #undef l
 81 #undef r
 82 inline bool isroot(int x) {
 83     return ch[p[x]][0]!=x && ch[p[x]][1]!=x;
 84 }
 85 inline void rotate(int x){
 86     int y=p[x],z=p[y];
 87     int l=ch[y][1]==x,r=l^1;
 88     if(!isroot(y)){
 89         ch[z][ch[z][1]==y]=x;
 90     }
 91     p[y]=x;
 92     p[ch[x][r]]=y;
 93     p[x]=z;
 94     
 95     ch[y][l]=ch[x][r];
 96     ch[x][r]=y;
 97     
 98     update(y);
 99 //    update(x);
100 }
101 
102 int stk[Maxn],top;
103 inline void splay(int x){
104     stk[top=1]=x;
105     for(int t=x;!isroot(t);stk[++top]=t=p[t]);
106     for(;top;top--) down(stk[top]);
107     for(;!isroot(x);){
108         int y=p[x],z=p[y];
109         if(!isroot(y)) {
110             if( (ch[y][0]==x) ^ (ch[z][0]==y)) rotate(x);
111             else rotate(y);
112         }
113         rotate(x);
114     }
115     update(x);
116 }
117 
118 inline void access(int x) {
119     for(int t=0;x;x=p[t=x]){
120         splay(x);
121         ch[x][1]=t;
122         update(x);
123     }
124 }
125 
126 inline void newroot(int x) {
127     access(x);
128     splay(x);
129     flip[x]^=1;
130 }
131 
132 inline void n_as(int u,int v){
133     newroot(u);
134     access(v);
135     splay(v);
136 }
137 
138 inline void Cut(int x,int y) {
139     n_as(x,y);
140     ch[y][0]=p[x]=0;
141     update(x);
142 }
143 
144 inline void Link(int x,int y) {
145     newroot(x);
146     p[x]=y;
147 }
148 
149 struct Edge{
150     int u,v,w,id;
151     bool deled;
152     void read() {
153         gt(u);gt(v);gt(w);
154         if(u>v) swap(u,v);
155     }
156     bool operator<(const Edge&rhs)const {
157         return u<rhs.u || (u==rhs.u&&v<rhs.v);
158     }
159     bool operator == (const Edge&rhs)const {
160         return u==rhs.u && v==rhs.v;
161     }
162     bool operator <= (const Edge&rhs)const {
163         return *this<rhs || *this==rhs;
164     }
165     Edge(int u=0,int v=0):u(u),v(v){}
166 }e[Maxm];
167 
168 bool cmpw(const Edge&lhs,const Edge&rhs) {
169     return lhs.w<rhs.w;
170 }
171 
172 struct Que{
173     int k,u,v;
174     Que (int k=0,int u=0,int v=0):k(k),u(u),v(v){}
175 }que[Maxq];
176 
177 int bs(const Edge&target) {
178     int l=1,r=m,res=-1;
179     for(int mid;l<=r;) {
180         mid=(l+r)>>1;
181         if(e[mid]<=target) res=mid,l=mid+1;
182         else r=mid-1;
183     }
184     if(res==-1) exit(-1);
185     return res;
186 }
187 
188 int fa[Maxn];
189 int Find(int x){
190     return fa[x]==x?x:fa[x]=Find(fa[x]);
191 }
192 bool Union(int x,int y) {
193     x=Find(x),y=Find(y);
194     if(x==y) return 0;
195     return fa[x]=y,1;
196 }
197 void ufs_init(int n) {
198     for(int i=0;i<=n;i++) fa[i]=i;
199 }
200 void AddEdge(int i) {
201     const int&u=e[i].u,&v=e[i].v;
202     if(Union(u,v)) Link(u,i+n),Link(v,i+n);
203     else {
204         n_as(u,v);int t=mx[v];
205         if(w[t] > e[i].w) {
206 //            Cut(e[t-n].u,t);
207             Cut(e[t-n].v,t);
208             Link(u,i+n);
209             Link(v,i+n);
210         }
211     }
212 }
213 
214 bool cmpid(const Edge&lhs,const Edge&rhs){
215     return lhs.id<rhs.id;
216 }
217 
218 void init() {
219     gt(n),gt(m),gt(Q);
220     for(int i=1;i<=m;i++) e[i].read();
221     sort(e+1,e+m+1);
222     for(int i=1;i<=m;i++) e[i].id=i,w[i+n]=e[i].w;
223     for(int k,u,v,i=1;i<=Q;i++) {
224         gt(k),gt(u),gt(v);
225         if(u>v) swap(u,v);
226         que[i]=Que(k,u,v);
227         if(k==2) e[bs(Edge(u,v))].deled=1;
228     }
229 }
230 
231 void Kruskal() {
232     sort(e+1,e+m+1,cmpw);
233     ufs_init(n);
234     for(int i=1,MST=0;i<=m;i++) {
235         if(e[i].deled) continue;
236         if(!Union(e[i].u,e[i].v)) continue;
237         Link(e[i].u,e[i].id+n);
238         Link(e[i].v,e[i].id+n);
239         if(++MST==n-1) break;
240     }
241 }
242 
243 int ans[Maxq],tq=0;
244 void work() {
245     Kruskal();
246     sort(e+1,e+m+1);
247     for(int i=Q;i;i--) {
248         const Que&q=que[i];
249         if(q.k==1) {
250             n_as(q.u,q.v);
251             ans[++tq] = w[mx[q.v]];
252         }else {
253             AddEdge(bs(Edge(q.u,q.v)));
254         }
255     }
256     for(int i=tq;i;i--) pt(ans[i]),pc('
');
257 }
258 
259 int main() {
260 #ifdef DEBUG
261     freopen("in.txt","r",stdin);
262     freopen("out.txt","w",stdout);
263 #endif
264     
265     init();
266     work();
267     
268     return printf(out),0;
269 }
View Code
原文地址:https://www.cnblogs.com/showson/p/4657076.html