// Artur Kraska, II UWr #include <algorithm> #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #include <cmath> #include <queue> #include <list> #include <set> #include <map> #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(typeof(coll.begin()) iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(typeof(coll.rbegin()) iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #define FOR(i, a, b) for(int i=a; i<=b; i++) #define FORD(i, a, b) for(int i=a; i>=b; i--) #define MP make_pair #define PB push_back #define deb(X) X; #define M 1000000007 #define INF 1000000007 using namespace std; int n, m; long long a; long long res[1000007], suma[1000007]; long long wzrost = 0, wynik = 0, dl = 0; struct fu { long long poz; long long o; long long ile; }; fu tab[1000007]; priority_queue <pair<long long, long long> > pq; // <czas, dodatni-polacz/ujemny-policz> long long Find(long long nr) { if(tab[nr].o == nr) return nr; tab[nr].o = Find(tab[nr].o); return tab[nr].o; } void Union(long long a) { a = Find(a); // cout << "a: " << a << ", b: " << a + tab[a].ile << endl; long long b = Find(a + tab[a].ile); // cout << " teraz, na dlugosci " << dl << " bedzie laczyl " << a << " i " << b << endl; if(a == b || b == n+1) return ; wynik -= (tab[a].ile*tab[a].poz + dl*tab[a].ile*(tab[a].ile-1)/2) - (suma[a+tab[a].ile-1] - suma[a-1]); wynik -= (tab[b].ile*tab[b].poz + dl*tab[b].ile*(tab[b].ile-1)/2) - (suma[b+tab[b].ile-1] - suma[b-1]); wzrost -= tab[a].ile*(tab[a].ile-1)/2; wzrost -= tab[b].ile*(tab[b].ile-1)/2; tab[b].o = a; tab[a].ile += tab[b].ile; wynik += (tab[a].ile*tab[a].poz + dl*tab[a].ile*(tab[a].ile-1)/2) - (suma[a+tab[a].ile-1] - suma[a-1]); wzrost += tab[a].ile*(tab[a].ile-1)/2; } long long my_ceil(long long a, long long b) { return (a+b-1)/b; } int main() { scanf("%d %d", &n, &m); FOR(i, 1, n) { scanf("%lld", &tab[i].poz); suma[i] = suma[i-1] + tab[i].poz; tab[i].o = i; tab[i].ile = 1; } tab[0].ile = 1; tab[n+1].o = n+1; tab[n+1].ile = 1; tab[n+1].poz = M * (long long)M; forr(i, m) { scanf("%lld", &a); pq.push(MP(-a, -i-1)); } FOR(i, 1, n) { pq.push(MP(-(tab[i].poz-tab[i-1].poz), i-1)); } long long last = 0; int ile = 0; while(!pq.empty()) { ile++; pair<long long, long long> p = pq.top(); pq.pop(); dl = -p.first; if(dl > last) { wynik += wzrost*(dl-last); last = dl; } if(p.second < 0) // podlicz { res[-p.second] = wynik; } else // podlacz { long long nr = Find(p.second); bool z = 0; while(nr + tab[nr].ile <= n && tab[nr].poz + dl*tab[nr].ile >= tab[nr + tab[nr].ile].poz) { // cout << "nr: " << nr << ", sasiad: " << nr + tab[nr].ile << endl; Union(nr); z=1; } if(z && nr + tab[nr].ile <= n) { pq.push(MP(-my_ceil(tab[nr + tab[nr].ile].poz - tab[nr].poz, tab[nr].ile), nr)); } } // cout << " po akcji " << p.first << ", " << p.second << " mamy wynik: " << wynik << " i wzrost " << wzrost << endl; } // cout << " n: " << n << ", m: " << m << ", ile: " << ile << endl; FOR(i, 1, m) { printf("%lld\n", res[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 | // Artur Kraska, II UWr #include <algorithm> #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #include <cmath> #include <queue> #include <list> #include <set> #include <map> #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(typeof(coll.begin()) iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(typeof(coll.rbegin()) iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #define FOR(i, a, b) for(int i=a; i<=b; i++) #define FORD(i, a, b) for(int i=a; i>=b; i--) #define MP make_pair #define PB push_back #define deb(X) X; #define M 1000000007 #define INF 1000000007 using namespace std; int n, m; long long a; long long res[1000007], suma[1000007]; long long wzrost = 0, wynik = 0, dl = 0; struct fu { long long poz; long long o; long long ile; }; fu tab[1000007]; priority_queue <pair<long long, long long> > pq; // <czas, dodatni-polacz/ujemny-policz> long long Find(long long nr) { if(tab[nr].o == nr) return nr; tab[nr].o = Find(tab[nr].o); return tab[nr].o; } void Union(long long a) { a = Find(a); // cout << "a: " << a << ", b: " << a + tab[a].ile << endl; long long b = Find(a + tab[a].ile); // cout << " teraz, na dlugosci " << dl << " bedzie laczyl " << a << " i " << b << endl; if(a == b || b == n+1) return ; wynik -= (tab[a].ile*tab[a].poz + dl*tab[a].ile*(tab[a].ile-1)/2) - (suma[a+tab[a].ile-1] - suma[a-1]); wynik -= (tab[b].ile*tab[b].poz + dl*tab[b].ile*(tab[b].ile-1)/2) - (suma[b+tab[b].ile-1] - suma[b-1]); wzrost -= tab[a].ile*(tab[a].ile-1)/2; wzrost -= tab[b].ile*(tab[b].ile-1)/2; tab[b].o = a; tab[a].ile += tab[b].ile; wynik += (tab[a].ile*tab[a].poz + dl*tab[a].ile*(tab[a].ile-1)/2) - (suma[a+tab[a].ile-1] - suma[a-1]); wzrost += tab[a].ile*(tab[a].ile-1)/2; } long long my_ceil(long long a, long long b) { return (a+b-1)/b; } int main() { scanf("%d %d", &n, &m); FOR(i, 1, n) { scanf("%lld", &tab[i].poz); suma[i] = suma[i-1] + tab[i].poz; tab[i].o = i; tab[i].ile = 1; } tab[0].ile = 1; tab[n+1].o = n+1; tab[n+1].ile = 1; tab[n+1].poz = M * (long long)M; forr(i, m) { scanf("%lld", &a); pq.push(MP(-a, -i-1)); } FOR(i, 1, n) { pq.push(MP(-(tab[i].poz-tab[i-1].poz), i-1)); } long long last = 0; int ile = 0; while(!pq.empty()) { ile++; pair<long long, long long> p = pq.top(); pq.pop(); dl = -p.first; if(dl > last) { wynik += wzrost*(dl-last); last = dl; } if(p.second < 0) // podlicz { res[-p.second] = wynik; } else // podlacz { long long nr = Find(p.second); bool z = 0; while(nr + tab[nr].ile <= n && tab[nr].poz + dl*tab[nr].ile >= tab[nr + tab[nr].ile].poz) { // cout << "nr: " << nr << ", sasiad: " << nr + tab[nr].ile << endl; Union(nr); z=1; } if(z && nr + tab[nr].ile <= n) { pq.push(MP(-my_ceil(tab[nr + tab[nr].ile].poz - tab[nr].poz, tab[nr].ile), nr)); } } // cout << " po akcji " << p.first << ", " << p.second << " mamy wynik: " << wynik << " i wzrost " << wzrost << endl; } // cout << " n: " << n << ", m: " << m << ", ile: " << ile << endl; FOR(i, 1, m) { printf("%lld\n", res[i]); } return 0; } |