1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| #include<bits/stdc++.h> #include<cassert> using namespace std;bool Mbe; #ifdef LOCAL #define debug(...) fprintf(stdout, ##__VA_ARGS__) #else #define debug(...) void(0) #endif #define int long long inline int read(){int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+ (x<<3)+(ch^48);ch=getchar();}return x*f;} #define Z(x) (x)*(x) #define pb push_back #define fi first #define se second
struct node { int n, s; bool operator < (const node &A) const { return s < A.s; } };
vector<node>q1, q2; int n, m, i, j, k, T; vector<int>G; map<int, int>mp; int l, r, ans, sum;
bool Med; signed main() { #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); atexit([]{fprintf(stderr,"%.lfms\n%lfMB\n",clock()*1000./CLOCKS_PER_SEC,(&Mbe-&Med)/1024./1024);}); #endif
n = read(); m = read(); if(n == 1) { printf("%lld", m); return 0; } for(l = 1; l <= n; l = r + 1) { r = min(n, n / (n / l));
G.pb(l); G.pb(r); } for(i = 1; i <= m; ++i) { l = read(); r = read(); mp[l]++; mp[r + 1]--; G.pb(l); G.pb(r + 1);
} sort(G.begin(), G.end()); G.erase(unique(G.begin(), G.end()), G.end());
m = G.size(); int mn = 1e18, len = 0; for(i = 0; i < m - 1; ++i) { sum += mp[G[i]]; if(sum) { q1.pb({G[i + 1] - G[i], sum * (n / G[i])}); len += G[i + 1] - G[i]; mn = min(mn, sum * (n / G[i])); debug("[%lld %lld] %lld(%lld * %lld)\n", G[i], G[i + 1] - 1, sum * (n / G[i]), sum, n / G[i]); } } if(len != n) ans = mn; sort(q1.begin(), q1.end()); i = j = 0; int flg = 0; auto Take = [&] () -> node { node t; if(i == q1.size()) t = q2[j], ++j, flg = 2; else if(j == q2.size()) t = q1[i], ++i, flg = 1; else { if(q1[i].s < q2[j].s) t = q1[i], ++i, flg = 1; else t = q2[j], ++j, flg = 2; } return t; }; while(i < q1.size() || j < q2.size()) { auto t = Take(); debug("(%lld %lld)\n", t.n, t.s); if(t.n >= 2) { q2.pb({t.n / 2, t.s * 2}); ans += t.n / 2 * 2 * t.s; if(t.n % 2 == 0) continue; } if(i < q1.size() || j < q2.size()) { auto t2 = Take(); if(t2.n > 1) { if(flg == 1) --i, q1[i].n--; if(flg == 2) --j, q2[j].n--; } ans += t.s + t2.s; q2.pb({1, t.s + t2.s}); } } printf("%lld", ans); return 0; }
|