RMQ的基础题目,简单题。
1 /* 4122 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector> 10 #include <deque> 11 #include <algorithm> 12 #include <cstdio> 13 #include <cmath> 14 #include <ctime> 15 #include <cstring> 16 #include <climits> 17 #include <cctype> 18 #include <cassert> 19 #include <functional> 20 #include <iterator> 21 #include <iomanip> 22 using namespace std; 23 //#pragma comment(linker,"/STACK:102400000,1024000") 24 25 #define sti set<int> 26 #define stpii set<pair<int, int> > 27 #define mpii map<int,int> 28 #define vi vector<int> 29 #define pii pair<int,int> 30 #define vpii vector<pair<int,int> > 31 #define rep(i, a, n) for (int i=a;i<n;++i) 32 #define per(i, a, n) for (int i=n-1;i>=a;--i) 33 #define clr clear 34 #define pb push_back 35 #define mp make_pair 36 #define fir first 37 #define sec second 38 #define all(x) (x).begin(),(x).end() 39 #define SZ(x) ((int)(x).size()) 40 #define lson l, mid, rt<<1 41 #define rson mid+1, r, rt<<1|1 42 43 const int maxm = 1e5+5; 44 const int maxn = 2505; 45 int T[maxn], N[maxn]; 46 int cost[maxm], val[maxm]; 47 int dp[maxm][17]; 48 int days[12] = { 49 31, 28, 31, 30, 31, 30, 50 31, 31, 30, 31, 30, 31 51 }; 52 int days_[12] = { 53 31, 29, 31, 30, 31, 30, 54 31, 31, 30, 31, 30, 31 55 }; 56 char month[12][4] = { 57 "Jan", "Feb", "Mar", "Apr", 58 "May", "Jun", "Jul", "Aug", 59 "Sep", "Oct", "Nov", "Dec" 60 }; 61 char s[12]; 62 int yy, mm, dd, hh; 63 int n, m; 64 65 int getMon(char *s) { 66 rep(i, 0, 12) 67 if (strcmp(month[i], s) == 0) 68 return i; 69 70 return -1; 71 } 72 73 bool isLeapYear(int y) { 74 return y%400==0 || (y%100!=0 && y%4==0); 75 } 76 77 int getTime() { 78 mm = getMon(s); 79 int ret = 0; 80 81 rep(i, 2000, yy) { 82 if (isLeapYear(i)) 83 ret += 366; 84 else 85 ret += 365; 86 } 87 if (isLeapYear(yy)) { 88 rep(i, 0, mm) 89 ret += days_[i]; 90 } else { 91 rep(i, 0, mm) 92 ret += days[i]; 93 } 94 ret += dd-1; 95 ret *= 24; 96 ret += hh + 1; 97 98 return ret; 99 } 100 101 void init_RMQ() { 102 int i, j; 103 104 for (i=0; i<m; ++i) 105 dp[i][0] = i; 106 107 for (j=1; (1<<j)<=m; ++j) { 108 for (i=0; i+(1<<j)-1<m; ++i) { 109 if (val[dp[i][j-1]] < val[dp[i+(1<<(j-1))][j-1]]) 110 dp[i][j] = dp[i][j-1]; 111 else 112 dp[i][j] = dp[i+(1<<(j-1))][j-1]; 113 } 114 } 115 } 116 117 int RMQ(int l, int r) { 118 if (l < 0) 119 l = 0; 120 121 int k = 0; 122 123 while (1<<(k+1) <= r-l+1) 124 ++k; 125 126 if (val[dp[l][k]] < val[dp[r-(1<<k)+1][k]]) 127 return dp[l][k]; 128 else 129 return dp[r-(1<<k)+1][k]; 130 } 131 132 int main() { 133 ios::sync_with_stdio(false); 134 #ifndef ONLINE_JUDGE 135 freopen("data.in", "r", stdin); 136 freopen("data.out", "w", stdout); 137 #endif 138 139 int st, cst; 140 int idx, tmp; 141 __int64 ans; 142 143 while (scanf("%d%d", &n, &m)!=EOF && (n||m)) { 144 rep(i, 0, n) { 145 scanf("%s%d%d%d%d", s, &dd, &yy, &hh, &N[i]); 146 T[i] = getTime(); 147 } 148 scanf("%d%d", &st, &cst); 149 rep(i, 0, m) { 150 scanf("%d", &cost[i]); 151 val[i] = cost[i] + cst * (m - i); 152 } 153 154 ans = 0; 155 init_RMQ(); 156 rep(i, 0, n) { 157 idx = RMQ(T[i]-st, T[i]-1); 158 tmp = cost[idx] + (T[i]-1-idx) * cst; 159 ans += 1LL * tmp * N[i]; 160 } 161 162 printf("%I64d\n", ans); 163 } 164 165 #ifndef ONLINE_JUDGE 166 printf("time = %d.\n", (int)clock()); 167 #endif 168 169 return 0; 170 }