Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define MAX 131072
- using namespace std;
- const int NMAX = 30010;
- FILE *IN;
- int ans;
- int N, M, F, Q;
- int DX, DY;
- struct per{
- int x, top, bottom;
- int index, cost;
- }query[2 * NMAX];
- struct tree{
- int sum, inc, dif;
- }T[6 * NMAX];
- int pos, sign;
- char f[MAX];
- inline void Read(int &nr){
- sign = 0;
- nr = 0;
- while(f[pos] < '0' || f[pos] > '9'){
- if(f[pos] == '-') sign = 1;
- pos++;
- if(pos == MAX)
- fread(f, MAX, 1, IN), pos = 0;
- }
- while(f[pos] >= '0' && f[pos] <= '9'){
- nr = 10 * nr + f[pos++] - '0';
- if(pos == MAX)
- fread(f, MAX, 1, IN), pos = 0;
- }
- if(sign) nr =- nr;
- }
- bool cmp(per a, per b){
- return a.x < b.x;
- }
- inline void read(){
- Read(N); Read(M); Read(F);
- Read(DX); Read(DY);
- int Cost;
- per X1, X2;
- for(int i = 1; i <= F; i++){
- Read(X1.x); Read(X1.bottom);
- Read(X2.x); Read(X2.top);
- Read(Cost);
- query[++Q].x = X1.x; query[Q].top = X1.bottom + 1; query[Q].bottom = X2.top + DY - 2;
- query[Q].cost = Cost; query[Q].index = i;
- query[++Q].x = X2.x + DX - 2; query[Q].top = X1.bottom; query[Q].bottom = X2.top + DY - 2;
- if(query[Q].top < query[Q - 1].top) query[Q].top = query[Q - 1].top;
- query[Q].cost = -Cost; query[Q].index = i;
- }
- sort(query + 1, query + Q + 1, cmp);
- }
- inline void Build(int node, int st, int nd){
- if(st == nd)
- T[node].sum = 2e9;
- else{
- int mid = (st + nd) / 2;
- Build(2 * node, st, mid);
- Build(2 * node + 1, mid + 1, nd);
- T[node].sum = 2e9;
- }
- }
- inline void Query(int node, int st, int nd, int A, int B){
- if(A <= st && nd <= B){
- ans = min(ans, T[node].sum);
- } else {
- int mid = (st + nd) / 2, ok = 0;
- int x = T[node].inc + T[node].dif;
- if(mid >= A) {
- T[2 * node].sum += x;
- Query(2 * node, st, mid, A, B);
- T[2 * node].sum -= x;
- }
- if(mid + 1 <= B) {
- T[2 * node + 1].sum += x;
- Query(2 * node + 1, mid + 1, nd, A, B);
- T[2 * node + 1].sum -= x;
- }
- }
- }
- inline void Update(int node, int st, int nd, int A, int B, int val){
- if(A <= st && nd <= B){
- //if(T[node].sum == 2e9) T[node].sum = 0;
- if(val < 0) T[node].dif += val;
- if(val > 0) T[node].inc += val;
- T[node].sum = T[node].inc + T[node].dif;
- } else {
- int mid = (st + nd) / 2, ok = 0;
- if(mid >= A)
- Update(2 * node, st, mid, A, B, val);
- if(mid + 1 <= B)
- Update(2 * node + 1, mid + 1, nd, A, B, val);
- int x = T[node].inc + T[node].dif;
- T[node].sum = min(T[2 * node].sum + x, T[2 * node + 1].sum + x);
- }
- }
- int main(){
- IN = fopen("demolish.in", "r");
- freopen("demolish.out", "w", stdout);
- read();
- Build(1, 1, M + 1);
- ans = 2e9;
- for(int i = 1; i <= Q; i++){
- if(query[i].x >= DX - 1)
- Query(1, 1, M + 1, DY - 1, M + 1);
- int j = i;
- while(query[j].x == query[i].x && j < Q){
- int y = query[j].bottom;
- int x = query[j].top;
- if(y > M) y = M;
- if(x > N) x = N;
- Update(1, 1, M + 1, x + 1, y + 1, query[j].cost);
- j++;
- }
- if(i != j) i = j - 1;
- }
- printf("%d", ans);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement