省选校内模拟D2

tot:60+10+30=100

T1

Solution 

Code


60pts:分块打表

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define ll long long
using namespace std;
int n,d[10000000];
ll ans[2001]={0,215084,496623,806589,1135731,1479458,1835386,2201339,2575690,2958128,3347193,3742589,4144083,4550506,
4962305,5377977,5798374,6222520,6651659,7082376,7518850,7958237,8399570,8844120,9292567,9743543,10196450,10652805,11111412,
11571295,12035120,12500087,12968163,13438524,13909588,14384074,14860691,15337970,15816693,16299663,16782730,17267659,17755601,
18241817,18733404,19225035,19717933,20211985,20709416,21207812,21706883,22206699,22708860,23212921,23717839,24222767,24731075,25238504,
25748574,26260230,26772576,27286129,27799924,28317323,28833098,29351177,29871228,30390258,30911583,31434763,31958347,32482777,33009353,33534200,34062605,34591797,
35119653,35651004,36183160,36714244,37248040,37783489,38317265,38853761,39391868,39927102,40465590,41005902,41547013,42087349,42630295,43172527,43715924,44260016,
44804945,45350172,45898743,46444980,46995564,47544031,48094156,48643567,49195105,49746902,50299904,50853872,51408413,51961446,52519509,53075037,53631522,54189097,
54748144,55306531,55865698,56425958,56986949,57549431,58111568,58673481,59237859,59802477,60366936,60931972,61497124,62065381,62633164,63197437,63767621,64335665,
64906073,65475551,66048006,66617376,67190070,67763274,68334960,68908480,69483727,70058032,70633486,71207954,71785022,72361109,72940583,73515884,74094705,74675928,
75254019,75833898,76415403,76994191,77576146,78159034,78741526,79323079,79908710,80493053,81075023,81662522,82246325,82833054,83420919,84005178,84592863,85181394,
85769079,86357197,86948926,87537244,88125382,88717453,89307401,89900306,90491564,91083230,91677185,92269812,92862498,93457305,94053258,94646568,95242293,95838298,
96433873,97030375,97627234,98225794,98822416,99423149,100018307,100620071,101219018,101818601,102417320,103019751,103622424,104223870,104825370,105426648,106030594,
106632124,107235613,107838730,108443731,109047994,109653019,110259647,110864273,111469982,112077929,112684553,113291969,113898843,114505773,115113021,115726032,116332437,
116943435,117551659,118162873,118772356,119383960,119993689,120607618,121220021,121831070,122441477,123056339,123668315,124283039,124897880,125510679,126124950,126742335,
127354644,127972434,128587611,129203640,129819295,130438033,131053510,131674063,132292141,132909646,133528084,134146790,134765489,135384710,136006091,136626488,137246432,
137869344,138487500,139107390,139729749,140353128,140973363,141597252,142222200,142844128,143468041,144091717,144714214,145341982,145965679,146589865,147215846,147841895,
148465952,149095220,149720321,150346832,150975797,151601460,152230776,152858493,153484461,154114878,154743237,155372931,156001294,156631273,157260763,157891468,158521786,
159152908,159784453,160417594,161048789,161678498,162310163,162943199,163575848,164212373,164844800,165476963,166111533,166745955,167380368,168015855,168648483,169283346,
169919340,170555721,171192682,171828646,172463437,173101912,173738494,174374305,175012456,175652083,176291465,176929115,177566633,178202939,178842986,179485106,180123533,
180762332,181401384,182043975,182682543,183327816,183964995,184606290,185249223,185890257,186530749,187176388,187815775,188459599,189104158,189745960,190387834,191036152,191677837,
192323141,192967340,193609586,194256350,194902280,195547973,196192061,196838066,197484999,198127881,198777771,199423953,200070225,200720667,201366735,202012950,202661919,203309953,
203956771,204608545,205255093,205904335,206556169,207204928,207853921,208506103,209153858,209805080,210455573,211105535,211756541,212409176,213059351,213713135,214362779,215014869,
215669211,216321519,216972945,217629840,218282442,218931351,219585078,220239012,220896730,221548525,222204487,222859249,223513321,224168401,224822260,225479917,226136068,226790501,
227449976,228103076,228762794,229416470,230076575,230731670,231389633,232047254,232707044,233364042,234021810,234681627,235338027,235998555,236655759,237316008,237975270,238634427,
239294662,239953714,240614749,241276993,241937617,242597338,243257992,243920455,244580131,245241883,245905706,246564635,247228745,247890656,248554205,249214898,249879062,250543604,
251204813,251868428,252530931,253199256,253864080,254525700,255189876,255852915,256521225,257183250,257849661,258514833,259182289,259849201,260512252,261177826,261845839,262513066,
263177590,263846266,264514735,265179475,265850060,266514680,267183755,267850271,268517156,269187362,269857244,270524375,271192502,271863200,272532975,273201399,273872994,274539528,
275210088,275880318,276550947,277223535,277895172,278564217,279233199,279907774,280577878,281247943,281919613,282591334,283264072,283938631,284607505,285278893,285954904,286627750,
287299139,287975894,288649556,289319858,289997849,290666612,291343574,292017488,292692623,293362979,294041891,294715374,295391010,296067078,296741493,297417639,298094148,298769724,
299446380,300121227,300799380,301474428,302155561,302827726,303505255,304184806,304858564,305537659,306214336,306892867,307571956,308251714,308925808,309606851,310285307,310961759,
311645114,312323423,313001594,313683806,314362073,315039449,315721421,316400927,317079390,317761089,318444336,319124139,319804530,320485305,321167370,321849006,322529469,323211990,
323894160,324575892,325255513,325938904,326624749,327302449,327987055,328670401,329353615,330036427,330722224,331400665,332088238,332770973,333456734,334136363,334824974,335507804,
336190757,336876818,337563017,338247773,338932316,339616445,340303808,340991637,341673960,342357726,343046961,343733013,344418966,345108336,345791823,346478997,347165559,347852736,
348540549,349229422,349915447,350603506,351292951,351980143,352668985,353355691,354042424,354733174,355418776,356110939,356797177,357488837,358175504,358865768,359559359,360245381,
360937601,361628099,362314886,363005726,363697946,364385321,365079401,365770919,366461286,367151226,367844322,368532843,369224499,369917007,370607040,371300919,371991786,372683358,
373377888,374068770,374763733,375452281,376148281,376838584,377532304,378226834,378921472,379613449,380307940,380998624,381694078,382392421,383083384,383778728,384473567,385169807,
385865021,386558024,387254294,387949691,388643783,389337392,390030581,390729884,391424051,392119217,392819274,393513699,394210452,394906317,395602344,396298407,396994722,397693989,
398391690,399085455,399783768,400480014,401184954,401879179,402575380,403274746,403971196,404669008,405370051,406069075,406765684,407464651,408161878,408864346,409564810,410262457,
410959283,411661151,412359056,413058329,413761553,414460625,415159868,415859513,416558468,417261740,417965450,418661714,419364317,420067443,420767760,421466598,422170578,422871009,
423570699,424273656,424977069,425675796,426382551,427080108,427787106,428486778,429190236,429892360,430596754,431297359,432002764,432708022,433404643,434113312,434816635,435520408,
436223401,436927483,437630680,438335722,439044895,439743389,440450231,441153125,441857678,442565198,443269304,443974841,444679175,445385072,446088554,446797199,447504404,448205624,
448912721,449618301,450326049,451032543,451741629,452446506,453153876,453857523,454567509,455272020,455984652,456685908,457395855,458106714,458811870,459515239,460227481,460931398,
461642206,462352822,463058722,463765777,464477683,465183976,465893443,466603720,467313295,468021373,468729643,469439420,470147714,470859944,471570143,472277495,472988765,473698304,
474407795,475120511,475832561,476537999,477254048,477961886,478669442,479384504,480091623,480803430,481514298,482228190,482941191,483649338,484361106,485072793,485786697,486498561,
487210962,487920702,488633745,489348636,490059700,490771174,491483332,492200542,492911542,493626070,494338612,495051529,495763975,496479115,497194555,497906047,498619093,499332487,
500049352,500760521,501473831,502188992,502908239,503622437,504334565,505050491,505764584,506479331,507196193,507907427,508628513,509341886,510058016,510770939,511489539,512204706,
512922333,513634515,514351722,515068320,515787624,516499986,517217805,517933209,518653170,519366957,520087101,520802874,521519868,522240693,522956302,523669762,524390170,525107971,
525826891,526546819,527263921,527981026,528701386,529416217,530135716,530853448,531573097,532292668,533012725,533729630,534448889,535168847,535887077,536606378,537328541,538047797,
538764527,539486270,540206009,540926015,541646714,542366147,543087767,543806462,544525802,545248533,545968167,546690540,547412943,548131638,548852013,549573993,550295346,551015220,
551736255,552460623,553184043,553901814,554628834,555348711,556070142,556793899,557514751,558235870,558957343,559680094,560405926,561126037,561848557,562573201,563296147,564020422,
564742312,565464610,566191636,566915731,567640315,568357742,569083388,569810408,570531641,571254377,571981205,572703953,573430514,574155641,574878125,575605127,576330071,577053074,
577781555,578506598,579228656,579954180,580683330,581408451,582130311,582856647,583583091,584308326,585033696,585762063,586483704,587212749,587939067,588662973,589394376,590120535,
590843739,591573892,592297966,593026897,593754610,594480715,595208782,595933660,596662192,597391507,598120300,598843945,599572444,600301414,601027975,601755403,602484466,603214141,
603942035,604672187,605401340,606128966,606859946,607585601,608316428,609048155,609770456,610503431,611233460,611957744,612690593,613420877,614149631,614879189,615606911,616339005,
617067642,617797926,618528777,619257459,619992702,620719455,621449247,622184064,622909272,623640282,624374427,625108851,625836849,626565783,627297765,628028079,628764850,629491354,
630228127,630956689,631686061,632417623,633157120,633885187,634614577,635349004,636081496,636812731,637546195,638275972,639012937,639745858,640473559,641205569,641944109,642678866,
643408280,644142056,644875841,645609296,646345238,647077898,647810183,648545099,649277858,650011493,650746541,651482822,652215470,652951133,653684861,654417924,655151868,655886055,
656623647,657354498,658091004,658827987,659566698,660291630,661032114,661767018,662502213,663236085,663977463,664707330,665443761,666183078,666917818,667654423,668388115,669123922,
669862411,670599190,671335360,672071854,672811213,673543108,674278678,675020377,675755302,676490011,677229892,677967916,678702022,679441642,680180729,680916644,681655211,682392938,
683129246,683864369,684608597,685341668,686083355,686817680,687555659,688297286,689035058,689773514,690511604,691247435,691988138,692732513,693467070,694208181,694947624,695681736,
696424803,697162146,697902753,698641971,699382125,700121421,700864110,701599758,702342171,703079172,703820712,704559423,705302373,706045731,706783282,707521930,708263869,709002631,
709742545,710485180,711225964,711967933,712712200,713445457,714187678,714935257,715672300,716412007,717157240,717896923,718640146,719384743,720123520,720866972,721609826,722351057,
723088853,723835001,724578356,725322380,726060476,726803999,727547888,728287382,729027701,729772697,730518878,731262857,732001760,732747305,733487030,734236271,734975796,735718638,
736463922,737210577,737952168,738695847,739438104,740181276,740925258,741670368,742415616,743158521,743907162,744646215,745391298,746137974,746877699,747629514,748372141,749116078,
749861182,750611587,751351927,752099866,752845459,753585166,754330951,755076112,755822797,756568951,757313767,758062534,758804944,759549730,760297993,761041486,761793091,762537955,
763282937,764029913,764775497,765521147,766271102,767013233,767762618,768511736,769258625,770001653,770750216,771496298,772241996,772991837,773739365,774484655,775231124,775983623,
776729909,777478785,778222062,778975086,779722650,780469776,781214367,781964868,782712270,783460359,784214271,784955703,785706219,786454329,787205742,787953834,788705055,789452514,
790197699,790949199,791697553,792445060,793198897,793944574,794692894,795444193,796196932,796943368,797697067,798440251,799196128,799944901,800695012,801442402,802196884,802944955,
803693299,804446566,805195234,805944796,806697413,807444452,808195982,808950974,809701106,810452024,811200026,811950509,812702354,813455789,814207583,814956947,815708714,816458009,
817214357,817962995,818712734,819465368,820220039,820973042,821720739,822475089,823225137,823982409,824732484,825483420,826237716,826986768,827740470,828492513,829247532,829996788,
830750064,831507963,832256868,833011302,833768097,834517884,835269966,836025489,836777974,837528898,838286098,839040184,839795701,840548755,841303513,842055109,842809687,843562072,
844319383,845069977,845826868,846578455,847336036,848087137,848841586,849596065,850347778,851105422,851861143,852616934,853369688,854127062,854879789,855632960,856393985,857145773,
857902199,858654842,859413392,860167661,860923829,861678176,862435637,863192558,863946500,864700136,865457567,866213288,866972462,867726438,868484913,869241318,869994189,870752706,
871508127,872269419,873019536,873780774,874542621,875291277,876052275,876807645,877564338,878324145,879080442,879836268,880592301,881349729,882106752,882868383,883625605,884385217,
885142177,885896506,886654531,887412163,888171403,888927280,889685308,890445373,891205093,891960496,892724722,893477536,894238723,894999127,895754119,896514586,897274141,898032607,
898789234,899548988,900308228,901069058,901829558,902587943,903347414,904108676,904865258,905626781,906383939,907142804,907900625,908664131,909423854,910180466,910947152,911700059,
912461981,913223768,913984670,914743079,915507815,916266198,917027787,917790126,918546456,919306854,920069880,920826990,921589914,922350273,923110641,923871450,924633468,925396260,
926158899,926919516,927676794,928438383,929206629,929969028,930722940,931487691,932252908,933011197,933772252,934539532,935297554,936060115,936820774,937585381,938349007,939106327,
939870886,940634179,941397301,942159034,942926134,943680583,944441458,945208138,945974818,946733134,947499871,948262495,949026212,949790003,950551733,951313190,952079105,952840013,
953605502,954372188,955133636,955893074,956667908,957424238,958187834,958955885,959717225,960479501,961244600,962007479,962771843,963541919,964304375,965064149,965831871,966596217,
967361193,968125131,968889360,969656313,970419783,971187243,971948340,972715659,973481631,974245515,975014457,975779802,976541595,977310471,978071214,978841608,979602699,980371293,
981135957,981897828,982667971,983431915,984202864,984968098,985733110,986499259,987266401,988032043,988796230,989566222,990332617,991100971,991862125,992629312,993403276,994165129,994928890,995696305,996466126,997232989,998005783,998765008,999534850,1000299104,1001064584,1001835188,1002604415,1003368701,1004134874,1004908139,1005672824,1006442933,1007212634,1007977688,1008745112,1009510568,1010279480,1011049367,1011820424,1012586270,1013355527,1014123143,1014891257,1015655057,1016430605,1017199173,1017967599,1018736016,1019505480,1020271965,1021041234,1021812276,1022580459,1023350430,1024116114,1024888512,1025657436,1026428508,1027198026,1027963995,1028733576,1029501285,1030273254,1031044842,1031814285,1032580686,1033352145,1034119452,1034895187,1035667321,1036436389,1037203219,1037971138,1038739087,1039519033,1040281057,1041056239,1041824113,1042593292,1043366920,1044138856,1044905977,1045676122,1046454097,1047217852,1047991882,1048760611,1049533756,1050304753,1051072738,1051848763,1052618320,1053388226,1054156766,1054930436,1055704631,1056474002,1057244339,1058019299,1058789543,1059562061,1060338524,1061106749,1061875421,1062647462,1063423088,1064192048,1064968349,1065739754,1066511516,1067283737,1068055235,1068826187,1069599440,1070371964,1071145194,1071922413,1072687899,1073465940,1074237465,1075011678,1075780416,1076557452,1077330405,1078103592,1078880034,1079649201,1080429933,1081198260,1081970244,1082747013,1083515109,1084290966,1085060475,1085837874,1086611301,1087384530,1088161491,1088933931,1089708352,1090480111,1091256028,1092026404,1092805096,1093576318,1094348464,1095131107,1095901138,1096679920,1097452981,1098225901,1098998032,1099776439,1100546623,1101324478,1102101496,1102874383,1103649940,1104429148,1105198204,1105972834,1106753122,1107530027,1108302347,1109078483,1109846822,1110628946,1111403615,1112177384,1112949974,1113732941,1114504070,1115282786,1116058661,1116836690,1117606625,1118383631,1119160982,1119937082,1120716269,1121489792,1122268280,1123047614,1123819577,1124595011,1125370040,1126149008,1126926072,1127699787,1128477528,1129258389,1130033481,1130807766,1131590220,1132366389,1133145384,1133918223,1134695439,1135472052,1136251875,1137032325,1137803748,1138584528,1139355423,1140134274,1140913293,1141694700,1142467749,1143247134,1144025496,1144806114,1145582935,1146363631,1147135225,1147918897,1148691919,1149473476,1150247302,1151033311,1151806000,1152585682,1153367152,1154142718,1154922733,1155699097,1156478860,1157258494,1158037564,1158816760,1159594978,1160376616,1161155518,1161931981,1162713706,1163490991,1164269962,1165050047,1165826609,1166607080,1167387470,1168166429,1168943162,1169728301,1170508073,1171287755,1172069489,1172843987,1173623906,1174405754,1175185442,1175965559,1176742814,1177525739,1178305871,1179087695,1179864143,1180647122,1181427026,1182209345,1182987068,1183768524,1184544843,1185327099,1186113099,1186891968,1187672046,1188451764,1189231548,1190014770,1190794821,1191579396,1192359297,1193139159,1193921046,1194701295,1195482141,1196263959,1197047499,1197828120,1198610889,1199388486,1200173601,1200951168,1201734111,1202517927,1203299139,1204082632,1204861720,1205643058,1206428584,1207212325,1207993102,1208774083,1209555400,1210338607,1211117689,1211905756,1212681331,1213469959,1214250958,1215031669,1215811615,1216596010,1217382121,1218162316,1218949324,1219730098,1220511517,1221293920,1222078549,1222860874,1223648264,1224427064,1225209401,1225994138,1226777447,1227562133,1228348736,1229128541,1229911895,1230693440,1231478981,1232265353,1233046787,1233833324,1234617986,1235403758,1236180860,1236963860,1237749398,1238533538,1239316862,1240105985,1240888787,1241671382,1242457907,1243238655,1244024547,1244808864,1245592773,1246378065,1247161026,1247948229,1248732819,1249520541,1250299404,1251087339,1251872700,1252659009,1253440962,1254230499,1255014081,1255797657,1256589126,1257372369,1258155357,1258944993,1259724453,1260510465,1261299570,1262081664,1262866794,1263658864,1264439911,1265230171,1266011407,1266795793,1267583893,1268370265,1269152875,1269941371,1270728997,1271514676,1272297139,1273085914,1273871098,1274662021,1275448654,1276228591,1277017981,1277805901,1278592015,1279378204,1280163322,1280949103,1281739132,1282529857,1283317360,1284102764,1284887021,1285671464,1286461988,1287251297,1288034243,1288822037,1289608811,1290399908,1291184027,1291974818,1292759675,1293549509,1294333082,1295124656,1295909333,1296696524,1297485281,1298273432,1299061961,1299851909,1300637813,1301429636,1302217862,1302999755,1303790168,1304578646,1305366771,1306156083,1306945152,1307730102,1308517311,1309306668,1310096028,1310885421,1311673515,1312464753,1313256978,1314041517,1314827406,1315622151,1316402397,1317199611,1317987639,1318778052,1319563458,1320357663,1321142670,1321932549,1322724600,1323514185,1324301655,1325096385,1325876443,1326666421,1327462021,1328250880,1329037645,1329831163,1330623598,1331404243,1332201004,1332987427,1333780387,1334569882,1335352591,1336149340,1336937674,1337728864,1338518224,1339305850,1340100427,1340889718,1341680674,1342469935,1343263021,1344051553,1344838801,1345633138,1346424061,1347212093,1348004537,1348800959,1349586155,1350378413,1351173320,1351960112,1352750921,1353546728,1354335197,1355122751,1355919131,1356706877,1357499597,1358291906,1359089759,1359878486,1360665524,1361458478,1362251771,1363039280,1363834118,1364624039,1365417758,1366206200,1366999805,1367797829,1368584637,1369374153,1370167737,1370965059,1371754701,1372546251,1373336523,1374127116,1374920148,1375712607,1376506464,1377307554,1378092909,1378887189,1379679411,1380469650,1381259601,1382057874,1382852706,1383642255,1384438047,1385227758,1386022821,1386814914,1387610655,1388397237,1389194448,1389989997,1390782904,1391577433,1392364330,1393158106,1393958224,1394742823,1395541642,1396336996,1397130964,1397926573,1398716140,1399509850,1400304895,1401099442,1401889891,1402687291,1403483047,1404275593,1405064575,1405867591,1406657662,1407455311,1408246729,1409040343,1409834707,1410631261,1411421713,1412217884,1413015320,1413807434,1414604297,1415397605,1416194834,1416990539,1417781486,1418577986,1419371525,1420170398,1420962719,1421760251};
int work(int n){
	int tot=0;
	rep(i,1,sqrt(n)){
		if(n%i)continue;
			tot++;
			if(i!=n/i)tot++;
	}return tot;
}
long long work(int l,int r){
	long long ans=0;
	rep(i,l,r){
		rep(j,1,sqrt(i)){
			if(i%j)continue;
			ans+=d[j];
			if(j!=i/j){
				if(d[i/j]!=0)
				ans+=d[i/j];
				else{
					d[i/j]=work(i/j);
					ans+=d[i/j];
				}
			}
		}
	}
	return ans;
}
int main()
{
	//freopen("divisor.in","r",stdin);
	//freopen("divisor.out","w",stdout);
	scanf("%d",&n);
	rep(i,1,sqrt(10000000)){
		d[i]=work(i);
	}
	int blo=5000;
	int star=n/blo;
	int more=ans[star];
	cout<<work(star*blo+1,n)+more;
	return 0;
}

