Topic Portal
The Fast Train
The Slow Train
Main topic
An infinitely large square chart has a $n$ black dot in it. Ask how many positions there are at least one black point or a black dot.
The scan line is obvious.
Consider the horizontal line, take it two endpoints, the horizontal axis of a small place to put a +1, a large place to put a-1 event.
Then scan, sweep to the cross of the line to update, vertical line segment with a tree-like array to find answers.
Then consider whether the original black spots on this column are counted, if not, add them.
Code
1 /**2 * Bzoj3 * problem#18184 * Accepted5 * Time:1824ms6 * memory:9776k7 */8#include <algorithm>9#include <iostream>Ten#include <cstdlib> One#include <cstring> A#include <cstdio> -#include <vector> - #ifndef WIN32 the #defineAuto "%lld" - #else - #defineAuto "%i64d" - #endif + using namespacestd; - #defineSmax (A, b) (A = Max (A, b)) + #defineSmin (A, b) (a = min (a, b)) AtypedefBOOLBoolean; at #definell Long Long - - Const intN = 1e5 +5; - -typedefclassIndexedtree { - Public: in ints; - int*ar; to + Indexedtree () {} -Indexedtree (ints): s (s) { theAR =New int[(S +1)]; *memset (AR,0,sizeof(int) * (S +1)); $ }Panax Notoginseng - voidAddintIdxintval) { the for(; IDX <= s; idx + = (idx & (-idx ))) +AR[IDX] + =Val; A } the + intQueryintidx) { - intRT =0; $ for(; idx; idx-= (IDX & (-idx ))) $RT + =Ar[idx]; - returnRT; - } the }indexedtree; - WuyitypedefclassEvent { the Public: - intx, Y, Val; Wu -Event (intx =0,inty =0,intval =0): X (x), Y (y), Val (val) {} About $Booleanoperator< (Event b)Const { - returnX <b.x; - } - }event; A + intN; the intXs[n], ys[n], bxs[n], bys[n]; - intLs[n], rs[n], us[n], ds[n]; $ll res =0; the intTP =0; theEvent Es[n <<1]; thevector<int>Vs[n]; the Indexedtree it; - inInlinevoidinit () { thescanf"%d", &n); the for(inti =1; I <= N; i++) Aboutscanf"%d%d", XS + i, Ys +i); the } the theInlinevoidDescrete (int* AR,int* BR,intN) { +memcpy (BR, AR,sizeof(int) * (n +1)); -Sort (br +1, BR + n +1); the for(inti =1; I <= N; i++)BayiAr[i] = Lower_bound (br +1, BR + n +1, Ar[i])-BR; the } the -Inlinevoidsolve () { - Descrete (XS, Bxs, n); the Descrete (ys, Bys, N); the the for(inti =1; I <= N; i++) theLs[i] = ds[i] =N; - for(inti =1; I <= N; i++) theUs[i] = rs[i] =0; the the for(inti =1; I <= N; i++) {94 smin (Ls[ys[i]], xs[i]); the Smax (Rs[ys[i]], xs[i]); the smin (Ds[xs[i]], ys[i]); the Smax (Us[xs[i]], ys[i]);98 } About - for(inti =1; I <= N; i++)101 Vs[xs[i]].push_back (Ys[i]);102 103 for(inti =1; I <= N; i++)104 if(Ls[i] < rs[i]-1) { theES[++TP] = Event (Ls[i], I,1);106ES[++TP] = Event (Rs[i], I,-1);107 }108Sort (es +1, ES + TP +1);109 the intPE =1;111it =indexedtree (n); thebxs[0] = -1e9-5;113 for(inti =1; I <= N; i++) { the if(Bxs[i] = = Bxs[i-1])Continue; the while(PE <= TP && es[pe].x = =i) theIt.add (Es[pe].y, es[pe].val), pe++;117 if(Ds[i] <=Us[i])118Res + = It.query (Us[i])-It.query (Ds[i]-1);//, Cerr << bxs[i] << Endl;119 for(intj =0; J < (Signed) vs[i].size (); J + +) - if(It.query (Vs[i][j])-It.query (Vs[i][j]-1) ==0)121res++;122 //cerr << bxs[i] << "" << res << Endl;123 }124printf (Auto"\ n", res); the }126 127 intMain () { - init ();129 solve (); the return 0;131}
Bzoj 1818 [Cqoi 2010] Internal White Point-scan line-tree array