classSolution{public:
vector<int>platesBetweenCandles(string s, vector<vector<int>>&queries){int n = s.length();
vector<int>preSum(n),left(n),right(n);for(int i =0, sum =0; i < n; i++){if(s[i]=='*') sum++;
preSum[i]= sum;}for(int i =0, l =-1; i < n; i++){if(s[i]=='|') l = i;
left[i]= l;}for(int i = n -1, r =-1; i >=0; i--){if(s[i]=='|') r = i;
right[i]= r;}
vector<int> ans;for(auto&query : queries){int x = right[query[0]], y = left[query[1]];
ans.push_back(x ==-1|| y ==-1|| x >= y ?0: preSum[y]- preSum[x]);}return ans;}};
classSolution{private:using ull =unsignedlonglong;
ull check(ull h, ull l1, ull l2){auto get =[](ull h, ull l)-> ull {
l =min(h, l);return((h -1)+(h - l))* l /2;};returnget(h, l1)+get(h, l2)+ h;}public:intmaxValue(int n,int index,int s){unsignedint lo =0, hi = s +1, l1 = index, l2 = n - index -1;while(lo < hi){unsignedint mi = lo +((hi - lo)>>1);check(mi, l1, l2)<= s - n ? lo = mi +1: hi = mi;}return lo;}};
classSolution{private:using ll =longlong;
ll get(ll val,const vector<int>&nums1,const vector<int>&nums2){
ll ans =0;for(ll x : nums1){int lo =0, hi = nums2.size();if(x >=0){while(lo < hi){int mi =(lo + hi)>>1;
x * nums2[mi]<= val ? lo = mi +1: hi = mi;}
ans += lo;}else{while(lo < hi){int mi =(lo + hi)>>1;
x * nums2[mi]> val ? lo = mi +1: hi = mi;}
ans += nums2.size()- lo;}}return ans;}public:
ll kthSmallestProduct(const vector<int>&nums1,const vector<int>&nums2, ll k){
ll lo =-1e10, hi =1e10;while(lo < hi){
ll mi =(lo + hi)>>1;get(mi, nums1, nums2)< k ? lo = mi +1: hi = mi;}return lo;}};
classSolution{private:int n, m;
vector<vector<int>> result;voiddfs(vector<vector<int>>&land,int x,int y){
land[x][y]=0;
result.back()[2]=max(result.back()[2], x);
result.back()[3]=max(result.back()[3], y);if(x +1< n && land[x +1][y]==1)dfs(land, x +1, y);if(y +1< m && land[x][y +1]==1)dfs(land, x, y +1);}public:
vector<vector<int>>findFarmland(vector<vector<int>>&land){
n = land.size();
m = land[0].size();for(int i =0; i < n;++i){for(int j =0; j < m;++j){if(land[i][j]==0)continue;
result.push_back({i, j, i, j});dfs(land, i, j);}}return result;}};
classSolution{private:int DIR[5][2]={{0,0},{0,1},{0,-1},{1,0},{-1,0}};public:intminCost(vector<vector<int>>&grid){int n = grid.size(), m = grid[0].size();
vector<vector<int>>cost(n,vector<int>(m, INT_MAX));
deque<tuple<int,int,int>> deq;
deq.emplace_back(0,0,0);while(!deq.empty()){auto[x, y, c]= deq.front();
deq.pop_front();if(c >= cost[x][y])continue;
cost[x][y]= c;for(int i =1; i <=4;++i){int nx = x + DIR[i][0], ny = y + DIR[i][1];if(nx <0|| ny <0|| nx == n || ny == m)continue;if(grid[x][y]== i){if(cost[nx][ny]> c) deq.emplace_front(nx, ny, c);}else{if(cost[nx][ny]> c +1) deq.emplace_back(nx, ny, c +1);}}}return cost[n -1][m -1];}};