multiset<pair<int,pair<int,int>>> s;
vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
vector<vector<int>> res;
for(int i=0;i<nums1.size();i++){
for(int j=0;j<nums2.size();j++){
pair<int,int> xx;
xx.first=nums1[i],xx.second=nums2[j];
pair<int,pair<int,int>> yy;
yy.first=nums1[i]+nums2[j];
yy.second=xx;
s.insert(yy);
}
}
auto it=s.begin();
for(int i=0;i<k&&it!=s.end();i++,it++){
pair<int,pair<int,int>> yy=*it;
pair<int,int> xx= yy.second;
vector<int> zz;
zz.push_back(xx.first),zz.push_back(xx.second);
res.push_back(zz);
}
return res;
}
struct Node{
bool end;
Node *son[26];
Node(){
end=false;
for(int i=0;i<26;i++) son[i]=NULL;
}
} *root;
/** Initialize your data structure here. */
Trie() {
root=new Node();
}
/** Inserts a word into the trie. */
void insert(string word) {
auto p=root;
for(int i=0;i<word.size();i++){
int u=word[i]-'a';
if(!p->son[u]) p->son[u]=new Node() ;
p=p->son[u];
if(i==word.size()-1) p->end=true;
}
}
/** Returns if the word is in the trie. */
bool search(string word) {
auto p=root;
for(int i=0;i<word.size();i++){
int u=word[i]-'a';
if(!p->son[u]) return false;
p=p->son[u];
if(i==word.size()-1&&!p->end) return false;
}
return true;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string word) {
auto p=root;
for(int i=0;i<word.size();i++){
int u=word[i]-'a';
if(!p->son[u]) return false;
p=p->son[u];
}
return true;
}
vector<string> words;
/** Initialize your data structure here. */
MagicDictionary() {
}
void buildDict(vector<string> dictionary) {
words=dictionary;
}
bool search(string searchWord) {
for(int i=0;i<words.size();i++){
if(searchWord==words[i]||searchWord.size()!=words[i].size()) continue;
if(searchWord.size()==words[i].size()) {
int cnt=0;
for(int j=0;j<words[i].size();j++){
if(searchWord[j]!=words[i][j]) cnt++;
if(cnt==2) break;
}
if(cnt==1) return true;
}
}
return false;
}
struct Node{
bool flag;
Node* son[26];
Node(){
flag=false;
for(int i=0;i<26;i++) son[i]=NULL;
}
}*root;
string replaceWords(vector<string>& dictionary, string sentence) {
root=new Node();
string res;
for(int i=0;i<dictionary.size();i++) insert(dictionary[i]);
vector<string> word;
for(int i=0,j=0;i<sentence.size();i++){
while(j<sentence.size()&&sentence[j]!=' ') j++;
string w=sentence.substr(i,j-i);
word.push_back(w);
i=j;j++;
}
for(int i=0;i<word.size();i++){
res+=search(word[i]);
if(i!=word.size()-1) res+=' ';
}
return res;
}
void insert(string str){
auto p=root;
for(int i=0;i<str.size();i++){
int u=str[i]-'a';
if(!p->son[u]) p->son[u]=new Node();
p=p->son[u];
if(i==str.size()-1) p->flag=true;
}
}
string search(string str){
string xx;
auto p=root;
for(int i=0;i<str.size();i++){
int u=str[i]-'a';
xx+=str[i];
if(!p->son[u]) break;
p=p->son[u];
if(p->flag) {
return xx;
}
}
// cout<<str<<" "<<xx<<endl;
return str;
}
static bool cmp(const string&a ,const string& b){
return a.size()>b.size();
}
int minimumLengthEncoding(vector<string>& words) {
sort(words.begin(),words.end(),cmp);
int res=0;
int cnt=0;
vector<bool> st(words.size(),0);
for(int i=0;i<words.size();i++){
for(int j=i-1;j>=0;j--){
if(!st[j]&&words[j].size()>=words[i].size()){
if(words[j].find(words[i],words[j].size()-words[i].size())!=-1)
st[i]=true,cnt++;
}
}
if(!st[i]) res+=words[i].size()+1;;
}
return res;
}
struct Node{
bool flag;
int val;
Node* son[26];
Node(){
val=0;
flag=false;
for(int i=0;i<26;i++) son[i]=NULL;
}
}*root;
/** Initialize your data structure here. */
void insert_m(string str,int val){
auto p=root;
for(int i=0;i<str.size();i++){
int u=str[i]-'a';
if(!p->son[u]) p->son[u]=new Node();
p=p->son[u];
if(i==str.size()-1) p->flag=true,p->val=val;
}
}
MapSum() {
root=new Node();
}
void insert(string key, int val) {
insert_m(key,val);
}
int sum(string str) {
auto p=root;
int sumN=0;
for(int i=0;i<str.size();i++){
int u=str[i]-'a';
if(!p->son[u]) return 0;
p=p->son[u];
if(i==str.size()-1&&p->flag) sumN+=p->val;
}
int xx=0;
for(int i=0;i<26;i++){
if(p->son[i]){
xx=0;
dfs(p->son[i],xx);
sumN+=xx;
}
}
return sumN;
}
void dfs(Node* root,int& xx){
auto p=root;
if(p->flag) xx+=p->val;
for(int i=0;i<26;i++){
if(p->son[i]) dfs(p->son[i],xx);
}
}
struct Node{
Node *son[2];
Node(){
for(int i=0;i<2;i++) son[i]=NULL;
}
}*root;
void insert(int x){
auto p=root;
for(int i=30;i>=0;i--){
int u=x>>i&1;
if(!p->son[u]) p->son[u]=new Node();
p=p->son[u];
}
}
int search(int x){
auto p=root;
int num=0;
for(int i=30;i>=0;i--){
int t=(x>>i)&1;
if( (t==1&&p->son[!t]) || (t==0&&p->son[!t]) ){
int aa=(1<<2);
num+=(1<<i);
p=p->son[!t];
}else {
p=p->son[t];
}
}
return num;
}
int findMaximumXOR(vector<int>& nums) {
int aa=2;
aa+=(1<<2);
root=new Node();
for(int i=0;i<nums.size();i++) insert(nums[i]);
int res=0;
for(int i=0;i<nums.size();i++){
res=max(res,search(nums[i]));
}
return res;
}
int searchInsert(vector<int>& nums, int target) {
if(target>nums[nums.size()-1]) return nums.size();
if(target<nums[0]) return 0;
int l=0,r=nums.size()-1;
while(l<r){
int mid=(l+r)/2;
if(nums[mid]>=target) r=mid;
else l=mid+1;
}
return l;
}
int peakIndexInMountainArray(vector<int>& nums) {
int l=0,r=nums.size()-1;
while(l<r){
int mid=(l+r)/2;
if(nums[mid]>nums[mid+1]) r=mid;
else l=mid+1;
}
return r;
}
剑指 Offer II 070. 排序数组中只出现一次的数字
int singleNonDuplicate(vector<int>& nums) {
nums.push_back(nums.back()+1);
int l=0,r=nums.size()/2-1;
while(l<r){
int mid=(l+r)/2;
if(nums[mid*2]!=nums[mid*2+1]) r=mid;
else l=mid+1;
}
return nums[r*2];
}
vector<int> sum;
vector<int> nums;
Solution(vector<int>& w) {
sum.push_back(w[0]);nums.push_back(w[0]);
for(int i=1;i<w.size();i++)
nums.push_back(w[i]), sum.push_back(sum.back()+w[i]);
}
int pickIndex() {
if(nums.size()==1) return 0;
int xx=rand()%sum.back();
int l=0,r=sum.size()-1;
while(l<r){
int mid=(l+r)/2;
if(sum[mid]>xx) r=mid;
else l=mid+1;
}
//cout<<xx<<" "<<sum[r]<<endl;
return r;
}
int mySqrt(int x) {
if(x==1) return 1;
long l=1,r=x;
while(l<r){
long mid=(l+r+1)/2;
if(mid<=x/mid) l=mid+1;
else r=mid-1;
}
if(l*l>x) return l-1;
return l;
}
int minEatingSpeed(vector<int>& piles, int h) {
int l=0,r=1e9;
int res=INT_MAX;
while(l<r){
int mid=(l+r+1)/2;
bool flag=false;
int cnt=0;
for(int j=0;j<piles.size();j++){
if(piles[j]%mid!=0) cnt+=piles[j]/mid+1;
else cnt+=piles[j]/mid;
if(cnt>h){
flag=true;
break;
}
}
if(flag) l=mid;
else res=min(res,mid), r=mid-1;
}
return res;
}
vector<vector<int>> merge(vector<vector<int>>& intervals) {
vector<vector<int>> res;
sort(intervals.begin(),intervals.end());
res.push_back(intervals[0]);
for(int i=1;i<intervals.size();i++){
if(res.back()[1]>=intervals[i][0]&&res.back()[1]<intervals[i][1]) res.back()[1]=intervals[i][1];
else if(res.back()[1]<intervals[i][0]) res.push_back(intervals[i]);
}
return res;
}
vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
sort(arr1.begin(),arr1.end());
unordered_map<int,int> map;
vector<int> res;
for(int i=0;i<arr2.size();i++){
map[arr2[i]]++;
int l=0,r=arr1.size()-1;
while(l<r){
int mid=(l+r)/2;
if(arr1[mid]>=arr2[i]) r=mid;
else l=mid+1;
}
while(arr1[l]==arr2[i]) res.push_back(arr1[l]),l++;
}
for(int i=0;i<arr1.size();i++){
if(map[arr1[i]]==0) res.push_back(arr1[i]);
}
return res;
}
int findKthLargest(vector<int>& nums, int k) {
sort(nums.begin(),nums.end());
return nums[nums.size()-k];
}
ListNode* sortList(ListNode* head) {
if(head==NULL) return NULL;
vector<int> res;
while(head) res.push_back(head->val),head=head->next;
sort(res.begin(),res.end());
ListNode* lires=new ListNode();
ListNode* li=new ListNode();
lires=li;
li->val=res[0];
for(int i=1;i<res.size();i++){
ListNode* xx=new ListNode();
xx->val=res[i];
li->next=xx;
li=xx;
}
return lires;
}
ListNode* mergeKLists(vector<ListNode*>& lists) {
if(lists.empty()) return NULL;
vector<int> p;
for(int i=0;i<lists.size();i++){
vector<int> u= sortList(lists[i]);
if(u.empty()) continue;
p.insert(p.end(),u.begin(),u.end());
}
sort(p.begin(),p.end());
if(p.empty()) return NULL;
ListNode* li=new ListNode(p[0]);
ListNode* head=new ListNode();
head=li;
for(int i=1;i<p.size();i++){
ListNode* xx=new ListNode(p[i]);
li->next=xx;
li=xx;
}
return head;
}
vector<int> sortList(ListNode* head) {
vector<int> res;
if(head==NULL) return res;
while(head) res.push_back(head->val),head=head->next;
return res;
}
vector<vector<int>>res;
vector<vector<int>> subsets(vector<int>& nums) {
res.push_back({});
vector<int> xx;
dfs(0,xx,nums);
return res;
}
void dfs(int idx,vector<int> xx,vector<int> nums){
if(idx==nums.size()) return ;
for(int i=idx;i<nums.size();i++){
xx.push_back(nums[i]);
res.push_back(xx);
dfs(i+1,xx,nums);
xx.pop_back();
}
}
vector<vector<int>> res;
vector<vector<int>> combine(int n, int k) {
vector<int> xx;
dfs(n,k,1,xx);
return res;
}
void dfs(int n,int k,int idx,vector<int> xx){
if(xx.size()==k){
res.push_back(xx);
return ;
}
for(int i=idx;i<=n;i++){
xx.push_back(i);
dfs(n,k,i+1,xx);
xx.pop_back();
}
}