fork download
  1. // 私はあなたがいなくて寂しいです
  2. // 你可以拖延,但时间不会。
  3. // 雪花飄飄北風嘯嘯
  4. // 天地一片蒼茫
  5. /**
  6.  * author: semicolon
  7. */
  8. #include <iostream>
  9. #include <vector>
  10. #include <cmath>
  11. #include <algorithm>
  12. #include <set>
  13. #include <map>
  14. #include <unordered_map>
  15. #include <unordered_set>
  16. #include <sstream>
  17. #include <utility>
  18. #include <numeric>
  19. #include <iomanip>
  20. #include <deque>
  21. #include <queue>
  22. #include <stack>
  23. #include <list>
  24. #include <iterator>
  25. #include <climits>
  26. #include <bitset>
  27. #include <cassert>
  28. #include <cstdio>
  29. #include <cstdlib>
  30. #include <cstring>
  31. #include <time.h>
  32. #include <functional>
  33. #include <forward_list>
  34. #include <tuple>
  35. #include <chrono>
  36. #include <random>
  37. //#pragma GCC optimize("O2,O3,unroll-loops")
  38. //#pragma GCC target("avx,avx2,fma")
  39. // #pragma GCC optimize("O2")
  40. // #pragma GCC optimize("Ofast,fast-math")
  41. // #pragma GCC optimize("no-stack-protector")
  42. // #pragma GCC optimize("inline")
  43. // #pragma GCC optimize("tree-vectorize")
  44. // #pragma GCC optimize("tree-loop-distribute-patterns")
  45. // #pragma GCC optimize("tree-loop-distribute-decls")
  46. // #pragma GCC optimize("tree-loop-improve")
  47. // #pragma GCC optimize("tree-loop-vectorize")
  48. // #pragma GCC optimize("tree-vectorize-loops")
  49. // #pragma GCC optimize("tree-vectorize-loops-aggressive")
  50. // #pragma GCC optimize("tree-vectorize-loops-optimized")
  51. // #pragma GCC optimize("tree-vectorize-loops-optimized-aggressive")
  52. // #pragma GCC optimize("unroll-loops")
  53. // #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
  54.  
  55. using namespace std;
  56. void debug_out() {cout << endl;}
  57. template<typename Head, typename ...Tail>
  58. void debug_out(Head H, Tail ...T) {
  59. cout << H << ' ';
  60. debug_out(T...);
  61. }
  62. void Semicolon();
  63.  
  64. #define coutmatrix(a,n,m) for(int i = 0; i < n; i++) {for(int j = 0; j < m; j++) {cout << a[i][j] << " ";}cout << endl;}
  65. #define coutarr(a,n) for(int ix = 0;ix < n;ix++) {cout << a[ix] << " ";} cout << endl;
  66. #define coutvec(a) for(int ix = 0;ix < a.size();ix++) {cout << a[ix] << " ";} cout << endl
  67. #define runtime cerr<< "Time elapsed: " << (1.0*clock()/CLOCKS_PER_SEC)<< "s" << endl;
  68. #define debug(...) cout << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
  69. #define For(i,n) for(int i = 0; i < n; i++)
  70. #define Fod(i,a,b) for(int i = a; i <= b; i++)
  71. #define Rev(i,n) for(int i = n-1; i >= 0; i++) //reverse
  72. #define IOS ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  73. #define tcase() int t; cin >> t; while (t--) {Semicolon();};
  74. #define all(x) (x).begin(),(x).end()
  75. #define sz(x) ((int)(x).size())
  76. #define setpr(x) cout<<fixed<<setprecision(x)
  77. #define freo(s) freopen(s".INP","r",stdin); freopen(s".OUT","w",stdout);
  78. #define fx(a,n) a,a+n
  79. #define str stringstream
  80. #define endl '\n'
  81. #define pb push_back
  82. #define pf push_front
  83. #define popb pop_back
  84. #define popf pop_front
  85. #define eb emplace_back
  86. #define ef emplace_front
  87. #define ins insert
  88. #define le length
  89. #define fi first
  90. #define se second
  91. #define mkp make_pair
  92. #define ci1(a) cin >> a
  93. #define ci2(a, b) cin >> a >> b
  94. #define ci3(a, b, c) cin >> a >> b >> c
  95. #define ci4(a, b, c, d) cin >> a >> b >> c >> d
  96. #define MXINT = INT_MAX
  97. #define MNINT = INT_MIN
  98. #define MXLONG = LONG_MAX
  99. #define MNLONG = LONG_MIN
  100. #define vvi vector<vector<int>>
  101. #define vvl vector<vector<long long>>
  102. #define segleft id<<1
  103. #define segright id<<1|1
  104. #define MASK(i) (1LL << (i))
  105. #define BIT(x, i) (((x)>>(i)) & 1)
  106. using ll = signed long long int;
  107. using ull = unsigned long long int;
  108. using ld = long double;
  109. using lld = long double;
  110. using pii = pair<int, int>;
  111. using pll = pair<long long, long long>;
  112. using pil = pair<int, long long>;
  113. using pli = pair<long long, int>;
  114. using vi = vector<int>;
  115. using vl = vector<long long>;
  116. using qi = queue<int>;
  117. using q_pii = queue<pair<int,int>>;
  118. using q_pll = queue<pair<long long,long long>>;
  119. using mii = map<int,int>;
  120. using mll = map<long long,long long>;
  121. using mp_ivi = map<int, vector<int>>;
  122. using mp_lvl = map<long long, vector<long long>>;
  123. using mulmpi = multimap<int, int>;
  124. using mulmpl = multimap<long long, long long>;
  125. using dqi = deque<int>;
  126. using dql = deque<long long>;
  127. using sti = stack<int>;
  128. using stl = stack<long long>;
  129. using stpii = stack<pair<int,int>>;
  130. using stpll = stack<pair<long long,long long>>;
  131. using vpii = vector<pair<int,int>>;
  132. using vpll = vector<pair<long long,long long>>;
  133. using vpil = vector<pair<int,long long>>;
  134. using vpli = vector<pair<long long,int>>;
  135. using si = set<int>;
  136. using sl = set<long long>;
  137. const ll MOD = 1e9+7;
  138. const ll MAXLL = LLONG_MAX;
  139. const ll NMAXLL = -MAXLL;
  140. const ll INF = INT_MAX;
  141. const ll INFL = LLONG_MAX;
  142. const long double pi = acos(-1.0);
  143. const char compass[4] = {'N','W','E','S'};
  144. const char dir[4] = {'U','R','D','L'};
  145. const int dx[4] = {-1, 0, 1, 0};
  146. const int dy[4] = {0, 1, 0, -1};
  147. /// 0 = up (U), 1 = right (R), 2 = down (D), 3 = left (L)
  148. const int dXy[] = {0, 0, -1, 1, 1, -1, 1, -1};
  149. const int dYx[] = {-1, 1, 0, 0, 1, 1, -1, -1};
  150.  
  151. template<class X, class Y>
  152. bool minimize(X &x, const Y &y) {
  153. if (x > y) {
  154. x = y;
  155. return true;
  156. }
  157. return false;
  158. }
  159. template<class X, class Y>
  160. bool maximize(X &x, const Y &y) {
  161. if (x < y) {
  162. x = y;
  163. return true;
  164. }
  165. return false;
  166. }
  167.  
  168. template<typename T>
  169. class range {
  170. private:
  171. T last;
  172. T itr;
  173. public:
  174. range(T end, T start = 0) : last(end), itr(start) {}
  175. const range& begin() const { return *this; }
  176. const range& end() const { return *this; }
  177. bool operator!=(const range&) const { return itr < last; }
  178. void operator++() { ++itr; }
  179. T operator*() const { return itr; }
  180. };
  181. #define int long long
  182. //#define ll long long
  183. void iofile(string s) {
  184. freopen((s + ".inp").c_str(), "r", stdin);
  185. freopen((s + ".out").c_str(), "w", stdout);
  186. }
  187. int readInt() {
  188. int x; cin >> x; return x;
  189. }
  190.  
  191. //#define LENGTH 1000005
  192. //#define NMOD 2
  193. //#define BASE 311
  194. //const int Hashmod[] = {(int)1e9 + 7, (int)1e9 + 2277, (int)1e9 + 5277, (int)1e9 + 8277, (int)1e9 + 9277};
  195.  
  196. //mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
  197. //mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  198. //mt19937 rnf(2106);
  199.  
  200. struct BIT {
  201. vector<int> bit1, bit2;
  202. int n;
  203.  
  204. void init(int _n) {
  205. n = _n;
  206. bit1.assign(n+5, 0);
  207. bit2.assign(n+5, 0);
  208. }
  209.  
  210. void updatePoint(vector<int>& b, int u, int v) {
  211. int idx = u;
  212. while (idx <= n) {
  213. b[idx] += v;
  214. idx += (idx & (-idx));
  215. }
  216. }
  217.  
  218. void updateRange(int l, int r, int v) {
  219. updatePoint(bit1, l, (n - l + 1) * v);
  220. updatePoint(bit1, r + 1, -(n - r) * v);
  221. updatePoint(bit2, l, v);
  222. updatePoint(bit2, r + 1, -v);
  223. }
  224.  
  225. int getsum(vector<int>& b, int u) {
  226. int idx = u, ans = 0;
  227. while (idx > 0) {
  228. ans += b[idx];
  229. idx -= (idx & (-idx));
  230. }
  231. return ans;
  232. }
  233.  
  234. int prefixsum(int u) {
  235. return getsum(bit1, u) - getsum(bit2, u) * (n - u);
  236. }
  237.  
  238. int rangesum(int l, int r) {
  239. return prefixsum(r) - prefixsum(l - 1);
  240. }
  241. };
  242.  
  243. struct query {
  244. int l, r, id;
  245. };
  246.  
  247. BIT bit;
  248. const int N = 2e5+5;
  249. const int T = 2e5;
  250. vector<pair<int,int>> have;
  251. vector<int> ans(N);
  252. vector<query> q;
  253. int a[N];
  254. int n, que;
  255.  
  256. void bs(int l, int r, vector<query> Q, vector<pair<int,int>> can) {
  257. if (l==r || Q.empty()) {
  258. for(auto p : Q) {
  259. ans[p.id] = l;
  260. }
  261. return;
  262. }
  263.  
  264. int mid = l+r>>1;
  265.  
  266. vector<pair<int,int>> L, R;
  267. vector<query> QL, QR;
  268.  
  269.  
  270. for(auto [val, id] : can) {
  271. if (val <= mid) {
  272. bit.updateRange(id, id, 1);
  273. L.push_back({val, id});
  274. } else {
  275. R.push_back({val, id});
  276. }
  277. }
  278.  
  279.  
  280. for(int i = 0 ; i < sz(Q); i++) {
  281. int tmp = bit.rangesum(Q[i].l, Q[i].r);
  282. int len = Q[i].r - Q[i].l + 1;
  283. int check = (len+1)/2;
  284.  
  285.  
  286. if (tmp >= check) {
  287. ans[Q[i].id] = mid;
  288. QL.push_back(Q[i]);
  289. } else {
  290. QR.push_back(Q[i]);
  291. }
  292. }
  293.  
  294.  
  295. bit.init(n+5);
  296. bs(l, mid, QL, L);
  297. bs(mid+1, r, QR, R);
  298. }
  299.  
  300. void Semicolon() {
  301. cin >> n >> que;
  302.  
  303. for(int i = 1; i <= n; i++) {
  304. cin >> a[i];
  305. have.push_back({a[i], i});
  306. }
  307.  
  308. for(int i = 1; i <= que; i++) {
  309. int l, r; cin >> l >> r;
  310. q.push_back({l, r, i});
  311. }
  312.  
  313. bit.init(n+5);
  314. bs(1, T, q, have);
  315.  
  316. for(int i = 1; i <= que; i++) {
  317. cout << ans[i] << endl;
  318. }
  319. }
  320.  
  321. signed main() {
  322. IOS
  323. //freopen("input.txt", "r", stdin);
  324. //iofile("");
  325. //freo("")
  326. //tcase();
  327. Semicolon();
  328. return 0;
  329. }
  330.  
  331. /*
  332.   .........
  333.   .........
  334.   ..... .....=*%%##*-.....
  335.   ...-##%%%%#+........-%@*------+#%%#................
  336.   .:%%%#+++#%%%#=:...:%%*-------::=%@%+::............
  337.   ....+%%#-------+%@@#-..%%#----+++=----+%%*=+:...::........
  338.   ......-%%#----------+#%*--++----=+++=-------=#%%%%%*=----:......
  339.   ......:#%%----+++=-------------------------------=#%%%%%+.........
  340.   ..:++=*#=----=+=------------------------------------=*%%%%=--:...
  341.   ......=%@%%%+-------------------------------------------------=#%@@*:...
  342.   ...-#%%%#+-------------------------------------------------------+%%%*.....
  343.   ..#@%%+------------------------------------------------------------+%@@-....
  344.   ....+%@%+------------------------------------------------------------::--#%%+.....
  345.   ..:#%%*-:-----------------------------------------------------------------+%%*:....
  346.   ....=%%%=:--------------------------------------------------------------------=%@#:...
  347.   ...=%%*------------------------------------------------------------------------=#%#:..
  348.   ...=@%+---------------------------------------....:------------------------------=#%#:..
  349.   ..=%%*---------------------------------------:.....:------------------------------+%%*:...
  350.   ..-%%#=------------------------.....:----------....:--------------------------------*%@:...
  351.   ....*%%+------------------------:.....:-----------------------------------------------+%@*...
  352.   ...:%%*:-------------------------:...:-------------------------------------------------#%%...
  353.   ...#%%=--------------------------------------------------------------------------------#%%:..
  354.   ..:#%#---------------------------------------------------------------------------------*%%:...
  355.   ..=%%*---------------------------------------------------------------------------------*%%-...
  356.   ..=%@=----------------------------------------------------------------==+==------------*%@-..
  357.   ..+%@=--------------------------------------------------*%@%%==*=--=+++++++++=---------*%%-...
  358. .....-%%*-------------------------------------:-----------*%%%%%@@+-=++++++++++++=--------#%@:..
  359. .....:#%*---------------------------=**=:---------=-------+%%%%%@*--=+++++++++++++=------=%%*....
  360. ......#%%=------------------------+%%%%%#=-----=%%@%-......:%%%%+---+++++#%%@#++++-------*%@-
  361. ......+%%*------------------------*@%%%%@*-:....:*%=..............--=+%%%%%%%@@%+------+%%*.
  362. .....--%%%=------------=+++++==---=#@%%@%=.......=%@-::............---*%%-......+%@@#*%%+:..
  363. .....:-+%%*----------=+++++++++++---+##=........-#@%%%%*............---*%%-......+%@@#*%%+:..
  364. ....:---+%%+--------=+++++++++++++=--:..........*#=.................-=%%%%=........+%%%%=....
  365. ........-+%#=------==+++++++++++++=-:...............................=%%#:...........:#%%*:...
  366. .......:-%%*--------=+++++++++++++=-:..............................:%@#...............-%%#-.........
  367.   .....:@%*--------=++++++++++++=--:..........................-*%%%%@:...............:=#%%+:.......
  368.   .:%%#=---------=+++++++==----:.......................:*%%%#*%%@-..............:--=*%%#.......
  369.   ..-%%#=-----------====--------:................--===*%@*+=+=#@@:............:------*%%#......
  370.   ...-#%%+------------------------:-+++++-..:+#%%%%%%@%#+=====+%%*:........::---------+%%#:....
  371.   . ....-#@%%%####*######=-----*%%+=======*%%*======*%%+=======#%%+--------------------+%%*:..
  372.   ......+%%@%@@%%@@@@@%+-----#%%=========*%#+======#@*=======+%%%*--------------------+%%*:
  373.   ........+%@%#=----------------=*@%+======+=+%%*+**##%@%%%%%%%#%@%%%%#=-------------------+%%*-
  374.   ......+%@%*--------------------=@%#========+#%%@@@%#+-.:--=*#%*=:-+%@%=-:-----------------*%%*
  375.   ....-#%@*----------+=-----------+%%*======#%@*:...................:-*%*-------------------=#%%
  376.   ..-+%@#=----------#@#=-----------*@%#*#%@@%*.......................-------------------------#%
  377.   .:*@%*-----------+%%+-------------=*%%%#=...........................------------------------=
  378.   ...+@%+.----------=@%*-----------------:..............................:------------------------+
  379.   ...:%%*....:------=%%#------------------................................-------------------------
  380.   ...=%%=......:----+%@+-----------------:................................:------------------------
  381.   ..*%%:.........:-#%%------------------:................................:------------------------
  382.   ...*%%-..........:#%#-------------------.................................------------------------
  383.   ...-%%#:.........-#%*-------------------:................................------------------------
  384.   ....=@%#-........-#@#--------------------:..................:::.........:------------------------
  385.   ........ .. ....-#%%**##%@%*-........ .. ..
  386. */
  387.  
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
Standard output is empty