100pts

#include<bits/stdc++.h>
#define rep(i,a,b) for(ll i=(a);i<=(b);i++)
#define ll long long
using namespace std;
ll n,topx,topy,tot1,tot2,ans=0;
int main()
{
	//freopen("divisor.in","r",stdin);
	//freopen("divisor.out","w",stdout);
	scanf("%lld",&n);
	topx=pow(n,0.3333333333);
	rep(x,1,topx){
		topy=sqrt(n/x);
		rep(y,x,topy){
			ll xy=x*y;
			ans+=max(0*1LL,n/xy-y+1);
		}
	}
	ans*=6;
	rep(i,1,sqrt(n))tot1+=n/(i*i);
	tot2=topx;
	ans=ans-tot1*3-tot2*2;
	cout<<ans;
	return 0;
}

T2

Solution

 

Code


10pts

#include<bits/stdc++.h>
#define N 200003
using namespace std;
int n,m;
struct data1{int pos,color,pre;}w[N];
struct data{int l,r,id,time;}q[N];
int belong[N],last[N],cnt,sz,vis[N],ans[N],ans1;
map<int,int> color;
map<int,int> num;
int cmp(data a,data b)
{
	if (belong[a.l]==belong[b.l])
	return a.r<b.r;
	return belong[a.l]<belong[b.l];
}
void calc(int x)
{
	if (vis[x]){
		if (!--num[color[x]])  ans1--;
    }
	else{
		if (++num[color[x]]==1)  ans1++;
    }
	vis[x]^=1;
}
void change(int x,int c)
{
	if (vis[x]){
	 	calc(x);
	 	color[x]=c;
	 	calc(x);
	}
	else color[x]=c;
}
int main()
{
	//freopen("simple.in","r",stdin);
	//freopen("simple.out","w",stdout);
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++)
	scanf("%d",&color[i]),last[i]=color[i];
	for (int i=1;i<=m;i++)
	{
		int op;scanf("%d",&op);
		int x,y; scanf("%d%d",&x,&y);
		if (op==2){
		w[++cnt].pos=x; w[cnt].color=y; w[cnt].pre=last[x];
			last[x]=y;
		}
		else{
		q[++sz].l=x; q[sz].r=y; q[sz].id=sz; q[sz].time=cnt;
		}
	}
	int block=sqrt(n);
	for (int i=1;i<=n;i++)
	belong[i]=(i-1)/block+1;
	sort(q+1,q+sz+1,cmp);
    int l,r;
    l=r=1; calc(l);
	for (int i=1;i<=sz;i++){
	 	for (int j=q[i-1].time+1;j<=q[i].time;j++)
	 	change(w[j].pos,w[j].color);
	 	for (int j=q[i-1].time;j>q[i].time;j--)
	 	change(w[j].pos,w[j].pre);
	 	while (l<q[i].l)  calc(l++);
	 	while (l>q[i].l)  calc(--l);
	 	while (r<q[i].r)  calc(++r);
	 	while (r>q[i].r)  calc(r--);
	 	ans[q[i].id]=ans1;
	}
	for (int i=1;i<=sz;i++)printf("%d\n",ans[i]);
}

