leetcode/907-231127/main.cpp
2023-11-27 15:36:30 +01:00

151 lines
15 KiB
C++

#include<bits/stdc++.h>
using namespace std;
class Solution{
public:
struct node
{
node *pre;
node *nxt;
int val;
};
class li{
public:
node header;
node end;
li(){
header.nxt = &end;
end.pre = &header;
header.val = 0;
end.val = 0x3f3f3f;
}
void add(node *x, li l){
node * rlt = find(x, l);
x->nxt = rlt->nxt;
rlt->nxt->pre = x;
rlt->nxt = x;
x->pre = rlt;
}
void add(int x, li l){
node * c = new node();
c->val = x;
l.add(c,l);
}
node* find(node *x, li l){
node * cur = &l.header;
while(cur->val< x->val){
cur = cur->nxt;
}
return cur->pre;
}
void del(int x){
node * cur = this->header.nxt;
while(cur->val!=x){
cur = cur->nxt;
}
if(cur->val != this->end.val){
cur->nxt->pre = cur->pre;
cur->pre->nxt = cur->nxt;
free(cur);
}
}
};
// int sumSubarrayMins(vector<int>& arr){
// long long sum = 0;
// int mod = 1e9+7;
// for(int i = 1 ; i <= arr.size() ; i++){
// li work_list = li();
// for(int j = 0 ; j < i ; j++){
// work_list.add(arr[j],work_list);
// }
// sum+=work_list.header.nxt->val;
// for(int j = i ; j < arr.size(); j++){
// work_list.del(arr[j-i]);
// work_list.add(arr[j],work_list);
// sum+=work_list.header.nxt->val;
// sum%=mod;
// }
// }
// return sum;
// }
int sumSubarrayMins(vector<int>& arr){
// use monotonical stack
long long sum = 0;
const int length = arr.size();
vector<int> left_idx(length, -1);
vector<int> right_idx(length,length);
stack< pair<int,int> > sta;
for(int i = 0 ; i < length ;i++){
int cnt = 0;
while(cnt <= i ){
while(sta.empty() == false && sta.top().second> arr[cnt]) sta.pop();
sta.push(make_pair(cnt,arr[cnt]));
cnt++;
}
sta.pop();
if(!sta.empty())
left_idx[i] = sta.top().first;
}
sta = stack< pair<int,int> >();
for(int i = 0 ; i < length;i++){
int cnt = i;
bool flag = 0;
while(cnt < length){
while(sta.empty() == false && sta.top().second > arr[cnt]) {
if(sta.top().first != i)
sta.pop();
else{
right_idx[i] = cnt;
flag = 1;
break;
}
}
if(flag) break;
sta.push(make_pair(cnt,arr[cnt]));
cnt++;
}
}
const int mod = 1e9 + 7 ;
for(int i = 0 ; i < length ;i++){
cout<<left_idx[i]<<' '<<right_idx[i]<<endl;
sum += ((i - left_idx[i]) * (right_idx[i] - i) ) * arr[i] % mod;
sum %= mod;
}
return sum;
}
};
int main(){
Solution sol;
vector<int> vec1 = {3,1,2,4};
cout<<sol.sumSubarrayMins(vec1)<<endl;
vector<int> vec2 = {11,81,94,43,3};
cout<<sol.sumSubarrayMins(vec2)<<endl;
// auto start = chrono::high_resolution_clock::now();
// vector<int> vec3 = {1,26734,27943,524,4612,26421,17122,2570,15674,24112,7796,12767,22132,11774,127,8546,16572,20639,12467,9527,21142,29257,26006,14806,3832,19818,6304,6811,18841,6696,21366,15128,12996,24112,13873,21626,18587,22330,26768,6413,20338,134,6480,22239,9226,6521,16723,26628,16225,6394,8797,3890,9928,1510,19621,15927,3333,1407,4406,25824,28889,9153,13234,5720,21808,15276,370,6999,241,13129,26136,26421,18083,28788,27343,15316,11801,9463,5007,16524,14216,303,1016,6518,7794,13498,27069,27709,8094,20357,16684,27007,9076,13325,17847,12585,12545,6018,22253,24412,19540,16884,26412,25804,595,20374,619,12213,6028,6150,13070,1374,21737,14743,26182,8197,29022,28890,11486,4630,3753,2065,1266,26052,10924,14024,13081,24058,12033,10712,20360,11513,19732,13744,17048,12078,7601,28306,17662,19083,8598,19081,16580,17442,16174,7725,19634,2423,9330,9109,13032,83,6140,25620,23759,399,18202,15453,2923,22252,10867,11467,10976,18250,25012,25274,9378,26928,1760,18689,21015,28673,12517,12439,18831,10206,29060,10435,4407,10186,7638,29956,26649,22021,8227,8451,4557,17543,6623,3137,27029,8073,18781,3882,8540,4153,8282,283,10854,28166,18778,28476,13539,1786,4607,14617,6838,2750,7880,14047,12619,22485,28217,16297,2729,3523,12101,2577,16300,21286,8692,15655,29373,4995,27883,12444,26411,11217,27545,26438,24921,9880,7779,2952,23384,12011,13948,2861,2080,25248,16760,4958,5683,10805,7975,4095,228,28656,13329,19839,16967,21051,26723,9871,19712,446,8902,785,13306,25710,26796,20127,25379,14185,2470,13950,21548,6757,3837,29541,13392,19450,29769,20946,12424,16208,14892,13533,17031,9274,3443,28888,27114,20631,13237,26711,13961,19286,27760,5348,22970,28016,12336,16832,9929,27933,10067,7299,11762,2905,4781,2907,21345,9963,1122,20727,8969,24317,27537,17559,27963,27693,11783,21382,597,18981,29057,17087,8911,16192,11459,8862,13471,6762,14218,7155,542,667,12711,8956,1177,24497,2943,9246,24671,22054,12855,23007,7784,12656,24346,9349,24116,13108,21107,5379,13641,4024,16904,3876,28277,27679,8311,9504,129,29864,17304,9540,29879,7995,19549,12824,3605,7710,21160,6952,9415,5398,1122,1573,5864,2842,20614,26538,14855,9197,15134,21477,18952,4796,23187,26967,15075,20832,17450,4983,11540,28922,1610,22931,18562,17063,14785,4502,18292,28325,15236,25468,20536,5171,24635,5080,28281,4809,12344,24793,10820,18493,19894,8710,12153,17553,15020,28136,21718,2574,20550,17769,15794,9741,23319,27271,9729,14512,21493,22829,8479,17237,26257,2140,24820,21046,24817,26657,22319,19981,2843,17138,1021,13102,2534,18248,980,22097,19994,2527,3932,23203,22344,27247,10538,6211,28642,14194,16995,2726,24200,6693,28511,13888,4740,18100,7500,3396,14583,15123,21341,27012,6520,24756,13607,3780,22530,11582,16806,97,7420,17375,929,3947,15740,6615,21130,26780,4939,11151,2149,28188,21372,4316,6977,18419,8974,4727,25056,8278,21028,11911,7157,25944,21102,12394,18811,4104,29252,22555,24702,6187,25190,9541,10214,20026,26214,20475,8709,24737,4049,28281,21172,16847,6271,13823,12712,15869,22958,24708,14471,18466,24381,21432,17951,12072,1861,24954,6074,17079,6331,114,12098,16224,7819,1519,23126,14510,1629,4123,21076,29558,28140,28849,13690,19769,2512,17645,22132,26930,2479,12416,8147,3462,7032,24018,16654,16425,6922,24768,14537,25037,13152,53,6801,18268,5006,14536,17943,11720,857,1860,24287,14852,2857,6151,9639,1657,18210,15024,23337,14297,16336,24118,24812,9792,2931,26927,23127,589,1801,25909,12099,164,16978,29889,29502,7328,22146,5565,24589,18452,24505,20623,24941,4125,8109,5605,3712,6662,2502,13479,10357,267,5866,21070,1303,558,25697,27452,1109,7796,10753,17430,18818,28933,8112,12049,11286,25158,5795,27279,19863,14677,21586,5902,1322,25964,10701,26204,18703,17464,26479,9116,11497,25344,27224,23574,27569,16514,5199,25448,26500,9126,20005,565,27784,10415,16918,20524,24704,21210,9811,27920,6688,22408,15214,7604,15383,9448,15626,6257,7933,7339,25848,11747,25708,11363,9614,19924,12137,24562,19416,20995,29095,25348,10250,13783,25981,26784,16167,16371,2499,6383,3550,9760,478,15232,18,10791,3064,7295,19924,7935,174,24768,16171,26478,8057,9595,926,15229,26843,54,17326,7211,20606,27764,14531,26303,11572,6531,12681,19759,24071,20310,2156,23865,3976,3452,6371,5227,5795,6016,16059,25810,15763,19722,23469,29704,8229,28262,5867,5784,23877,2369,27037,27094,21064,20899,14387,2123,10375,22822,17631,9176,5166,6320,16026,21001,28329,23419,21184,18985,29941,18285,23450,12063,4181,11016,23771,15244,14867,6242,10874,22482,12516,5270,28765,27993,25818,14679,29602,2140,19648,964,10335,16796,14490,13093,28122,26502,26719,23403,6047,19838,11099,26666,10332,17904,24809,9866,3640,10706,8112,10532,29303,25785,17537,25197,15164,27938,7598,27027,16923,3328,14196,9432,6643,23609,5050,18760,16607,16440,5614,21039,14198,16742,27851,29810,18292,15071,29333,994,4933,21146,9431,14933,18688,12098,17732,9535,4794,19142,4892,5064,25497,23264,2997,22093,24692,24767,18692,16140,18291,9612,17404,23691,13337,3256,25168,8774,13258,19752,24109,19197,11583,3190,29030,14925,13253,22870,22237,21134,29393,24969,18838,3846,24903,15472,17549,16708,6666,16702,14095,26914,28233,29491,15880,8739,2833,639,8295,1187,21528,14341,24595,25013,14181,14455,15906,19653,14279,28796,8268,1144,29159,2630,5642,8217,25230,9486,19678,25389,16300,6843,15402,687,10985,5406,12066,1804,20103,20680,24203,17878,3700,4368,29528,20875,19874,14623,9713,25504,28192,7492,10315,9551,29553,17279,22274,10006,9801,22347,24460,4289,26264,19552,9117,5864,28404,15654,13176,9174,19810,268,27341,18487,19,1851,18000,7218,9944,26207,23598,15463,26305,17452,8408,29282,1780,2070,26883,16527,26288,23803,21979,28682,10338,18774,27184,7380,183,16370,12834,11925,11541,20281,15800,13256,20365,10770,14711,10041};
// cout<<sol.sumSubarrayMins(vec3)<<endl;
// auto end = chrono::high_resolution_clock::now();
// chrono::duration<double> diff = end - start;
// cout<<diff.count()<<endl;
// start = chrono::high_resolution_clock::now();
// vector<int> vec4 = {29959,29867,29822,29704,29676,29650,29577,29488,29286,29255,29232,29207,29071,29034,28925,28849,28731,28693,28624,28606,28591,28397,28357,28308,28295,28210,28119,28090,28004,27903,27845,27830,27777,27736,27640,27540,27506,27428,27341,27308,27182,27152,27122,27029,26928,26872,26796,26765,26663,26597,26580,26530,26498,26475,26436,26406,26382,26312,26213,26134,26088,26025,25943,25912,25875,25845,25810,25702,25638,25614,25531,25524,25488,25470,25444,25402,25283,25262,25121,24988,24958,24886,24769,24697,24635,24595,24490,24456,24453,24346,24313,24248,24200,24148,24107,24052,24044,24021,23970,23908,23897,23835,23752,23741,23714,23661,23596,23545,23509,23470,23439,23409,23350,23215,23166,23155,23100,23024,22923,22825,22793,22627,22613,22536,22450,22383,22312,22268,22205,22175,22136,22028,21971,21900,21824,21769,21726,21583,21546,21513,21494,21428,21327,21264,21254,21174,21140,21112,21000,20921,20902,20830,20817,20783,20735,20657,20616,20573,20485,20378,20363,20305,20259,20210,20114,20002,19846,19785,19747,19667,19645,19622,19610,19580,19542,19516,19454,19392,19310,19277,19194,19131,19090,19004,18883,18845,18791,18781,18668,18591,18518,18475,18368,18331,18310,18287,18217,18114,18092,18048,17990,17964,17912,17836,17740,17704,17630,17613,17573,17428,17356,17341,17300,17260,17180,17174,17126,17071,17041,16866,16850,16828,16672,16618,16577,16499,16407,16357,16318,16293,16202,16150,16075,16041,15948,15921,15844,15843,15785,15764,15668,15626,15579,15473,15387,15255,15190,15139,15062,14996,14954,14918,14907,14902,14867,14851,14817,14799,14751,14720,14536,14506,14474,14353,14303,14280,14185,14107,14012,13932,13858,13781,13585,13563,13533,13451,13412,13362,13249,13208,13181,13064,13037,12961,12926,12892,12786,12731,12611,12573,12506,12502,12496,12470,12443,12370,12262,12182,12153,12069,12000,11847,11806,11781,11708,11687,11593,11550,11445,11372,11329,11308,11291,11268,11241,11191,11027,10982,10879,10862,10776,10695,10603,10502,10464,10350,10338,10305,10273,10176,10124,10094,10038,9953,9935,9812,9786,9743,9728,9508,9472,9383,9349,9236,9215,9130,9124,9042,9008,8988,8901,8833,8809,8780,8716,8580,8462,8334,8321,8305,8280,8257,8246,8137,8077,8043,8016,7984,7955,7927,7906,7746,7663,7653,7572,7542,7530,7489,7420,7390,7361,7337,7245,7210,7188,7175,7096,6898,6846,6745,6675,6569,6478,6427,6363,6284,6260,6243,6206,6154,6135,6078,6061,6017,5995,5917,5863,5836,5793,5763,5743,5678,5572,5532,5459,5384,5341,5299,5251,5231,4995,4933,4861,4740,4672,4625,4496,4445,4361,4282,4215,4135,4097,4028,3917,3862,3711,3553,3498,3410,3388,3384,3288,3279,3244,3221,3181,3171,3150,3060,3035,2975,2965,2834,2760,2637,2584,2533,2440,2383,2311,2285,2255,2211,2192,2121,2054,2010,1964,1850,1724,1642,1577,1411,1409,1332,1296,1265,1256,1220,1195,937,903,880,811,739,720,650,609,547,533,459,434,384,279,231,163,102,78,30,5,52,100,155,217,277,328,389,446,473,546,583,649,702,734,768,857,882,912,1043,1219,1243,1258,1290,1325,1359,1409,1567,1642,1679,1726,1873,1965,2017,2088,2172,2204,2226,2273,2288,2316,2434,2522,2558,2622,2678,2790,2933,2965,3025,3037,3071,3167,3180,3194,3233,3269,3282,3383,3387,3401,3465,3528,3595,3801,3910,4020,4078,4128,4213,4271,4295,4420,4472,4612,4663,4739,4845,4891,4980,5109,5241,5284,5335,5379,5388,5478,5546,5639,5705,5751,5766,5803,5855,5879,5975,6000,6024,6070,6093,6137,6156,6212,6256,6276,6304,6421,6441,6537,6614,6743,6844,6893,7087,7169,7183,7200,7237,7262,7352,7376,7398,7441,7491,7541,7564,7602,7656,7707,7814,7924,7940,7958,8014,8036,8048,8132,8141,8250,8279,8288,8321,8331,8374,8515,8655,8723,8807,8825,8878,8953,8990,9011,9077,9128,9172,9219,9276,9383,9420,9499,9535,9736,9744,9801,9900,9951,10038,10093,10119,10147,10265,10301,10314,10340,10456,10499,10564,10622,10767,10802,10876,10882,10997,11063,11217,11243,11276,11299,11314,11365,11407,11456,11587,11627,11705,11751,11792,11831,11901,12012,12118,12180,12240,12296,12385,12469,12473,12497,12503,12537,12578,12723,12778,12858,12901,12936,13020,13048,13136,13195,13232,13325,13377,13424,13493,13547,13564,13724,13856,13911,13938,14075,14151,14234,14300,14353,14395,14499,14507,14705,14724,14796,14802,14823,14858,14882,14905,14914,14936,14962,15049,15114,15161,15237,15272,15399,15565,15587,15666,15749,15778,15830,15843,15864,15928,16039,16075,16141,16163,16246,16315,16333,16389,16415,16526,16601,16650,16798,16845,16861,16991,17046,17090,17140,17178,17186,17292,17305,17343,17419,17456,17610,17617,17693,17728,17783,17909,17918,17970,18032,18083,18104,18114,18223,18296,18330,18363,18428,18496,18578,18660,18733,18782,18792,18861,18929,19069,19127,19184,19269,19279,19355,19394,19494,19539,19559,19599,19612,19643,19666,19745,19760,19815,19864,20012,20141,20231,20270,20330,20370,20380,20500,20595,20617,20690,20751,20811,20824,20843,20910,20925,21044,21126,21165,21198,21260,21280,21343,21467,21505,21531,21564,21640,21755,21817,21885,21929,22010,22103,22159,22196,22229,22270,22368,22414,22515,22570,22615,22630,22806,22864,22951,23030,23107,23155,23191,23226,23399,23438,23464,23487,23524,23559,23634,23667,23719,23747,23764,23869,23901,23936,24012,24022,24045,24074,24141,24185,24204,24272,24327,24452,24455,24490,24560,24615,24641,24734,24815,24890,24963,25025,25242,25282,25283,25414,25446,25475,25489,25527,25586,25636,25640,25771,25844,25848,25883,25923,26005,26048,26106,26157,26312,26359,26395,26429,26465,26491,26513,26558,26584,26601,26667,26770,26864,26900,26996,27118,27129,27176,27272,27313,27389,27478,27517,27580,27700,27761,27811,27844,27848,27967,28051,28108,28176,28264,28302,28332,28380,28525,28591,28617,28681,28727,28744,28874,28994,29047,29123,29221,29239,29274,29347,29493,29596,29668,29694,29717,29847,29871};
// cout<<sol.sumSubarrayMins(vec4)<<endl;
// end = chrono::high_resolution_clock::now();
// diff = end - start;
// cout<<diff.count()<<endl;
return 0;
}