#include<bits/stdc++.h> #define PII pair<int,int> #define f first #define s second #define VI vector<int> #define LL long long #define MP make_pair #define LD long double #define PB push_back #define ALL(V) V.begin(),V.end() #define abs(x) max((x),-(x)) #define PDD pair<LD,LD> #define VPII vector< PII > #define siz(V) ((int)V.size()) #define FOR(x, b, e) for(int x=b;x<=(e);x++) #define FORD(x, b, e) for(int x=b;x>=(e);x--) #define REP(x, n) for(int x=0;x<(n);x++) #define mini(a,b) a=min(a,b) #define maxi(a,b) a=max(a,b) using namespace std; const int MXN = 2e5+5; LL in[MXN]; int fu[MXN]; int find(int x){return fu[x] == x ? x : fu[x] = find(fu[x]);} struct spoj { LL sum; // czy sum nie powinno być __int128?? LL cnt; LL start; spoj(LL sum, LL cnt, LL start) : sum(sum), cnt(cnt), start(start) { } spoj(){} } t[MXN]; spoj merge(spoj a, spoj b) { return spoj(a.sum + b.sum, a.cnt + b.cnt, min(a.start, b.start)); } LL coeffsum = 0; // ile razy odjąć T żeby otrzymać wynik LL sumall = 0; // suma po sum - start * cnt LL val(spoj a) { //res = start + start+T, start+T*2, start+T*3, ..., start+T*(cnt-1) - sum return a.sum - a.start * a.cnt; } LL coeff(spoj a) { return a.cnt * (a.cnt-1) / 2; } set<pair<LL, int>> starts; priority_queue<pair<LL, PII>, vector<pair<LL, PII>>, greater<pair<LL, PII>>> que; LL get_diff(int x, int y) { if(x < 0 || y < 0)return 2e18; //może find return (t[y].start - t[x].start) / t[x].cnt + ((t[y].start - t[x].start) % t[x].cnt > 0); } void merge(int x, int y) // TODO: więcej mergeować { // cerr<<"merge: "<<x<<" "<<y<<endl; if(x < 0 || y < 0)return; x = find(x); y = find(y); if(x == y)return; sumall -= val(t[x]); sumall -= val(t[y]); coeffsum -= coeff(t[x]); coeffsum -= coeff(t[y]); starts.erase(MP(t[x].start, x)); starts.erase(MP(t[y].start, y)); fu[y] = x; t[x] = merge(t[x], t[y]); sumall += val(t[x]); coeffsum += coeff(t[x]); starts.insert(MP(t[x].start, x)); auto f = starts.find(MP(t[x].start, x)); f--; // cerr<<"ins: "<< que.push(MP(get_diff(f->s, x), MP(f->s, x))); f++; f++; que.push(MP(get_diff(x, f->s), MP(x, f->s))); } LL act(LL t) { // cerr<<coeffsum<<" "<<sumall<<endl; return coeffsum * t - sumall; } LL ans[MXN]; int main() { int n, m; scanf("%d%d", &n, &m); FOR(i, 0, n)fu[i] = i; LL prev = 0; t[0] = spoj(0, 1, 0); vector<pair<LL, PII>> V; FOR(i, 1, n) { LL a; scanf("%lld", &a); V.PB(MP(a-prev, MP(i, i+1))); prev = a; in[i] = a; t[i] = spoj(a, 1, a); } sort(ALL(V)); reverse(ALL(V)); //sumres na początku 0 //coeffsum na początku 0 FOR(i, 0, n) starts.insert(MP(in[i], i)); starts.insert(MP(-1e18, -1)); starts.insert(MP(2e18, -2)); FOR(i, 1, n) { que.push(MP(get_diff(i-1, i), MP(i-1, i))); } VPII q; REP(i, m) { int a; scanf("%d", &a); q.PB(MP(a, i)); } sort(ALL(q)); REP(i, m) { while(que.size() && que.top().f <= q[i].f) { // cerr<<que.top().f<<" "; int a = que.top().s.f; int b = que.top().s.s; que.pop(); merge(a, b); } // cerr<<"next: "<<que.top().f<<" "<<que.top().s.f<<" "<<que.top().s.f<<endl; ans[q[i].s] = act(q[i].f); } REP(i, m) printf("%lld\n", ans[i]); }
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | #include<bits/stdc++.h> #define PII pair<int,int> #define f first #define s second #define VI vector<int> #define LL long long #define MP make_pair #define LD long double #define PB push_back #define ALL(V) V.begin(),V.end() #define abs(x) max((x),-(x)) #define PDD pair<LD,LD> #define VPII vector< PII > #define siz(V) ((int)V.size()) #define FOR(x, b, e) for(int x=b;x<=(e);x++) #define FORD(x, b, e) for(int x=b;x>=(e);x--) #define REP(x, n) for(int x=0;x<(n);x++) #define mini(a,b) a=min(a,b) #define maxi(a,b) a=max(a,b) using namespace std; const int MXN = 2e5+5; LL in[MXN]; int fu[MXN]; int find(int x){return fu[x] == x ? x : fu[x] = find(fu[x]);} struct spoj { LL sum; // czy sum nie powinno być __int128?? LL cnt; LL start; spoj(LL sum, LL cnt, LL start) : sum(sum), cnt(cnt), start(start) { } spoj(){} } t[MXN]; spoj merge(spoj a, spoj b) { return spoj(a.sum + b.sum, a.cnt + b.cnt, min(a.start, b.start)); } LL coeffsum = 0; // ile razy odjąć T żeby otrzymać wynik LL sumall = 0; // suma po sum - start * cnt LL val(spoj a) { //res = start + start+T, start+T*2, start+T*3, ..., start+T*(cnt-1) - sum return a.sum - a.start * a.cnt; } LL coeff(spoj a) { return a.cnt * (a.cnt-1) / 2; } set<pair<LL, int>> starts; priority_queue<pair<LL, PII>, vector<pair<LL, PII>>, greater<pair<LL, PII>>> que; LL get_diff(int x, int y) { if(x < 0 || y < 0)return 2e18; //może find return (t[y].start - t[x].start) / t[x].cnt + ((t[y].start - t[x].start) % t[x].cnt > 0); } void merge(int x, int y) // TODO: więcej mergeować { // cerr<<"merge: "<<x<<" "<<y<<endl; if(x < 0 || y < 0)return; x = find(x); y = find(y); if(x == y)return; sumall -= val(t[x]); sumall -= val(t[y]); coeffsum -= coeff(t[x]); coeffsum -= coeff(t[y]); starts.erase(MP(t[x].start, x)); starts.erase(MP(t[y].start, y)); fu[y] = x; t[x] = merge(t[x], t[y]); sumall += val(t[x]); coeffsum += coeff(t[x]); starts.insert(MP(t[x].start, x)); auto f = starts.find(MP(t[x].start, x)); f--; // cerr<<"ins: "<< que.push(MP(get_diff(f->s, x), MP(f->s, x))); f++; f++; que.push(MP(get_diff(x, f->s), MP(x, f->s))); } LL act(LL t) { // cerr<<coeffsum<<" "<<sumall<<endl; return coeffsum * t - sumall; } LL ans[MXN]; int main() { int n, m; scanf("%d%d", &n, &m); FOR(i, 0, n)fu[i] = i; LL prev = 0; t[0] = spoj(0, 1, 0); vector<pair<LL, PII>> V; FOR(i, 1, n) { LL a; scanf("%lld", &a); V.PB(MP(a-prev, MP(i, i+1))); prev = a; in[i] = a; t[i] = spoj(a, 1, a); } sort(ALL(V)); reverse(ALL(V)); //sumres na początku 0 //coeffsum na początku 0 FOR(i, 0, n) starts.insert(MP(in[i], i)); starts.insert(MP(-1e18, -1)); starts.insert(MP(2e18, -2)); FOR(i, 1, n) { que.push(MP(get_diff(i-1, i), MP(i-1, i))); } VPII q; REP(i, m) { int a; scanf("%d", &a); q.PB(MP(a, i)); } sort(ALL(q)); REP(i, m) { while(que.size() && que.top().f <= q[i].f) { // cerr<<que.top().f<<" "; int a = que.top().s.f; int b = que.top().s.s; que.pop(); merge(a, b); } // cerr<<"next: "<<que.top().f<<" "<<que.top().s.f<<" "<<que.top().s.f<<endl; ans[q[i].s] = act(q[i].f); } REP(i, m) printf("%lld\n", ans[i]); } |