T3

Solution

 Code


30pts

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define ll long long
using namespace std;
const int N=4e6;
struct node{int l,r,v;}e[N];
int fa[N],tot=-1,cnt=0,n,m,a,b,c,l,r,v;
ll ans=0;
bool cmp(node a,node b){return a.v<b.v;}
int getfa(int x){
	if(fa[x]==x)return x;
	return fa[x]=getfa(fa[x]);
}
int main()
{
	//freopen("spanning.in","r",stdin);
	//freopen("spanning.out","w",stdout);
	scanf("%d%d",&n,&m);
	rep(i,0,n-1)fa[i]=i;
	rep(i,1,m){
		scanf("%d%d%d",&l,&r,&v);
		tot=-1;
		for(int j=0;j<=1000;j++){
			++tot;
			e[++cnt].l=(l+j)%n;
			e[cnt].r=(r+j)%n;
			e[cnt].v=v+tot;
			++tot;
			e[++cnt].l=(l+j+1)%n;
			e[cnt].r=(r+j)%n;
			e[cnt].v=v+tot;
		}
	}
	sort(e+1,e+cnt+1,cmp);
	rep(i,1,cnt){
		l=e[i].l,r=e[i].r,v=e[i].v;
		a=getfa(l),b=getfa(r);
		if(a==b)continue;
		ans+=v;fa[a]=b;
	}
	cout<<ans;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值