#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <set> #include <map> #include <cmath> #include <list> #include <ctime> #include <sstream> #include <queue> #include <stack> #include <bitset> #include <unordered_set> #include <unordered_map> using namespace std; typedef vector<int> vi; typedef pair<int,int> pii; typedef long long ll; typedef short int sint; #define FOR(x, b, e) for(int x=(b); x<=(e); ++x) #define FORD(x, b, e) for(int x=((int)(b))-1; x>=(e); --x) #define REP(x, n) for(int x=0; x<(n); ++x) #define ALL(c) c.begin(),c.end() #define SIZE(x) ((int)((x).size())) #define PB push_back #define ST first #define ND second #define mp(x,y) make_pair(x,y) #define DEBUG 1 #define debug(x) {if (DEBUG)cerr <<#x <<" = " <<x <<endl; } #define debugv(x) {if (DEBUG) {cerr <<#x <<" = "; FOREACH(it, (x)) cerr <<*it <<", "; cout <<endl; }} #define REMAX(a,b) (a)=max((a),(b)); #define REMIN(a,b) (a)=min((a),(b)); #define wez(n) int (n); scanf("%d",&(n)); #define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m)); bool debug = false ;//true; const int N = 202002; const int M = 1001010; struct Grupa { ll first; int ile; ll czek; ll nextD; int next,prev; bool deleted; void print(int ind) const { if (!debug) { return; } printf("Grupa: %d, first: %lld, ile : %d, czek: %lld, nextD: %lld, (%d, %d)\n", ind, first, ile, czek, nextD, prev, next); } } zb[N]; ll t[N]; int n, m, grCnt; ll res[M]; int d[N], maxD; ll sum, toAdd, currAdd; ll dist(int x, int y) { if (x == -1 || y == -1) { return M; } Grupa pre = zb[x], post = zb[y]; return (post.first - pre.first + pre.ile) / (pre.ile ); } typedef pair<ll, int> PLI; struct compare { bool operator() (const PLI& p1,const PLI& p2) { if (p1.ST != p2.ST) { return p1.ST > p2.ST; } if (zb[p1.ND].first != zb[p2.ND].first) { return zb[p1.ND].first > zb[p2.ND].first; } return p1.ND > p2.ND; } }; ll npo2(ll n) { return (n - 1) * (n) / 2; } Grupa lacz(Grupa pre, Grupa post, ll d) { ll zas = pre.first + (d * (pre.ile - 1)); ll lew = post.first - d; ll zakres = zas - lew; currAdd += zakres * post.ile; if (debug)printf("toAdd: %lld, pre.ile: %d, post.ile %d\n", toAdd, pre.ile, post.ile); toAdd = toAdd - npo2(pre.ile) - npo2(post.ile) + npo2(pre.ile + post.ile); if (debug)printf("new toAdd: %lld\n", toAdd); pre.ile += post.ile; return pre; } void usun(Grupa& grupa) { zb[grupa.prev].next = grupa.next; zb[grupa.next].prev = grupa.prev; grupa.deleted = true; } priority_queue<PLI, vector<PLI>, compare> nasty; int main() { scanf("%d %d", &n, &m); REP(i, n) { scanf("%lld", &t[i + 1]); } ++n; REP(i, m) { scanf("%d", &d[i]); if (d[i] > maxD) { maxD = d[i]; } } grCnt = n; REP(i, n) { zb[i].first = t[i]; zb[i].ile = 1; } REP(i, n - 1) { zb[i].nextD = dist(i, i + 1); zb[i].next = i + 1; zb[i + 1].prev = i; nasty.push(mp(zb[i].nextD, i)); zb[i].print(i); } zb[0].prev = -1; zb[n - 1].next = -1; zb[n - 1].nextD = M; FOR(d, 1, maxD) { sum += toAdd; currAdd = 0; if (debug)printf("d: %d, toAdd: %lld\n", d, toAdd); if (debug)printf(" on top: "); zb[nasty.top().ND].print(nasty.top().ND); while ((nasty.top()).ST <= d) { auto next = nasty.top(); nasty.pop(); if (zb[next.ND].deleted || zb[next.ND].nextD != next.ST) { if (debug)printf("\tskipping: %d\n", next.ND); continue; } if (debug)printf("\tsciagam grupe z pocz: %lld\n", zb[next.ND].first); zb[next.ND] = lacz(zb[next.ND], zb[zb[next.ND].next], d); usun(zb[zb[next.ND].next]); if (debug)printf("\t updated: "); zb[next.ND].print(next.ND); zb[next.ND].nextD = dist(next.ND, zb[next.ND].next); nasty.push(mp(zb[next.ND].nextD, next.ND)); // lacz z nastepnym, usuwaj nastepnego } sum += currAdd; if (debug)printf("\tres[%d] = %lld, curAdd: %lld\n", d, sum, currAdd); res[d] = sum; } REP(i, m) { printf("%lld\n", res[d[i]]); } return 0; }
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 154 155 156 157 158 159 160 161 162 163 164 | #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <set> #include <map> #include <cmath> #include <list> #include <ctime> #include <sstream> #include <queue> #include <stack> #include <bitset> #include <unordered_set> #include <unordered_map> using namespace std; typedef vector<int> vi; typedef pair<int,int> pii; typedef long long ll; typedef short int sint; #define FOR(x, b, e) for(int x=(b); x<=(e); ++x) #define FORD(x, b, e) for(int x=((int)(b))-1; x>=(e); --x) #define REP(x, n) for(int x=0; x<(n); ++x) #define ALL(c) c.begin(),c.end() #define SIZE(x) ((int)((x).size())) #define PB push_back #define ST first #define ND second #define mp(x,y) make_pair(x,y) #define DEBUG 1 #define debug(x) {if (DEBUG)cerr <<#x <<" = " <<x <<endl; } #define debugv(x) {if (DEBUG) {cerr <<#x <<" = "; FOREACH(it, (x)) cerr <<*it <<", "; cout <<endl; }} #define REMAX(a,b) (a)=max((a),(b)); #define REMIN(a,b) (a)=min((a),(b)); #define wez(n) int (n); scanf("%d",&(n)); #define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m)); bool debug = false ;//true; const int N = 202002; const int M = 1001010; struct Grupa { ll first; int ile; ll czek; ll nextD; int next,prev; bool deleted; void print(int ind) const { if (!debug) { return; } printf("Grupa: %d, first: %lld, ile : %d, czek: %lld, nextD: %lld, (%d, %d)\n", ind, first, ile, czek, nextD, prev, next); } } zb[N]; ll t[N]; int n, m, grCnt; ll res[M]; int d[N], maxD; ll sum, toAdd, currAdd; ll dist(int x, int y) { if (x == -1 || y == -1) { return M; } Grupa pre = zb[x], post = zb[y]; return (post.first - pre.first + pre.ile) / (pre.ile ); } typedef pair<ll, int> PLI; struct compare { bool operator() (const PLI& p1,const PLI& p2) { if (p1.ST != p2.ST) { return p1.ST > p2.ST; } if (zb[p1.ND].first != zb[p2.ND].first) { return zb[p1.ND].first > zb[p2.ND].first; } return p1.ND > p2.ND; } }; ll npo2(ll n) { return (n - 1) * (n) / 2; } Grupa lacz(Grupa pre, Grupa post, ll d) { ll zas = pre.first + (d * (pre.ile - 1)); ll lew = post.first - d; ll zakres = zas - lew; currAdd += zakres * post.ile; if (debug)printf("toAdd: %lld, pre.ile: %d, post.ile %d\n", toAdd, pre.ile, post.ile); toAdd = toAdd - npo2(pre.ile) - npo2(post.ile) + npo2(pre.ile + post.ile); if (debug)printf("new toAdd: %lld\n", toAdd); pre.ile += post.ile; return pre; } void usun(Grupa& grupa) { zb[grupa.prev].next = grupa.next; zb[grupa.next].prev = grupa.prev; grupa.deleted = true; } priority_queue<PLI, vector<PLI>, compare> nasty; int main() { scanf("%d %d", &n, &m); REP(i, n) { scanf("%lld", &t[i + 1]); } ++n; REP(i, m) { scanf("%d", &d[i]); if (d[i] > maxD) { maxD = d[i]; } } grCnt = n; REP(i, n) { zb[i].first = t[i]; zb[i].ile = 1; } REP(i, n - 1) { zb[i].nextD = dist(i, i + 1); zb[i].next = i + 1; zb[i + 1].prev = i; nasty.push(mp(zb[i].nextD, i)); zb[i].print(i); } zb[0].prev = -1; zb[n - 1].next = -1; zb[n - 1].nextD = M; FOR(d, 1, maxD) { sum += toAdd; currAdd = 0; if (debug)printf("d: %d, toAdd: %lld\n", d, toAdd); if (debug)printf(" on top: "); zb[nasty.top().ND].print(nasty.top().ND); while ((nasty.top()).ST <= d) { auto next = nasty.top(); nasty.pop(); if (zb[next.ND].deleted || zb[next.ND].nextD != next.ST) { if (debug)printf("\tskipping: %d\n", next.ND); continue; } if (debug)printf("\tsciagam grupe z pocz: %lld\n", zb[next.ND].first); zb[next.ND] = lacz(zb[next.ND], zb[zb[next.ND].next], d); usun(zb[zb[next.ND].next]); if (debug)printf("\t updated: "); zb[next.ND].print(next.ND); zb[next.ND].nextD = dist(next.ND, zb[next.ND].next); nasty.push(mp(zb[next.ND].nextD, next.ND)); // lacz z nastepnym, usuwaj nastepnego } sum += currAdd; if (debug)printf("\tres[%d] = %lld, curAdd: %lld\n", d, sum, currAdd); res[d] = sum; } REP(i, m) { printf("%lld\n", res[d[i]]); } return 0; } |