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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// pppbbb present's: 
#include <bits/stdc++.h>
//#define NETBEANS
#define REP(i,x) for(uint32 i = 0 ; i < (x) ; i++)

#define DEBUG_MODE
#ifdef DEBUG_MODE
#define print(x) cout << #x << " = " << x << endl
#define debug(x) x
#else
#define print(x)
#define debug(x)
#endif

#define hash_map      unordered_map
#define hash_multimap unordered_multimap
#define hash_set      unordered_set
#define hash_multiset unordered_multiset

using namespace std;

typedef short int int16;
typedef unsigned short int uint16;
typedef int int32;
typedef unsigned int uint32;
typedef long long int64;
typedef unsigned long long uint64;

typedef pair <int, int> PII;
typedef pair <uint32, uint32> PUU;
typedef pair <uint32, int> PUI;
typedef pair <int, uint32> PIU;
typedef pair <int64, int64> PLL;
typedef pair <int64, int> PLI;
typedef pair <int, int64> PIL;
typedef pair <int, int16> PIS;
typedef pair <int16, int> PSI;

//#define NETBEANS
#define gc getchar_unlocked
inline void FScanUint32(uint32& x)
{
#ifndef NETBEANS
    register uint32 c = gc();
    x = 0;
    for(;(c<48 || c>57);c = gc());
    for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
#else
    scanf("%u", &x);
#endif
    
}

inline void FScanUint64(uint64& x)
{
#ifndef NETBEANS
    register uint32 c = gc();
    x = 0;
    for(;(c<48 || c>57);c = gc());
    for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
#else
    scanf("%llu", &x);
#endif
}


 #define pc(x) putchar_unlocked(x);
    inline void FWriteUint64 (uint64& n)
    {
        #ifndef NETBEANS
        uint64 N = n, rev, count = 0;
        rev = N;
        if (N == 0) { pc('0'); pc('\n'); return ;}
        while ((rev % 10) == 0) { count++; rev /= 10;} //obtain the count of the number of 0s
        rev = 0;
        while (N != 0) { rev = (rev<<3) + (rev<<1) + N % 10; N /= 10;}  //store reverse of N in rev
        while (rev != 0) { pc(rev % 10 + '0'); rev /= 10;}
        while (count--) pc('0');
        #else
        printf("%llu", n);
    #endif
    }

struct PTT
{
    uint64 height;
    uint32 field_nr;
    uint64 day;
};

class Application
{
public:
    inline void Run();
private:
    // Methods
    inline void LoadData();
    inline void Solve();
    inline void PreprocessData();
    inline uint32 BinarySearch();
    inline bool Check(uint32 ind);
    inline uint32 UpperBoundCuts(uint32 ind);
    // Fields
    uint32 size_;
    uint32 query_number_;
    
    vector <uint32> grow_by_turn_;
    vector <uint64> grow_from_end_;
    vector <PTT> cuts_;
    
    // to not pass so many arguments
    uint64 cut_day_;
    uint64 cut_height_;
    PTT last_;
};

int main()
{
   // ios_base::sync_with_stdio(false);
    Application app;
    app.Run();
}

inline void Application::LoadData()
{
   // cin >> size_ >> query_number_;
    FScanUint32(size_);
    FScanUint32(query_number_);
    
    grow_by_turn_.reserve(size_);
    uint32 temp;
    
    REP(i, size_)
    {
        //cin >> temp;
        FScanUint32(temp);
        grow_by_turn_.push_back(temp);
    }
}

inline void Application::PreprocessData()
{
    sort(grow_by_turn_.begin(), grow_by_turn_.end());
    grow_from_end_.resize(size_ + 1);
    
    grow_from_end_.back() = 0ull;
    
    for(int i = size_ - 1; i >= 0; --i)
        grow_from_end_[i] = grow_from_end_[i + 1] + grow_by_turn_[i];
    
    
    last_.day = 0;
    last_.field_nr = size_;
    last_.height = 0;
    cuts_.push_back(last_);
}

inline uint32 Application::UpperBoundCuts(uint32 ind)
{
    uint32 begin = 0;
    uint32 end = cuts_.size();
    
    while(begin < end)
    {
        uint32 middle = (begin + end) / 2;
        
        if(cuts_[middle].field_nr > ind)
            end = middle;
        else
            begin = middle + 1;
    }
    
    return begin;
}

inline bool Application::Check(uint32 ind)
{
    uint32 last_cut = UpperBoundCuts(ind);
    
    if(last_cut == 0)
        return(cut_day_ * grow_by_turn_[ind] > cut_height_);
    else
        return( ( (cut_day_ - cuts_[last_cut - 1].day) * grow_by_turn_[ind]) + cuts_[last_cut - 1].height > cut_height_);
}

inline uint32 Application::BinarySearch()
{
    uint32 begin = size_ - 1;
    uint32 end = -1;
    
    while(begin - end > 1)
    {
        uint32 middle = (begin + end + 1) / 2;
        
        if(Check(middle))
            begin = middle;
        else
            end = middle;
    }
    
    return begin;
}

inline void Application::Solve()
{
    PreprocessData();

    PTT temp;
    REP(query, query_number_)
    {
        //cin >> cut_day_ >> cut_height_;
        FScanUint64(cut_day_);
        FScanUint64(cut_height_);
        if(!Check(size_ - 1))
        {
#ifdef NETBEANS
            puts("0");
#else
        pc('0');
        pc('\n');
#endif
            continue;
        }       
        
        uint32 cut_from = BinarySearch();
        
        uint32 place_in_cuts = UpperBoundCuts(cut_from);
        uint64 result;
      
        
        if(place_in_cuts > 0)
        {
            place_in_cuts -= 1;
            result = (
                     ((grow_from_end_[cut_from] - grow_from_end_[cuts_[place_in_cuts + 1].field_nr]) * (cut_day_ - cuts_[place_in_cuts].day)) + 
                     (cuts_[place_in_cuts].height * (cuts_[place_in_cuts + 1].field_nr - cut_from))
                     );
            place_in_cuts +=1;
        }
        else
        {
            result = (grow_from_end_[cut_from] - grow_from_end_[cuts_[place_in_cuts].field_nr]) * cut_day_;
        }
       
        for(uint32 i = place_in_cuts; i < cuts_.size() - 1; ++i)
        {
            result += ( 
                    ((grow_from_end_[cuts_[i].field_nr] - grow_from_end_[cuts_[i + 1].field_nr]) * (cut_day_ - cuts_[i].day)) +
                    (cuts_[i].height * (cuts_[i + 1].field_nr - cuts_[i].field_nr))
                    );
        }
        
        result -= ((size_ - cut_from) * cut_height_);
        
        //cout << result << "\n";
        
       FWriteUint64(result);
#ifdef NETBEANS
        puts("");
#else
        pc('\n');
#endif
        while(!cuts_.empty() && cuts_.back().field_nr >= cut_from)
            cuts_.pop_back();
        
        temp.day = cut_day_;
        temp.field_nr = cut_from;
        temp.height = cut_height_;
        
        cuts_.push_back(temp);
        cuts_.push_back(last_);
    }
}

inline void Application::Run()
{
    LoadData();
    Solve();
}