fork download
  1. #define LOCAL
  2.  
  3. /** ` Micro Mezzo Macro Flation -- Overheated Economy ., **/
  4.  
  5. #include <algorithm>
  6. #include <iostream>
  7. #include <iomanip>
  8. #include <sstream>
  9. #include <cstring>
  10. #include <cstdio>
  11. #include <string>
  12. #include <vector>
  13. #include <bitset>
  14. #include <queue>
  15. #include <stack>
  16. #include <cmath>
  17. #include <ctime>
  18. #include <list>
  19. #include <set>
  20. #include <map>
  21.  
  22. using namespace std;
  23.  
  24. #define REP(i, n) for (int i=0;i<int(n);++i)
  25. #define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
  26. #define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
  27. #define REP_1(i, n) for (int i=1;i<=int(n);++i)
  28. #define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)
  29. #define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)
  30. #define REP_C(i, n) for (int n____=int(n),i=0;i<n____;++i)
  31. #define FOR_C(i, a, b) for (int b____=int(b),i=a;i<b____;++i)
  32. #define DWN_C(i, b, a) for (int a____=int(a),i=b-1;i>=a____;--i)
  33. #define REP_N(i, n) for (i=0;i<int(n);++i)
  34. #define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)
  35. #define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)
  36. #define REP_1_C(i, n) for (int n____=int(n),i=1;i<=n____;++i)
  37. #define FOR_1_C(i, a, b) for (int b____=int(b),i=a;i<=b____;++i)
  38. #define DWN_1_C(i, b, a) for (int a____=int(a),i=b;i>=a____;--i)
  39. #define REP_1_N(i, n) for (i=1;i<=int(n);++i)
  40. #define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)
  41. #define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)
  42. #define REP_C_N(i, n) for (n____=int(n),i=0;i<n____;++i)
  43. #define FOR_C_N(i, a, b) for (b____=int(b),i=a;i<b____;++i)
  44. #define DWN_C_N(i, b, a) for (a____=int(a),i=b-1;i>=a____;--i)
  45. #define REP_1_C_N(i, n) for (n____=int(n),i=1;i<=n____;++i)
  46. #define FOR_1_C_N(i, a, b) for (b____=int(b),i=a;i<=b____;++i)
  47. #define DWN_1_C_N(i, b, a) for (a____=int(a),i=b;i>=a____;--i)
  48.  
  49. #define ECH(it, A) for (typeof(A.begin()) it=A.begin(); it != A.end(); ++it)
  50. #define DO(n) while(n--)
  51. #define DO_C(n) int n____ = n; while(n____--)
  52. #define TO(i, a, b) int s_=a<b?1:-1,b_=b+s_;for(int i=a;i!=b_;i+=s_)
  53. #define TO_1(i, a, b) int s_=a<b?1:-1,b_=b;for(int i=a;i!=b_;i+=s_)
  54. #define SQZ(i, j, a, b) for (int i=int(a),j=int(b)-1;i<j;++i,--j)
  55. #define SQZ_1(i, j, a, b) for (int i=int(a),j=int(b);i<=j;++i,--j)
  56. #define REP_2(i, j, n, m) REP(i, n) REP(j, m)
  57. #define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)
  58.  
  59. #define ALL(A) A.begin(), A.end()
  60. #define LLA(A) A.rbegin(), A.rend()
  61. #define CPY(A, B) memcpy(A, B, sizeof(A))
  62. #define INS(A, P, B) A.insert(A.begin() + P, B)
  63. #define ERS(A, P) A.erase(A.begin() + P)
  64. #define BSC(A, X) find(ALL(A), X) // != A.end()
  65. #define CTN(T, x) (T.find(x) != T.end())
  66. #define SZ(A) int(A.size())
  67. #define PB push_back
  68. #define MP(A, B) make_pair(A, B)
  69.  
  70. #define Rush int T____; RD(T____); DO(T____)
  71. #pragma comment(linker, "/STACK:36777216")
  72. //#pragma GCC optimize ("O2")
  73. #define Ruby system("ruby main.rb")
  74. #define Haskell system("runghc main.hs")
  75. #define Pascal system("fpc main.pas")
  76.  
  77. typedef long long LL;
  78. typedef double DB;
  79. typedef unsigned UINT;
  80. typedef unsigned long long ULL;
  81.  
  82. typedef vector<int> VI;
  83. typedef vector<char> VC;
  84. typedef vector<string> VS;
  85. typedef vector<LL> VL;
  86. typedef vector<DB> VD;
  87. typedef set<int> SI;
  88. typedef set<string> SS;
  89. typedef set<LL> SL;
  90. typedef set<DB> SD;
  91. typedef map<int, int> MII;
  92. typedef map<string, int> MSI;
  93. typedef map<LL, int> MLI;
  94. typedef map<DB, int> MDI;
  95. typedef map<int, bool> MIB;
  96. typedef map<string, bool> MSB;
  97. typedef map<LL, bool> MLB;
  98. typedef map<DB, bool> MDB;
  99. typedef pair<int, int> PII;
  100. typedef pair<int, bool> PIB;
  101. typedef vector<PII> VII;
  102. typedef vector<VI> VVI;
  103. typedef vector<VII> VVII;
  104. typedef set<PII> SII;
  105. typedef map<PII, int> MPIII;
  106. typedef map<PII, bool> MPIIB;
  107.  
  108. /** I/O Accelerator **/
  109.  
  110. /* ... :" We are I/O Accelerator ... Use us at your own risk ;) ... " .. */
  111.  
  112. template<class T> inline void RD(T &);
  113. template<class T> inline void OT(const T &);
  114.  
  115. inline int RD(){ int x; RD(x); return x;}
  116. template<class T> inline T& _RD(T &x){ RD(x); return x;}
  117. inline void RC(char &c){scanf(" %c", &c);}
  118. inline void RS(char *s){scanf("%s", s);}
  119.  
  120. template<class T0, class T1> inline void RD(T0 &x0, T1 &x1){RD(x0), RD(x1);}
  121. template<class T0, class T1, class T2> inline void RD(T0 &x0, T1 &x1, T2 &x2){RD(x0), RD(x1), RD(x2);}
  122. template<class T0, class T1, class T2, class T3> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3){RD(x0), RD(x1), RD(x2), RD(x3);}
  123. template<class T0, class T1, class T2, class T3, class T4> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4);}
  124. template<class T0, class T1, class T2, class T3, class T4, class T5> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5);}
  125. template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6);}
  126. template<class T0, class T1> inline void OT(T0 &x0, T1 &x1){OT(x0), OT(x1);}
  127. template<class T0, class T1, class T2> inline void OT(T0 &x0, T1 &x1, T2 &x2){OT(x0), OT(x1), OT(x2);}
  128. template<class T0, class T1, class T2, class T3> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3){OT(x0), OT(x1), OT(x2), OT(x3);}
  129. template<class T0, class T1, class T2, class T3, class T4> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);}
  130. template<class T0, class T1, class T2, class T3, class T4, class T5> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);}
  131. template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);}
  132.  
  133. template<class T> inline void RST(T &A){memset(A, 0, sizeof(A));}
  134. template<class T0, class T1> inline void RST(T0 &A0, T1 &A1){RST(A0), RST(A1);}
  135. template<class T0, class T1, class T2> inline void RST(T0 &A0, T1 &A1, T2 &A2){RST(A0), RST(A1), RST(A2);}
  136. template<class T0, class T1, class T2, class T3> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3){RST(A0), RST(A1), RST(A2), RST(A3);}
  137. template<class T0, class T1, class T2, class T3, class T4> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);}
  138. template<class T0, class T1, class T2, class T3, class T4, class T5> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);}
  139. template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);}
  140.  
  141.  
  142. template<class T> inline void CLR(priority_queue<T, vector<T>, less<T> > &Q){
  143. while (!Q.empty()) Q.pop();
  144. }
  145.  
  146. template<class T> inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q){
  147. while (!Q.empty()) Q.pop();
  148. }
  149.  
  150. template<class T> inline void CLR(T &A){A.clear();}
  151. template<class T0, class T1> inline void CLR(T0 &A0, T1 &A1){CLR(A0), CLR(A1);}
  152. template<class T0, class T1, class T2> inline void CLR(T0 &A0, T1 &A1, T2 &A2){CLR(A0), CLR(A1), CLR(A2);}
  153. template<class T0, class T1, class T2, class T3> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3){CLR(A0), CLR(A1), CLR(A2), CLR(A3);}
  154. template<class T0, class T1, class T2, class T3, class T4> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);}
  155. template<class T0, class T1, class T2, class T3, class T4, class T5> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);}
  156. template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);}
  157. template<class T> inline void CLR(T &A, int n){REP(i, n) CLR(A[i]);}
  158. template<class T> inline void FLC(T &A, int x){memset(A, x, sizeof(A));}
  159. template<class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x){FLC(A0, x), FLC(A1, x);}
  160. template<class T0, class T1, class T2> inline void FLC(T0 &A0, T1 &A1, T2 &A2){FLC(A0), FLC(A1), FLC(A2);}
  161. template<class T0, class T1, class T2, class T3> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3){FLC(A0), FLC(A1), FLC(A2), FLC(A3);}
  162. template<class T0, class T1, class T2, class T3, class T4> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4);}
  163. template<class T0, class T1, class T2, class T3, class T4, class T5> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5);}
  164. template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5), FLC(A6);}
  165.  
  166. template<class T> inline void SRT(T &A){sort(ALL(A));}
  167. template<class T, class C> inline void SRT(T &A, C B){sort(ALL(A), B);}
  168.  
  169. /** Add - On **/
  170.  
  171. const int MOD = 1000000007;
  172. const int INF = 1000000000;
  173. const DB EPS = 1e-2;
  174. const DB OO = 1e15;
  175. const DB PI = 3.14159265358979323846264; //M_PI;
  176.  
  177. // <<= ` 0. Daily Use .,
  178.  
  179. template<class T> inline void checkMin(T &a,const T b){if (b<a) a=b;}
  180. template<class T> inline void checkMax(T &a,const T b){if (b>a) a=b;}
  181. template <class T, class C> inline void checkMin(T& a, const T b, C c){if (c(b,a)) a = b;}
  182. template <class T, class C> inline void checkMax(T& a, const T b, C c){if (c(a,b)) a = b;}
  183. template<class T> inline T min(T a, T b, T c){return min(min(a, b), c);}
  184. template<class T> inline T max(T a, T b, T c){return max(max(a, b), c);}
  185. template<class T> inline T min(T a, T b, T c, T d){return min(min(a, b), min(c, d));}
  186. template<class T> inline T max(T a, T b, T c, T d){return max(min(a, b), max(c, d));}
  187. template<class T> inline T sqr(T a){return a*a;}
  188. template<class T> inline T cub(T a){return a*a*a;}
  189. int Ceil(int x, int y){return (x - 1) / y + 1;}
  190.  
  191. // <<= ` 1. Bitwise Operation .,
  192. inline bool _1(int x, int i){return x & 1<<i;}
  193. inline bool _1(LL x, int i){return x & 1LL<<i;}
  194. inline LL _1(int i){return 1LL<<i;}
  195. //inline int _1(int i){return 1<<i;}
  196. inline LL _U(int i){return _1(i) - 1;};
  197. //inline int _U(int i){return _1(i) - 1;};
  198.  
  199.  
  200. template<class T> inline T low_bit(T x) {
  201. return x & -x;
  202. }
  203.  
  204. template<class T> inline T high_bit(T x) {
  205. T p = low_bit(x);
  206. while (p != x) x -= p, p = low_bit(x);
  207. return p;
  208. }
  209.  
  210. inline int count_bits(int x){
  211. x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1);
  212. x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2);
  213. x = (x & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4);
  214. x = (x & 0x00ff00ff) + ((x & 0xff00ff00) >> 8);
  215. x = (x & 0x0000ffff) + ((x & 0xffff0000) >> 16);
  216. return x;
  217. }
  218.  
  219. inline int count_bits(LL x){
  220. x = (x & 0x5555555555555555LL) + ((x & 0xaaaaaaaaaaaaaaaaLL) >> 1);
  221. x = (x & 0x3333333333333333LL) + ((x & 0xccccccccccccccccLL) >> 2);
  222. x = (x & 0x0f0f0f0f0f0f0f0fLL) + ((x & 0xf0f0f0f0f0f0f0f0LL) >> 4);
  223. x = (x & 0x00ff00ff00ff00ffLL) + ((x & 0xff00ff00ff00ff00LL) >> 8);
  224. x = (x & 0x0000ffff0000ffffLL) + ((x & 0xffff0000ffff0000LL) >> 16);
  225. x = (x & 0x00000000ffffffffLL) + ((x & 0xffffffff00000000LL) >> 32);
  226. return x;
  227. }
  228.  
  229. int reverse_bits(int x){
  230. x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa);
  231. x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc);
  232. x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0);
  233. x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00);
  234. x = ((x >>16) & 0x0000ffff) | ((x <<16) & 0xffff0000);
  235. return x;
  236. }
  237.  
  238. LL reverse_bits(LL x){
  239. x = ((x >> 1) & 0x5555555555555555LL) | ((x << 1) & 0xaaaaaaaaaaaaaaaaLL);
  240. x = ((x >> 2) & 0x3333333333333333LL) | ((x << 2) & 0xccccccccccccccccLL);
  241. x = ((x >> 4) & 0x0f0f0f0f0f0f0f0fLL) | ((x << 4) & 0xf0f0f0f0f0f0f0f0LL);
  242. x = ((x >> 8) & 0x00ff00ff00ff00ffLL) | ((x << 8) & 0xff00ff00ff00ff00LL);
  243. x = ((x >>16) & 0x0000ffff0000ffffLL) | ((x <<16) & 0xffff0000ffff0000LL);
  244. x = ((x >>32) & 0x00000000ffffffffLL) | ((x <<32) & 0xffffffff00000000LL);
  245. return x;
  246. }
  247.  
  248. // <<= ` 2. Modular Arithmetic Basic .,
  249.  
  250. inline void INC(int &a, int b){a += b; if (a >= MOD) a -= MOD;}
  251. inline int sum(int a, int b){a += b; if (a >= MOD) a -= MOD; return a;}
  252. inline void DEC(int &a, int b){a -= b; if (a < 0) a += MOD;}
  253. inline int dff(int a, int b){a -= b; if (a < 0) a += MOD; return a;}
  254. inline void MUL(int &a, int b){a = (LL)a * b % MOD;}
  255. inline int pdt(int a, int b){return (LL)a * b % MOD;}
  256.  
  257. inline int pow(int a, int b){
  258. int c = 1;
  259. while (b) {
  260. if (b&1) MUL(c, a);
  261. MUL(a, a), b >>= 1;
  262. }
  263. return c;
  264. }
  265.  
  266. template<class T>
  267. inline int pow(T a, int b){
  268. T c(1);
  269. while (b) {
  270. if (b&1) MUL(c, a);
  271. MUL(a, a), b >>= 1;
  272. }
  273. return c;
  274. }
  275.  
  276. inline int _I(int b){
  277. int a = MOD, x1 = 0, x2 = 1, q;
  278. while (true){
  279. q = a / b, a %= b;
  280. if (!a) return (x2 + MOD) % MOD;
  281. DEC(x1, pdt(q, x2));
  282.  
  283. q = b / a, b %= a;
  284. if (!b) return (x1 + MOD) % MOD;
  285. DEC(x2, pdt(q, x1));
  286. }
  287. }
  288.  
  289. inline void DIV(int &a, int b){MUL(a, _I(b));}
  290. inline int qtt(int a, int b){return pdt(a, _I(b));}
  291.  
  292. inline int sum(int a, int b, int MOD){
  293. a += b; if (a >= MOD) a -= MOD;
  294. return a;
  295. }
  296.  
  297. inline int phi(int n){
  298. int res = n;
  299. for (int i=2;sqr(i)<=n;++i) if (!(n%i)){
  300. DEC(res, qtt(res, i));
  301. do{n /= i;} while(!(n%i));
  302. }
  303. if (n != 1)
  304. DEC(res, qtt(res, n));
  305. return res;
  306. }
  307.  
  308. // <<= '9. Comutational Geometry .,
  309.  
  310. struct Po; struct Line; struct Seg;
  311.  
  312. inline int sgn(DB x){return x < -EPS ? -1 : x > EPS;}
  313. inline int sgn(DB x, DB y){return sgn(x - y);}
  314.  
  315. struct Po{
  316. DB x, y;
  317. Po(DB _x = 0, DB _y = 0):x(_x), y(_y){}
  318.  
  319. friend istream& operator >>(istream& in, Po &p){return in >> p.x >> p.y;}
  320. friend ostream& operator <<(ostream& out, Po p){return out << "(" << p.x << ", " << p.y << ")";}
  321.  
  322. friend bool operator ==(Po, Po);
  323. friend bool operator !=(Po, Po);
  324. friend Po operator +(Po, Po);
  325. friend Po operator -(Po, Po);
  326. friend Po operator *(Po, DB);
  327. friend Po operator /(Po, DB);
  328.  
  329. bool operator < (const Po &rhs) const{return sgn(x, rhs.x) < 0 || sgn(x, rhs.x) == 0 && sgn(y, rhs.y) < 0;}
  330. Po operator-() const{return Po(-x, -y);}
  331. Po& operator +=(Po rhs){x += rhs.x, y += rhs.y; return *this;}
  332. Po& operator -=(Po rhs){x -= rhs.x, y -= rhs.y; return *this;}
  333. Po& operator *=(DB k){x *= k, y *= k; return *this;}
  334. Po& operator /=(DB k){x /= k, y /= k; return *this;}
  335.  
  336. DB length_sqr(){return sqr(x) + sqr(y);}
  337. DB length(){return sqrt(length_sqr());}
  338.  
  339. DB atan(){
  340. return atan2(y, x);
  341. }
  342.  
  343. void input(){
  344. scanf("%lf %lf", &x, &y);
  345. }
  346. };
  347.  
  348. bool operator ==(Po a, Po b){return sgn(a.x - b.x) == 0 && sgn(a.y - b.y) == 0;}
  349. bool operator !=(Po a, Po b){return sgn(a.x - b.x) != 0 || sgn(a.y - b.y) != 0;}
  350. Po operator +(Po a, Po b){return Po(a.x + b.x, a.y + b.y);}
  351. Po operator -(Po a, Po b){return Po(a.x - b.x, a.y - b.y);}
  352. Po operator *(Po a, DB k){return Po(a.x * k, a.y * k);}
  353. Po operator *(DB k, Po a){return a * k;}
  354. Po operator /(Po a, DB k){return Po(a.x / k, a.y / k);}
  355.  
  356. struct Line{
  357. Po a, b;
  358. Line(Po _a = Po(), Po _b = Po()):a(_a), b(_b){}
  359. Line(DB x0, DB y0, DB x1, DB y1):a(Po(x0, y0)), b(Po(x1, y1)){}
  360. Line(Seg);
  361.  
  362. friend ostream& operator <<(ostream& out, Line p){return out << p.a << "-" << p.b;}
  363. };
  364.  
  365. struct Seg{
  366. Po a, b;
  367. Seg(Po _a = Po(), Po _b = Po()):a(_a), b(_b){}
  368. Seg(DB x0, DB y0, DB x1, DB y1):a(Po(x0, y0)), b(Po(x1, y1)){}
  369. Seg(Line l);
  370.  
  371. friend ostream& operator <<(ostream& out, Seg p){return out << p.a << "-" << p.b;}
  372. DB length(){return (b - a).length();}
  373. };
  374.  
  375. Line::Line(Seg l):a(l.a), b(l.b){}
  376. Seg::Seg(Line l):a(l.a), b(l.b){}
  377.  
  378. #define innerProduct dot
  379. #define scalarProduct dot
  380. #define dotProduct dot
  381. #define outerProduct det
  382. #define crossProduct det
  383.  
  384. inline DB dot(DB x1, DB y1, DB x2, DB y2){return x1 * x2 + y1 * y2;}
  385. inline DB dot(Po a, Po b){return dot(a.x, a.y, b.x, b.y);}
  386. inline DB dot(Po p0, Po p1, Po p2){return dot(p1 - p0, p2 - p0);}
  387. inline DB dot(Line l1, Line l2){return dot(l1.b - l1.a, l2.b - l2.a);}
  388. inline DB det(DB x1, DB y1, DB x2, DB y2){return x1 * y2 - x2 * y1;}
  389. inline DB det(Po a, Po b){return det(a.x, a.y, b.x, b.y);}
  390. inline DB det(Po p0, Po p1, Po p2){return det(p1 - p0, p2 - p0);}
  391. inline DB det(Line l1, Line l2){return det(l1.b - l1.a, l2.b - l2.a);}
  392.  
  393. template<class T1, class T2> inline DB dist(T1 x, T2 y){return sqrt(dist_sqr(x, y));}
  394.  
  395. inline DB dist_sqr(Po a, Po b){return sqr(a.x - b.x) + sqr(a.y - b.y);}
  396. inline DB dist_sqr(Po p, Line l){Po v0 = l.b - l.a, v1 = p - l.a; return sqr(fabs(det(v0, v1))) / v0.length_sqr();}
  397. inline DB dist_sqr(Po p, Seg l){
  398. Po v0 = l.b - l.a, v1 = p - l.a, v2 = p - l.b;
  399. if (sgn(dot(v0, v1)) * sgn(dot(v0, v2)) <= 0) return dist_sqr(p, Line(l));
  400. else return min(v1.length_sqr(), v2.length_sqr());
  401. }
  402.  
  403. inline DB dist_sqr(Line l, Po p){return dist_sqr(p, l);}
  404. inline DB dist_sqr(Seg l, Po p){return dist_sqr(p, l);}
  405.  
  406. inline DB dist_sqr(Line l1, Line l2){
  407. if (sgn(det(l1, l2)) != 0) return 0;
  408. return dist_sqr(l1.a, l2);
  409. }
  410. inline DB dist_sqr(Line l1, Seg l2){
  411. Po v0 = l1.b - l1.a, v1 = l2.a - l1.a, v2 = l2.b - l1.a; DB c1 = det(v0, v1), c2 = det(v0, v2);
  412. return sgn(c1) != sgn(c2) ? 0 : sqr(min(fabs(c1), fabs(c2))) / v0.length_sqr();
  413. }
  414.  
  415. bool isIntersect(Seg l1, Seg l2){
  416.  
  417. //if (l1.a == l2.a || l1.a == l2.b || l1.b == l2.a || l1.b == l2.b) return true;
  418.  
  419. return
  420. min(l1.a.x, l1.b.x) <= max(l2.a.x, l2.b.x) &&
  421. min(l2.a.x, l2.b.x) <= max(l1.a.x, l1.b.x) &&
  422. min(l1.a.y, l1.b.y) <= max(l2.a.y, l2.b.y) &&
  423. min(l2.a.y, l2.b.y) <= max(l1.a.y, l1.b.y) &&
  424. sgn( det(l1.a, l2.a, l2.b) ) * sgn( det(l1.b, l2.a, l2.b) ) <= 0 &&
  425. sgn( det(l2.a, l1.a, l1.b) ) * sgn( det(l2.b, l1.a, l1.b) ) <= 0;
  426.  
  427. }
  428.  
  429. inline DB dist_sqr(Seg l1, Seg l2){
  430. if (isIntersect(l1, l2)) return 0;
  431. else return min(dist_sqr(l1.a, l2), dist_sqr(l1.b, l2), dist_sqr(l2.a, l1), dist_sqr(l2.b, l1));
  432. }
  433.  
  434. inline bool isOnExtremePoint(const Po &p, const Seg &l){
  435. return p == l.a || p == l.b;
  436. }
  437.  
  438. inline bool isOnseg(const Po &p, const Seg &l){
  439.  
  440. //if (p == l.a || p == l.b) return false;
  441.  
  442. return sgn(det(p, l.a, l.b)) == 0 &&
  443. sgn(l.a.x, p.x) * sgn(l.b.x, p.x) <= 0 && sgn(l.a.y, p.y) * sgn(l.b.y, p.y) <= 0;
  444. }
  445.  
  446. inline Po intersect(const Line &l1, const Line &l2){
  447. return l1.a + (l1.b - l1.a) * (det(l2.a, l1.a, l2.b) / det(l2, l1));
  448. }
  449.  
  450. // perpendicular foot
  451. inline Po intersect(const Po & p, const Line &l){
  452. return intersect(Line(p, p + Po(l.a.y - l.b.y, l.b.x - l.a.x)), l);
  453. }
  454.  
  455. inline Po rotate(Po p, DB alpha, Po o = Po()){
  456. p.x -= o.x, p.y -= o .y;
  457. return Po(p.x * cos(alpha) - p.y * sin(alpha), p.y * cos(alpha) + p.x * sin(alpha)) + o;
  458. }
  459.  
  460. // <<= ' A. Random Event ..
  461.  
  462. inline int rand32(){return (bool(rand() & 1) << 30) | (rand() << 15) + rand();}
  463. inline int random32(int l, int r){return rand32() % (r - l + 1) + l;}
  464. inline int random(int l, int r){return rand() % (r - l + 1) + l;}
  465. int dice(){return rand() % 6;}
  466. bool coin(){return rand() % 2;}
  467.  
  468. // <<= ' 0. I/O Accelerator interface .,
  469.  
  470. template<class T> inline void RD(T &x){
  471. //cin >> x;
  472. scanf("%d", &x);
  473. //char c; for (c = getchar(); c < '0'; c = getchar()); x = c - '0'; for (c = getchar(); c >= '0'; c = getchar()) x = x * 10 + c - '0';
  474. //char c; c = getchar(); x = c - '0'; for (c = getchar(); c >= '0'; c = getchar()) x = x * 10 + c - '0';
  475. }
  476.  
  477. template<class T> inline void OT(const T &x){
  478. printf("%d\n", x);
  479. }
  480.  
  481. /* .................................................................................................................................. */
  482.  
  483. const int N = 50009, M = 10009;
  484.  
  485. const int NN = 2500009;
  486.  
  487. int l[NN], r[NN], c[NN], total;
  488.  
  489. PII A[N+M]; int B[N+M], Q[M][3];
  490. int S[N], C[N], Null;
  491. int n, m, An, Tn;
  492.  
  493. #define lx l[x]
  494. #define rx r[x]
  495. #define ly l[y]
  496. #define ry r[y]
  497. #define cx c[x]
  498. #define cy c[y]
  499.  
  500. #define mid ((ll+rr)>>1)
  501. #define lc lx, ll, mid
  502. #define rc rx, mid+1, rr
  503.  
  504.  
  505. void Build(int &x, int ll, int rr){
  506. x = ++total; if (ll < rr) Build(lc), Build(rc);
  507. }
  508.  
  509. int Insert(int y, int p, int d){
  510.  
  511. int x = ++total, root = x;
  512.  
  513. c[x] = c[y] + d; int ll = 0, rr = Tn;
  514.  
  515. while (ll < rr){
  516. if (p <= mid){
  517. lx = ++total, rx = ry;
  518. x = lx, y = ly, rr = mid;
  519. }
  520. else {
  521. lx = ly, rx = ++total;
  522. x = rx, y = ry, ll = mid + 1;
  523. }
  524. c[x] = c[y] + d;
  525. }
  526.  
  527. return root;
  528. }
  529.  
  530.  
  531. struct Pack{
  532. VI L;
  533.  
  534. inline Pack(){}
  535. inline Pack(int x){L.PB(x);}
  536.  
  537. inline void operator += (int x){
  538. L.PB(x);
  539. }
  540.  
  541. inline operator int() const{
  542. int res = 0; REP(i, SZ(L)) res += c[l[L[i]]];
  543. return res;
  544. }
  545.  
  546. void lt(){
  547. REP(i, SZ(L)) L[i] = l[L[i]];
  548. }
  549. void rt(){
  550. REP(i, SZ(L)) L[i] = r[L[i]];
  551. }
  552.  
  553. };
  554.  
  555.  
  556.  
  557. void Modify(int x, int p, int d){
  558. while (x <= n) C[x] = Insert(C[x], p, d), x += low_bit(x);
  559. }
  560.  
  561. Pack Query(int x){
  562. Pack res; while (x) res += C[x], x ^= low_bit(x);
  563. return res;
  564. }
  565.  
  566. int Query(int ll, int rr, int k){
  567.  
  568. --ll; Pack a = Query(rr), b = Query(ll), c = S[rr], d = S[ll];
  569.  
  570. int t; ll = 0, rr = Tn;
  571.  
  572. while (ll < rr){
  573. if ((t = a - b + c - d) >= k){
  574. a.lt(), b.lt(), c.lt(), d.lt();
  575. rr = mid;
  576. }
  577. else {
  578. a.rt(), b.rt(), c.rt(), d.rt();
  579. k -= t, ll = mid + 1;
  580. }
  581. }
  582.  
  583. return ll;
  584. }
  585.  
  586.  
  587. int main(){
  588.  
  589. #ifndef ONLINE_JUDGE
  590. freopen("in.txt", "r", stdin);
  591. //freopen("out.txt", "w", stdout);
  592. #endif
  593.  
  594. #define key first
  595. #define id second
  596.  
  597. RD(n, m); REP(i, n) A[i] = MP(RD(), i);
  598.  
  599. An = n; char cmd; REP(i, m){
  600. RC(cmd); if(cmd == 'Q') RD(Q[i][0], Q[i][1], Q[i][2]);
  601. else RD(Q[i][0]), Q[i][2] = 0, A[An++] = MP(RD(), An);
  602. }
  603.  
  604. sort(A, A + An), B[A[0].id] = Tn = 0;
  605. FOR(i, 1, An){
  606. if(A[i].key != A[i-1].key) A[++Tn].key = A[i].key;
  607. B[A[i].id] = Tn;
  608. }
  609.  
  610. Build(Null, 0, Tn); REP_1(i, n) C[i] = Null;
  611.  
  612. S[0] = Null; REP(i, n){
  613. S[i+1] = Insert(S[i], B[i], 1);
  614. }
  615.  
  616. An = n;
  617.  
  618. REP(i, m) if (Q[i][2]){
  619. OT(A[Query(Q[i][0], Q[i][1], Q[i][2])].key);
  620. }else{
  621. Modify(Q[i][0], B[Q[i][0]-1], -1);
  622. Modify(Q[i][0], B[Q[i][0]-1] = B[An++], 1);
  623. }
  624. }
  625.  
Success #stdin #stdout 0.02s 33240KB
stdin
http://w...content-available-to-author-only...y.com/JudgeOnline/problem.php?id=1901
stdout
Standard output is empty