#include <algorithm> #include <iostream> #include <queue> using namespace std; #define int long long struct Piekarnik { int id, czas; int res; }; bool sortPiekarnikiById( const Piekarnik &a, const Piekarnik &b ) { return a.id < b.id; } bool sortPiekarnikiByCzas( const Piekarnik &a, const Piekarnik &b ) { return a.czas < b.czas; } struct Klient { int t, multiplier, deleted; }; struct Fala { int minToNext, startowy; Fala( int minToNext, int startowy ) : minToNext( minToNext ), startowy( startowy ) {} }; bool operator<( Fala a, Fala b ) { if ( a.minToNext != b.minToNext ) return a.minToNext > b.minToNext; return a.startowy > b.startowy; } const bool debug = false; Klient tab[200009]; Piekarnik piekarniki[200009]; int getDouble( int a ) { return a * ( a - 1 ) / 2; } int getSquare( int start, int step ) { int res = getDouble( start + step ) - getDouble( start ) - getDouble( step ); if ( debug ) cout << "getSquare " << start << " " << step << "=" << res << endl; return res; } main() { ios_base::sync_with_stdio( 0 ); int ileKlientow, ilePiekarnikow; cin >> ileKlientow >> ilePiekarnikow; priority_queue<Fala> q; tab[0].t = 0; tab[0].multiplier = 1; tab[0].deleted = 0; for ( int i = 1; i <= ileKlientow; i++ ) { cin >> tab[i].t; tab[i].multiplier = 1; tab[i].deleted = 0; q.push( Fala( tab[i].t - tab[i - 1].t, i - 1 ) ); } for ( int i = 0; i < ilePiekarnikow; i++ ) { cin >> piekarniki[i].czas; piekarniki[i].id = i; } sort( piekarniki, piekarniki + ilePiekarnikow, sortPiekarnikiByCzas ); int sumOfMultipliers = 0; int toSubtract = 0; for ( int i = 0; i < ilePiekarnikow; i++ ) { while ( !q.empty() && q.top().minToNext <= piekarniki[i].czas ) { Fala f = q.top(); q.pop(); int leftId = f.startowy; if ( debug ) cout << "startowy= " << f.startowy << " minToNext=" << f.minToNext << " multiplier=" << tab[leftId].multiplier << endl; if ( tab[leftId].deleted ) continue; int rightId = f.startowy + tab[f.startowy].multiplier; if ( debug ) cout << "rightId=" << rightId << endl; toSubtract += ( tab[rightId].t - tab[leftId].t ) * tab[rightId].multiplier; sumOfMultipliers += getSquare( tab[leftId].multiplier, tab[rightId].multiplier ); tab[leftId].multiplier += tab[rightId].multiplier; tab[rightId].deleted = true; rightId += tab[rightId].multiplier; if ( rightId <= ileKlientow ) { q.push( Fala( ( tab[rightId].t - tab[leftId].t - 1 ) / tab[leftId].multiplier + 1, leftId ) ); } } if ( debug ) cout << "Piekarnik time=" << piekarniki[i].czas << " sumOfMultipliers=" << sumOfMultipliers << " toSubtract=" << toSubtract << endl; piekarniki[i].res = sumOfMultipliers * piekarniki[i].czas - toSubtract; } for ( int i = 0; i <= ileKlientow; i++ ) { if ( debug ) cout << tab[i].deleted << " "; } if ( debug ) cout << endl; sort( piekarniki, piekarniki + ilePiekarnikow, sortPiekarnikiById ); for ( int i = 0; i < ilePiekarnikow; i++ ) { cout <<piekarniki[i].res << 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 | #include <algorithm> #include <iostream> #include <queue> using namespace std; #define int long long struct Piekarnik { int id, czas; int res; }; bool sortPiekarnikiById( const Piekarnik &a, const Piekarnik &b ) { return a.id < b.id; } bool sortPiekarnikiByCzas( const Piekarnik &a, const Piekarnik &b ) { return a.czas < b.czas; } struct Klient { int t, multiplier, deleted; }; struct Fala { int minToNext, startowy; Fala( int minToNext, int startowy ) : minToNext( minToNext ), startowy( startowy ) {} }; bool operator<( Fala a, Fala b ) { if ( a.minToNext != b.minToNext ) return a.minToNext > b.minToNext; return a.startowy > b.startowy; } const bool debug = false; Klient tab[200009]; Piekarnik piekarniki[200009]; int getDouble( int a ) { return a * ( a - 1 ) / 2; } int getSquare( int start, int step ) { int res = getDouble( start + step ) - getDouble( start ) - getDouble( step ); if ( debug ) cout << "getSquare " << start << " " << step << "=" << res << endl; return res; } main() { ios_base::sync_with_stdio( 0 ); int ileKlientow, ilePiekarnikow; cin >> ileKlientow >> ilePiekarnikow; priority_queue<Fala> q; tab[0].t = 0; tab[0].multiplier = 1; tab[0].deleted = 0; for ( int i = 1; i <= ileKlientow; i++ ) { cin >> tab[i].t; tab[i].multiplier = 1; tab[i].deleted = 0; q.push( Fala( tab[i].t - tab[i - 1].t, i - 1 ) ); } for ( int i = 0; i < ilePiekarnikow; i++ ) { cin >> piekarniki[i].czas; piekarniki[i].id = i; } sort( piekarniki, piekarniki + ilePiekarnikow, sortPiekarnikiByCzas ); int sumOfMultipliers = 0; int toSubtract = 0; for ( int i = 0; i < ilePiekarnikow; i++ ) { while ( !q.empty() && q.top().minToNext <= piekarniki[i].czas ) { Fala f = q.top(); q.pop(); int leftId = f.startowy; if ( debug ) cout << "startowy= " << f.startowy << " minToNext=" << f.minToNext << " multiplier=" << tab[leftId].multiplier << endl; if ( tab[leftId].deleted ) continue; int rightId = f.startowy + tab[f.startowy].multiplier; if ( debug ) cout << "rightId=" << rightId << endl; toSubtract += ( tab[rightId].t - tab[leftId].t ) * tab[rightId].multiplier; sumOfMultipliers += getSquare( tab[leftId].multiplier, tab[rightId].multiplier ); tab[leftId].multiplier += tab[rightId].multiplier; tab[rightId].deleted = true; rightId += tab[rightId].multiplier; if ( rightId <= ileKlientow ) { q.push( Fala( ( tab[rightId].t - tab[leftId].t - 1 ) / tab[leftId].multiplier + 1, leftId ) ); } } if ( debug ) cout << "Piekarnik time=" << piekarniki[i].czas << " sumOfMultipliers=" << sumOfMultipliers << " toSubtract=" << toSubtract << endl; piekarniki[i].res = sumOfMultipliers * piekarniki[i].czas - toSubtract; } for ( int i = 0; i <= ileKlientow; i++ ) { if ( debug ) cout << tab[i].deleted << " "; } if ( debug ) cout << endl; sort( piekarniki, piekarniki + ilePiekarnikow, sortPiekarnikiById ); for ( int i = 0; i < ilePiekarnikow; i++ ) { cout <<piekarniki[i].res << endl; } } |