#include <iostream> #include <algorithm> #include <vector> #include <string> #include <map> #include <set> #include <queue> #include <stack> #include <cmath> #include <cassert> #include <cstring> #include <bitset> #include <sstream> //#include <bits/stdc++.h> #include <ext/numeric> #include <unordered_set> #include <unordered_map> using namespace std ; using namespace __gnu_cxx ; typedef long long LL ; typedef pair<int,int> PII ; typedef vector<int> VI ; const int INF = 1e9+100 ; const LL INFLL = (LL)INF*INF ; #define REP(i,n) for(int i=0;i<(n);++i) #define ALL(c) c.begin(),c.end() #define FOREACH(i,c) for(auto i=(c).begin();i!=(c).end();++i) #define CLEAR(t) memset(t,0,sizeof(t)) #define PB push_back #define MP make_pair #define FI first #define SE second template<class T> void maxi(T &a, const T &b) { a = max(a,b) ; } template<class T> void mini(T &a, const T &b) { a = min(a,b) ; } template<class T1,class T2> ostream& operator<<(ostream &s, const pair<T1,T2> &x) { return s << "(" << x.FI << "," << x.SE << ")" ;} template<class T> ostream& operator<<(ostream &s, const vector<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T> ostream& operator<<(ostream &s, const set<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T1,class T2> ostream& operator<<(ostream &s, const map<T1, T2> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T1, class T2> T1 conv(const T2 &x) { stringstream sss ; sss << x ; T1 y ; sss >> y ; return y ; } template<class T> void _debug(bool printName, const char* s, const T &x) { if(printName) cerr << s << "=" ; cerr << x << endl ; } void _debug(bool, const char* s, const char*) { for(;*s;s++) if(*s!='"') { cerr << *s ; } cerr << endl ; } template<class T, class... R> void _debug(bool printName, const char* s, const T &x, R... y) { bool o=0, str=(*s=='"') ; for(; o || *s!=',' ; s++) if(*s=='"') o=!o ; else if(printName||str) cerr<<*s ; for(s++;*s && *s==' '; s++) ; if(!str) { if(printName) cerr << "=" ; cerr << x ; if(*s!='"' && printName) cerr << "," ; } cerr << " " ; _debug(printName, s, y...) ; } template<class T> void _coord(const T &x) { cerr << "[" << x << "]" ; } template<class T, class... R> void _coord(const T &x, R... y) { cerr << "[" << x << "]" ; _coord(y...) ; } template<class T, class I> void _val(T &t, const I &i) { cerr << t[i] ; } template<class T, class I, class... J> void _val(T &t, const I &i, J... j) { _val(t[i], j...) ; } #ifndef LOCAL #define CERR(...) #define DEBUG(...) #define DARRAY(...) #else #define CERR(...) if(DFLAG) _debug(0, #__VA_ARGS__, __VA_ARGS__) #define DEBUG(...) if(DFLAG) _debug(1, #__VA_ARGS__, __VA_ARGS__) #define DARRAY(t,...) if(DFLAG) { cerr << #t ; _coord(__VA_ARGS__) ; cerr << " = " ; _val(t,__VA_ARGS__) ; cerr << endl ; } #define DFLAG 0 #endif //////////////////////////////////////////////////////////////////////////////// LL triangle(LL n) { assert(n>=0) ; return n*(n+1)/2 ; } // Klasa reprezentujaca funkcje liniowa postaci a*x+b struct FunLin { LL a, b ; FunLin(LL aa=0, LL bb=0) : a(aa), b(bb) {} LL eval(LL x) { return a*x+b ; } FunLin operator+(const FunLin &F) { return FunLin(a+F.a, b+F.b) ; } FunLin operator-(const FunLin &F) { return FunLin(a-F.a, b-F.b) ; } } ; ostream & operator<<(ostream &sss, const FunLin &X) { return sss << X.a << "x" << (X.b>=0 ? "+" : "") << X.b ; } // Klasa reprezentujaca ulamki (nieujemne) struct Rational { LL a, b ; Rational(LL aa=0, LL bb=1) : a(aa), b(bb) {} bool operator<(const Rational &X) const { return a*X.b < X.a*b ; } bool operator==(const Rational &X) const { return a*X.b == X.a*b ; } } ; ostream & operator<<(ostream &sss, const Rational &X) { return sss << X.a << "/" << X.b ; } //////////////////////////////////////// // klasa reprezentujaca jedno z dwoch zdarzen zdarzenie: // - zlaczenie dwoch przedzialow // - pytanie o aktualna wartosc struct Event { Rational time ; bool isJoin ; int data ; // indentyfikator przedzialu albo indeks zapytania Event(Rational tt, bool ii, int dd) : time(tt), isJoin(ii), data(dd) {} ; bool operator<(const Event &e) const { if(!(time==e.time)) return e.time < time ; else if(isJoin!=e.isJoin) return isJoin < e.isJoin ; else return data<e.data ; // whatever } } ; //////////////////////////////////////// const int MAXN=2e5+100 ; LL t[MAXN], prefT[MAXN] ; int d[MAXN] ; // klasa reprezentujaca scalane przedzialy struct Interval { int j, i ; Interval(int jj) : Interval(jj, jj) {} Interval(int jj, int ii) : j(jj), i(ii) {} FunLin getFun() const { return FunLin(triangle(i-j), (i-j)*t[j]-(prefT[i]-prefT[j])) ; } bool operator<(const Interval &interv) const { return i<interv.i ; } } ; Interval join(const Interval &A, const Interval &B) { assert(A.i+1 == B.j) ; return Interval(A.j, B.i) ; } ostream & operator<<(ostream &sss, const Interval &X) { return sss << "[" << X.j << "," << X.i << "]" ; } LL ret[MAXN] ; int main() { ios_base::sync_with_stdio(0) ; int n, m, i ; cin >> n >> m ; for(i=1 ; i<=n ; i++) { cin >> t[i] ; prefT[i] = prefT[i-1]+t[i] ; } set<Interval> intervals = { Interval(0) } ; priority_queue<Event> Q ; for(i=1 ; i<=n ; i++) { intervals.insert(Interval(i)) ; Q.push(Event(t[i]-t[i-1], true, i)) ; } for(i=1 ; i<=m ; i++) { cin >> d[i] ; Q.push(Event(d[i], false, i)) ; } FunLin F ; while(!Q.empty()) { Event e = Q.top() ; Q.pop() ; if(!e.isJoin) { ret[e.data] = F.eval(d[e.data]) ; } else { auto it = intervals.lower_bound(Interval(e.data)) ; assert(it!=intervals.end()) ; if(it->j != e.data) continue ; assert(it!=intervals.begin()) ; auto prev=it ; prev-- ; Interval newInterv = join(*prev, *it) ; F = F - (prev->getFun() + it->getFun()) + newInterv.getFun() ; intervals.erase(prev) ; intervals.erase(it) ; intervals.insert(newInterv) ; it = intervals.lower_bound(Interval(e.data)) ; auto next = it ; next++ ; if(next == intervals.end()) continue ; Q.push(Event(Rational(t[next->j]-t[it->j], (next->j)-(it->j)), true, next->j)) ; } } for(int i=1 ; i<=m ; i++) cout << ret[i] << endl ; }
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | #include <iostream> #include <algorithm> #include <vector> #include <string> #include <map> #include <set> #include <queue> #include <stack> #include <cmath> #include <cassert> #include <cstring> #include <bitset> #include <sstream> //#include <bits/stdc++.h> #include <ext/numeric> #include <unordered_set> #include <unordered_map> using namespace std ; using namespace __gnu_cxx ; typedef long long LL ; typedef pair<int,int> PII ; typedef vector<int> VI ; const int INF = 1e9+100 ; const LL INFLL = (LL)INF*INF ; #define REP(i,n) for(int i=0;i<(n);++i) #define ALL(c) c.begin(),c.end() #define FOREACH(i,c) for(auto i=(c).begin();i!=(c).end();++i) #define CLEAR(t) memset(t,0,sizeof(t)) #define PB push_back #define MP make_pair #define FI first #define SE second template<class T> void maxi(T &a, const T &b) { a = max(a,b) ; } template<class T> void mini(T &a, const T &b) { a = min(a,b) ; } template<class T1,class T2> ostream& operator<<(ostream &s, const pair<T1,T2> &x) { return s << "(" << x.FI << "," << x.SE << ")" ;} template<class T> ostream& operator<<(ostream &s, const vector<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T> ostream& operator<<(ostream &s, const set<T> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T1,class T2> ostream& operator<<(ostream &s, const map<T1, T2> &t) { FOREACH(it, t) s << *it << " " ; return s ; } template<class T1, class T2> T1 conv(const T2 &x) { stringstream sss ; sss << x ; T1 y ; sss >> y ; return y ; } template<class T> void _debug(bool printName, const char* s, const T &x) { if(printName) cerr << s << "=" ; cerr << x << endl ; } void _debug(bool, const char* s, const char*) { for(;*s;s++) if(*s!='"') { cerr << *s ; } cerr << endl ; } template<class T, class... R> void _debug(bool printName, const char* s, const T &x, R... y) { bool o=0, str=(*s=='"') ; for(; o || *s!=',' ; s++) if(*s=='"') o=!o ; else if(printName||str) cerr<<*s ; for(s++;*s && *s==' '; s++) ; if(!str) { if(printName) cerr << "=" ; cerr << x ; if(*s!='"' && printName) cerr << "," ; } cerr << " " ; _debug(printName, s, y...) ; } template<class T> void _coord(const T &x) { cerr << "[" << x << "]" ; } template<class T, class... R> void _coord(const T &x, R... y) { cerr << "[" << x << "]" ; _coord(y...) ; } template<class T, class I> void _val(T &t, const I &i) { cerr << t[i] ; } template<class T, class I, class... J> void _val(T &t, const I &i, J... j) { _val(t[i], j...) ; } #ifndef LOCAL #define CERR(...) #define DEBUG(...) #define DARRAY(...) #else #define CERR(...) if(DFLAG) _debug(0, #__VA_ARGS__, __VA_ARGS__) #define DEBUG(...) if(DFLAG) _debug(1, #__VA_ARGS__, __VA_ARGS__) #define DARRAY(t,...) if(DFLAG) { cerr << #t ; _coord(__VA_ARGS__) ; cerr << " = " ; _val(t,__VA_ARGS__) ; cerr << endl ; } #define DFLAG 0 #endif //////////////////////////////////////////////////////////////////////////////// LL triangle(LL n) { assert(n>=0) ; return n*(n+1)/2 ; } // Klasa reprezentujaca funkcje liniowa postaci a*x+b struct FunLin { LL a, b ; FunLin(LL aa=0, LL bb=0) : a(aa), b(bb) {} LL eval(LL x) { return a*x+b ; } FunLin operator+(const FunLin &F) { return FunLin(a+F.a, b+F.b) ; } FunLin operator-(const FunLin &F) { return FunLin(a-F.a, b-F.b) ; } } ; ostream & operator<<(ostream &sss, const FunLin &X) { return sss << X.a << "x" << (X.b>=0 ? "+" : "") << X.b ; } // Klasa reprezentujaca ulamki (nieujemne) struct Rational { LL a, b ; Rational(LL aa=0, LL bb=1) : a(aa), b(bb) {} bool operator<(const Rational &X) const { return a*X.b < X.a*b ; } bool operator==(const Rational &X) const { return a*X.b == X.a*b ; } } ; ostream & operator<<(ostream &sss, const Rational &X) { return sss << X.a << "/" << X.b ; } //////////////////////////////////////// // klasa reprezentujaca jedno z dwoch zdarzen zdarzenie: // - zlaczenie dwoch przedzialow // - pytanie o aktualna wartosc struct Event { Rational time ; bool isJoin ; int data ; // indentyfikator przedzialu albo indeks zapytania Event(Rational tt, bool ii, int dd) : time(tt), isJoin(ii), data(dd) {} ; bool operator<(const Event &e) const { if(!(time==e.time)) return e.time < time ; else if(isJoin!=e.isJoin) return isJoin < e.isJoin ; else return data<e.data ; // whatever } } ; //////////////////////////////////////// const int MAXN=2e5+100 ; LL t[MAXN], prefT[MAXN] ; int d[MAXN] ; // klasa reprezentujaca scalane przedzialy struct Interval { int j, i ; Interval(int jj) : Interval(jj, jj) {} Interval(int jj, int ii) : j(jj), i(ii) {} FunLin getFun() const { return FunLin(triangle(i-j), (i-j)*t[j]-(prefT[i]-prefT[j])) ; } bool operator<(const Interval &interv) const { return i<interv.i ; } } ; Interval join(const Interval &A, const Interval &B) { assert(A.i+1 == B.j) ; return Interval(A.j, B.i) ; } ostream & operator<<(ostream &sss, const Interval &X) { return sss << "[" << X.j << "," << X.i << "]" ; } LL ret[MAXN] ; int main() { ios_base::sync_with_stdio(0) ; int n, m, i ; cin >> n >> m ; for(i=1 ; i<=n ; i++) { cin >> t[i] ; prefT[i] = prefT[i-1]+t[i] ; } set<Interval> intervals = { Interval(0) } ; priority_queue<Event> Q ; for(i=1 ; i<=n ; i++) { intervals.insert(Interval(i)) ; Q.push(Event(t[i]-t[i-1], true, i)) ; } for(i=1 ; i<=m ; i++) { cin >> d[i] ; Q.push(Event(d[i], false, i)) ; } FunLin F ; while(!Q.empty()) { Event e = Q.top() ; Q.pop() ; if(!e.isJoin) { ret[e.data] = F.eval(d[e.data]) ; } else { auto it = intervals.lower_bound(Interval(e.data)) ; assert(it!=intervals.end()) ; if(it->j != e.data) continue ; assert(it!=intervals.begin()) ; auto prev=it ; prev-- ; Interval newInterv = join(*prev, *it) ; F = F - (prev->getFun() + it->getFun()) + newInterv.getFun() ; intervals.erase(prev) ; intervals.erase(it) ; intervals.insert(newInterv) ; it = intervals.lower_bound(Interval(e.data)) ; auto next = it ; next++ ; if(next == intervals.end()) continue ; Q.push(Event(Rational(t[next->j]-t[it->j], (next->j)-(it->j)), true, next->j)) ; } } for(int i=1 ; i<=m ; i++) cout << ret[i] << endl ; } |