Sie sind auf Seite 1von 4

#include <iostream>

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <functional>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <list>
#include <bitset>
#include <utility>
#include <cassert>
#include <iomanip>
#include <ctime>
#include <fstream>

using namespace std;

const int me = 10025;


const int offset = 100000000;

void brute(){
int N, R;
map<pair<int, int>, int> m;
cin >> N >> R;
for(int i = 0; i < N; i ++){
int x[2], y[2];
cin >> x[0] >> y[0];
cin >> x[1] >> y[1];
for(int i = x[0]; i < x[1]; i ++)
for(int j = y[0]; j < y[1]; j ++)
m[make_pair(i, j)] ++;
}
cout << (int)m.size() << endl;
}

struct node{
long long sum;
int lazy;
node *lower_left;
node *lower_right;
node *upper_left;
node *upper_right;
node(){

}
};
typedef node* pnode;

long long get(pnode root){


return root ? root->sum : 0;
}
pnode initialize(long long value){
pnode root = pnode(malloc(sizeof(node)));
root->sum = value;
root->lazy = 0;
root->lower_left = NULL;
root->lower_right = NULL;
root->upper_left = NULL;
root->upper_right = NULL;
return root;
}
void push(pnode &root, int x, int y, int X, int Y){
if(!root)
return;
if(root->lazy){
root->sum = 1LL * (X - x + 1) * (Y - y + 1);
if(x == X && y == Y)
return;
if(x == X || y == Y){
root->lower_left->lazy = 1;
root->upper_left->lazy = 1;
}
else{
root->lower_left->lazy = 1;
root->lower_right->lazy = 1;
root->upper_left->lazy = 1;
root->upper_right->lazy = 1;
}
root->lazy = 0;
}
}
void update(pnode &root, int x, int y, int X, int Y, int rx, int ry, int RX, int
RY){
if(!root){
root = initialize(0);
root->lower_left = initialize(0);
root->lower_right = initialize(0);
root->upper_left = initialize(0);
root->upper_right = initialize(0);
}
push(root, x, y, X, Y);
if(x > RX || X < rx || y > RY || Y < ry)
return;
cout << "x = " << x << ", y = " << y << ", X = " << X << ", Y = " << Y << endl;
if(x >= rx && X <= RX && y >= ry && Y <= RY){
root->lazy = 1;
push(root, x, y, X, Y);
return;
}
int middle_x = (x + X) >> 1;
int middle_y = (y + Y) >> 1;
if(x == X){
update(root->lower_left, x, y, X, middle_y, rx, ry, RX, RY);
update(root->upper_left, x, middle_y + 1, X, Y, rx, ry, RX, RY);
}
else if(y == Y){
update(root->lower_left, x, y, middle_x, Y, rx, ry, RX, RY);
update(root->upper_left, middle_x + 1, y, X, Y, rx, ry, RX, RY);
}
else{
update(root->lower_left, x, y, middle_x, middle_y, rx, ry, RX, RY);
update(root->lower_right, x, middle_y + 1, middle_x, Y, rx, ry, RX, RY);
update(root->upper_left, middle_x + 1, y, X, middle_y, rx, ry, RX, RY);
update(root->upper_right, middle_x + 1, middle_y + 1, X, Y, rx, ry, RX,
RY);
}
root->sum = get(root->lower_left) + get(root->lower_right)
+ get(root->upper_left) + get(root->upper_right);
}
long long get(pnode &root, int x, int y, int X, int Y, int rx, int ry, int RX, int
RY){
if(x > RX || X < rx || y > RY || Y < ry)
return 0;
if(!root)
return 0;
push(root, x, y, X, Y);
if(x >= rx && X <= RX && y >= ry && Y <= RY)
return root->sum;
int middle_x = (x + X) >> 1;
int middle_y = (y + Y) >> 1;
if(x == X){
return get(root->lower_left, x, y, X, middle_y, rx, ry, RX, RY)
+ get(root->upper_left, x, middle_y + 1, X, Y, rx, ry, RX, RY);
}
else if(y == Y){
return get(root->lower_left, x, y, middle_x, Y, rx, ry, RX, RY)
+ get(root->upper_left, middle_x + 1, y, X, Y, rx, ry, RX, RY);
}
else{
return get(root->lower_left, x, y, middle_x, middle_y, rx, ry, RX, RY)
+ get(root->lower_right, x, middle_y + 1, middle_x, Y, rx, ry, RX, RY)
+ get(root->upper_left, middle_x + 1, y, X, middle_y, rx, ry, RX, RY)
+ get(root->upper_right, middle_x + 1, middle_y + 1, X, Y, rx, ry, RX, RY);
}
}

int N, R;
int x[2], y[2];
pnode root = NULL;

int main(int argc, const char * argv[]) {


ios_base::sync_with_stdio(0);
cin.tie(0);

cin >> N >> R;


for(int i = 0; i < N; i ++){
cin >> x[0] >> y[0] >> x[1] >> y[1];
x[1] --;
y[1] --;
x[0] += offset;
y[0] += offset;
x[1] += offset;
y[1] += offset;
cout << "sent as " << "x = " << x[0] << ", y = " << y[0] << ", X = " <<
x[1] << ", Y = " << y[1] << endl;
update(root, 1, 1, offset << 1, offset << 1, x[0], y[0], x[1], y[1]);
}
cout << 1LL * R * root->sum << endl;

return 0;
}

Das könnte Ihnen auch gefallen