class SummaryRanges {
public:/** Initialize your data structure here. */
map<int,int>m;SummaryRanges(){}voidaddNum(int val){if(m.count(val))return;
m[val]= val;int min = val, max = val;if(m.count(val -1)) min = m[val -1];//更新左端点if(m.count(val +1)) max = m[val +1];
m[max]= min;
m[min]= max;}
vector<vector<int>>getIntervals(){
vector<vector<int>> res;int min_v =-1;for(auto t : m)if(t.first > min_v){
res.push_back({t.first, t.second});
min_v = t.second;}return res;}};/**
* Your SummaryRanges object will be instantiated and called as such:
* SummaryRanges* obj = new SummaryRanges();
* obj->addNum(val);
* vector<vector<int>> param_2 = obj->getIntervals();
*/
class Solution {
public:intmaxEnvelopes(vector<vector<int>>& envelopes){sort(envelopes.begin(), envelopes.end());int n = envelopes.size();
vector<int>f(n);int res =0;for(int i =0; i < n; i ++){
f[i]=1;for(int j =0; j < i; j ++)if(envelopes[i][0]> envelopes[j][0]&& envelopes[i][1]> envelopes[j][1])
f[i]=max(f[i], f[j]+1);
res =max(res, f[i]);}return res;}};
class Twitter {
public:/** Initialize your data structure here. */
unordered_map<int, vector<pair<int,int>>>posts;
unordered_map<int, unordered_set<int>> follows;int id =0;Twitter(){}/** Compose a new tweet. */voidpostTweet(int userId,int tweetId){
posts[userId].push_back(make_pair(id ++, tweetId));}/** Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. */
vector<int>getNewsFeed(int userId){
vector<pair<int,int>>ps;for(auto x : posts[userId]) ps.push_back(x);for(auto follow : follows[userId])for(auto x : posts[follow])
ps.push_back(x);sort(ps.rbegin(), ps.rend());
vector<int>res;for(int i =0; i <10&& i < ps.size(); i ++)
res.push_back(ps[i].second);return res;}/** Follower follows a followee. If the operation is invalid, it should be a no-op. */voidfollow(int followerId,int followeeId){if(followerId != followeeId)
follows[followerId].insert(followeeId);}/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */voidunfollow(int followerId,int followeeId){
follows[followerId].erase(followeeId);}};/**
* Your Twitter object will be instantiated and called as such:
* Twitter* obj = new Twitter();
* obj->postTweet(userId,tweetId);
* vector<int> param_2 = obj->getNewsFeed(userId);
* obj->follow(followerId,followeeId);
* obj->unfollow(followerId,followeeId);
*/
class Solution {
public:intcountNumbersWithUniqueDigits(int n){if(n ==0)return1;
n =min(n,10);
vector<int>f(n);
f[0]=9;for(int i =1; i < n; i ++) f[i]= f[i -1]*(10- i);int res =0;for(int i =0; i < n; i ++) res += f[i];return res +1;}};
class Solution {
public:intmaxSumSubmatrix(vector<vector<int>>& matrix,int k){int n = matrix.size(), m = matrix[0].size();int res = INT_MIN;int s[n +1][m +1];memset(s,0,(n +1)*(m +1)*sizeof(int));//大小(内存)for(int i =0; i < n; i ++)for(int j =0; j < m; j ++)
s[i][j +1]= s[i][j]+ matrix[i][j];//枚举矩形左右边界for(int i =0; i < m; i ++)for(int j = i; j < m; j ++){
set<int>myset;int A[n], temp =0;
myset.insert(0);for(int t =0; t < n; t ++){
A[t]= s[t][j +1]- s[t][i];
temp += A[t];//当前前缀和auto it = myset.lower_bound(temp - k);if(it != myset.end())
res =max(res, temp -*it);
myset.insert(temp);}}return res;}};
class Solution {
public:
bool canMeasureWater(int x,int y,int z){if(!x &&!y)return z ==0;return z =0||(z %gcd(x, y)==0&& x + y >= z);}staticintgcd(int x,int y){if(y ==0)return x;int r = x % y;returngcd(y, r);}};
class Solution {
public:
bool isPerfectSquare(int num){for(longlong i =0; i * i <= num; i++)if(i * i == num)return true;return false;}};
class Solution {
public:
bool isPerfectSquare(int num){int res = num;int next =1;while(res >0){
res -= next;
next +=2;}return res ==0;}};
class Solution {
public:
vector<int>largestDivisibleSubset(vector<int>& nums){int n = nums.size(), max =0, end =-1;
vector<int>dp(n,1),last(n,-1), res;sort(nums.begin(), nums.end());for(int i =0; i < n; i ++){for(int j =0; j < i; j ++)if(nums[i]% nums[j]==0&& dp[i]<= dp[j]){
dp[i]= dp[j]+1;
last[i]= j;}if(dp[i]> max){
max = dp[i];
end = i;}}for(int i = end; i !=-1; i = last[i])
res.push_back(nums[i]);return res;}};