#include <iostream> #include <map> #include <algorithm> #define D(x) #define D2(x) using namespace std; typedef long long I; #define MAX 200100 int n,m; struct Zap { I d; int idx; I w; I a,b; }; Zap zap[MAX]; I t[MAX]; I tS[MAX]; I pa[MAX], pb[MAX]; map<int, int> mp; int currentTimeidx; I getTime(int i) { auto it = mp.upper_bound(i); int idx = it->second; I w = t[idx]+zap[i].d*(currentTimeidx - idx); D2(cout << "i: " << i << " getTime: " << it->first << " " << idx << "=" << w << "\n"); return w; } int find(I time, int a, int b) { if(b-a<=1) { if(getTime(a) <= time) return b; return a; } int s = (a+b)/2; if(getTime(s) <= time) return find(time, s, b); return find(time, a, s); } void add(int a, int b, int startTimeIdx) { I k = currentTimeidx - startTimeIdx - 1; D(cout << "add: (" << a << "," << b << ") " << t[startTimeIdx] << "---->" << t[currentTimeidx] << "\n"); if(k < 1) return; I ts = tS[currentTimeidx-1] - tS[startTimeIdx]; /* for(int i = startTimeIdx+1; i < currentTimeidx; i++) { ts+=t[i]; }*/ I bp = k*t[startTimeIdx] - ts; I ap = (k+1)*k/2; zap[a].a+=ap; zap[b].a-=ap; zap[a].b+=bp; zap[b].b-=bp; /* for(int i=a;i<b;i++) { zap[i].w += bp+ap*zap[i].d; D2(cout << zap[i].w << " " << k*t[startTimeIdx] << " + " << (k+1)*k/2*zap[i].d << " - " << ts << " | "); }*/ D2(cout << " ts: " << ts << " k:" << k << "\n"); } void printW() { cout << "w: "; for(int i=0;i<m;i++){ cout << zap[i].w << " "; } cout << "\n"; } void remove(int f) { int last = 0; while(!mp.empty()) { auto it = mp.begin(); if(it->first > f) { add(last, f, it->second); return; } add(last, it->first, it->second); last = it->first; mp.erase(it); } } void update(int f) { remove(f); mp.insert(make_pair(f, currentTimeidx)); D(printW()); D(cout << "update new: " << f << " time:" << t[currentTimeidx] << " ~" << mp.size() << "\n"); } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> t[i]; tS[0] = t[0] = 0; t[++n] = 1LL << 60; for (int i = 1; i <= n; i++) tS[i] = tS[i-1]+t[i]; for (int i = 0; i < m; i++) { cin >> zap[i].d; zap[i].idx = i; } sort(zap, zap + m, [](const Zap&x,const Zap&y) { return x.d<y.d;}); mp.insert(make_pair(m, 0)); for (currentTimeidx = 1; currentTimeidx <= n; currentTimeidx++) { D(cout << "currentTime: " << currentTimeidx << " " << t[currentTimeidx] << "\n"); int f = find(t[currentTimeidx], 0, m); D(cout << "f:" << f << "\n"); if(f>0) update(f); } I a=0,b=0; for(int i=0;i<m;i++) { a+=zap[i].a; b+=zap[i].b; zap[i].w += b+a*zap[i].d; } sort(zap, zap + m, [](const Zap&x,const Zap&y) { return x.idx<y.idx;}); for (int i = 0; i < m; i++) { cout << zap[i].w << "\n"; } 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 | #include <iostream> #include <map> #include <algorithm> #define D(x) #define D2(x) using namespace std; typedef long long I; #define MAX 200100 int n,m; struct Zap { I d; int idx; I w; I a,b; }; Zap zap[MAX]; I t[MAX]; I tS[MAX]; I pa[MAX], pb[MAX]; map<int, int> mp; int currentTimeidx; I getTime(int i) { auto it = mp.upper_bound(i); int idx = it->second; I w = t[idx]+zap[i].d*(currentTimeidx - idx); D2(cout << "i: " << i << " getTime: " << it->first << " " << idx << "=" << w << "\n"); return w; } int find(I time, int a, int b) { if(b-a<=1) { if(getTime(a) <= time) return b; return a; } int s = (a+b)/2; if(getTime(s) <= time) return find(time, s, b); return find(time, a, s); } void add(int a, int b, int startTimeIdx) { I k = currentTimeidx - startTimeIdx - 1; D(cout << "add: (" << a << "," << b << ") " << t[startTimeIdx] << "---->" << t[currentTimeidx] << "\n"); if(k < 1) return; I ts = tS[currentTimeidx-1] - tS[startTimeIdx]; /* for(int i = startTimeIdx+1; i < currentTimeidx; i++) { ts+=t[i]; }*/ I bp = k*t[startTimeIdx] - ts; I ap = (k+1)*k/2; zap[a].a+=ap; zap[b].a-=ap; zap[a].b+=bp; zap[b].b-=bp; /* for(int i=a;i<b;i++) { zap[i].w += bp+ap*zap[i].d; D2(cout << zap[i].w << " " << k*t[startTimeIdx] << " + " << (k+1)*k/2*zap[i].d << " - " << ts << " | "); }*/ D2(cout << " ts: " << ts << " k:" << k << "\n"); } void printW() { cout << "w: "; for(int i=0;i<m;i++){ cout << zap[i].w << " "; } cout << "\n"; } void remove(int f) { int last = 0; while(!mp.empty()) { auto it = mp.begin(); if(it->first > f) { add(last, f, it->second); return; } add(last, it->first, it->second); last = it->first; mp.erase(it); } } void update(int f) { remove(f); mp.insert(make_pair(f, currentTimeidx)); D(printW()); D(cout << "update new: " << f << " time:" << t[currentTimeidx] << " ~" << mp.size() << "\n"); } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> t[i]; tS[0] = t[0] = 0; t[++n] = 1LL << 60; for (int i = 1; i <= n; i++) tS[i] = tS[i-1]+t[i]; for (int i = 0; i < m; i++) { cin >> zap[i].d; zap[i].idx = i; } sort(zap, zap + m, [](const Zap&x,const Zap&y) { return x.d<y.d;}); mp.insert(make_pair(m, 0)); for (currentTimeidx = 1; currentTimeidx <= n; currentTimeidx++) { D(cout << "currentTime: " << currentTimeidx << " " << t[currentTimeidx] << "\n"); int f = find(t[currentTimeidx], 0, m); D(cout << "f:" << f << "\n"); if(f>0) update(f); } I a=0,b=0; for(int i=0;i<m;i++) { a+=zap[i].a; b+=zap[i].b; zap[i].w += b+a*zap[i].d; } sort(zap, zap + m, [](const Zap&x,const Zap&y) { return x.idx<y.idx;}); for (int i = 0; i < m; i++) { cout << zap[i].w << "\n"; } return 0; } |