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
| #include <cstdio> #include <random> #include <unordered_map> #include <queue> #include <algorithm>
using namespace std;
class card { public: long long t; bool winner; int id; unsigned long long hasher; friend bool operator<(const card &x, const card &y) { if(x.winner && !y.winner) return false; if(!x.winner && y.winner) return true; return x.t > y.t || x.t == y.t && x.id > y.id; } } cards[200005];
unsigned long long bas = 1000000007, rbas = 13499267949257065399ull;
long long ts[10000005]; long long gt[10000005]; unsigned long long hqk[200005];
int main() { int n, h; scanf("%d%d", &n, &h); mt19937_64 mt(random_device{}()); hqk[0] = 1; for(int i=1;i<=n;i++) { hqk[i] = hqk[i-1] * bas; scanf("%lld", &cards[i].t); cards[i].hasher = mt(); cards[i].id = i; } int k; scanf("%d", &k); while(k--) { int s; scanf("%d", &s); cards[s].winner = true; } unordered_map<unsigned long long, int> um; priority_queue<card> gss; queue<card> qp; unsigned long long alpha = 0, beta = 0; for(int i=1;i<=h;i++) { gss.push(cards[i]); alpha ^= cards[i].hasher; } for(int i=h+1;i<=n;i++) { qp.push(cards[i]); beta += hqk[cards[i].id - h - 1] * cards[i].hasher; } int rotl = 0, rotr = 0; int cur = 0; int sum = 0; long long T = 0; while(++cur) { if(um[alpha ^ beta]) { rotl = um[alpha ^ beta]; rotr = cur - 1; break; } um[alpha ^ beta] = cur; auto fst = gss.top(), scd = qp.front(); T += fst.t; if(fst.winner) sum++; ts[cur] = T; gt[cur] = sum; alpha ^= fst.hasher; alpha ^= scd.hasher; beta -= scd.hasher; beta *= rbas; beta += fst.hasher * hqk[n - h - 1]; gss.pop(); qp.pop(); qp.push(fst); gss.push(scd); } int q; scanf("%d", &q); while(q--) { long long t; scanf("%lld", &t); if(t < ts[rotl - 1]) { printf("%lld\n", gt[upper_bound(ts + 1, ts + rotl + 1, t) - ts - 1]); continue; } else { printf("%lld\n", gt[rotl - 1] + (t - ts[rotl]) / (ts[rotr] - ts[rotl - 1]) * (gt[rotr] - gt[rotl - 1]) + gt[upper_bound(ts + rotl, ts + rotr + 1, (t - ts[rotl]) % (ts[rotr] - ts[rotl - 1]) + ts[rotl]) - ts - 1] - gt[rotl - 1]); } } return 0; }
|