Submission #330573
Source Code Expand
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cstdio>
using namespace std;
#define REP(i,n) for((i)=0;(i)<(int)(n);(i)++)
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
const int kM = 1002, kQ = 1002;
typedef long long ll;
typedef pair<ll,ll> P;
ll N, M;
ll A[kM], B[kM], C[kM];
ll Q;
ll D[kQ], E[kQ], F[kQ];
struct Rect {
ll lx, ly, width, height;
ll value;
bool operator<(const Rect& other) const {
return value < other.value;
}
};
ll rect_lattice_point(const Rect& r, const Rect& ref) {
ll temp = 0;
ll edge_lesser = min(ref.width, ref.height);
ll edge_greater = max(ref.width, ref.height);
if (r.value < edge_lesser) {
temp = (r.value + 1) * (r.value + 2) / 2;
}
else if (r.value < edge_greater) {
ll delta = r.value - edge_lesser;
temp += delta * (edge_lesser + 1);
temp += (edge_lesser + 1) * (edge_lesser + 2) / 2;
if (r.value >= ref.width) {
temp -= 1 + r.value - ref.width;
}
}
else {
temp = (ref.width + 1) * (ref.height + 1);
ll delta = r.value - edge_greater;
temp -= (edge_lesser - delta) * (edge_lesser - delta + 1) / 2;
if (r.value >= ref.width) {
temp -= 1 + r.value - ref.width;
}
}
return temp;
}
ll square_lattice_point(const Rect& r, const Rect& ref) {
ll temp = 0;
if (r.value < ref.width) {
temp = (r.value + 1) * (r.value + 2) / 2;
}
else {
if (ref.width == 1) {
temp = 2;
}
else {
temp = (ref.width + 1) * (ref.width + 2) / 2; // 斜線以外全部
}
temp += ref.width + 1; // 斜線
ll delta = r.value - ref.width;
temp -= (ref.width - delta) * (ref.width + 1 - delta) / 2;
temp -= 1 + (r.value - ref.width); // 右端
}
return temp;
}
void solve() {
vector<ll> verts, horis;
for (int i = 0; i < M; i++) {
int a = A[i], b = B[i], c = C[i];
int vert_index = a + (c - 1),
hori_index = b;
verts.push_back(vert_index);
horis.push_back(hori_index);
}
for (int i = 0; i < Q; i++) {
int d = D[i], e = E[i], f = F[i];
int vert_index = d + (f - 1),
hori_index = e;
verts.push_back(vert_index);
horis.push_back(hori_index);
}
verts.push_back(0);
verts.push_back(1);
verts.push_back(N + 2);
horis.push_back(0);
horis.push_back(1);
horis.push_back(N + 2);
sort(verts.begin(), verts.end());
sort(horis.begin(), horis.end());
verts.erase(unique(verts.begin(), verts.end()), verts.end());
horis.erase(unique(horis.begin(), horis.end()), horis.end());
const int kV = verts.size(), kH = horis.size();
map<P, Rect> rects;
// start as 1
for (int i = 1; i < kV-1; i++) {
int height = verts[i] - verts[i-1];
for (int j = 0; j < kH-1; j++) {
int width = horis[j+1] - horis[j];
rects[P(verts[i], horis[j])] = (Rect){-1, -1, width, height, -1};
}
}
// foreach(rects, i) {
// printf("lx,ly,w,h=%lld,%lld,%lld,%lld\n",
// i->first.first, i->first.second,
// i->second.width, i->second.height);
// }
priority_queue<Rect> PQ;
// 1. set init x-size
for (int i = 0; i < M; i++) {
int v_idx = A[i] + (C[i] - 1);
int h_idx = B[i];
PQ.push((Rect){v_idx, h_idx, -1, -1, C[i] - 1});
}
while (!PQ.empty()) {
Rect r = PQ.top(); PQ.pop();
Rect& ref = rects[P(r.lx, r.ly)];
if (r.value > ref.value) {
ref.value = r.value;
// printf("write as lx,ly,w,h,x=%lld,%lld,%lld,%lld,%lld\n",
// r.lx, r.ly, r.value, ref.width, ref.height);
} else {
break;
}
if (ref.value >= ref.width) {
PQ.push((Rect){r.lx, *upper_bound(horis.cbegin(), horis.cend(), r.ly),
-1, -1, ref.value - ref.width});
// printf("push(right) lx,ly,x=%lld,%lld,%lld\n", r.lx,
// *upper_bound(horis.cbegin(), horis.cend(), r.ly) , ref.value - ref.width);
}
if (ref.value > ref.height) {
vector<ll>::iterator ptr = lower_bound(verts.begin(), verts.end(), r.lx);
if (*ptr != verts.front()) {
--ptr;
PQ.push((Rect){*ptr, r.ly, -1, -1, ref.value - ref.height});
//printf("push(up) lx,ly,x=%lld,%lld,%lld\n", *ptr, r.ly, ref.value - ref.height);
}
}
}
// 2. couting
queue<Rect> wq;
for (int i = 0; i < Q; i++) {
int v_idx = D[i] + (F[i] - 1);
int h_idx = E[i];
ll ans = 0;
wq.push((Rect){v_idx, h_idx, -1, -1, F[i] - 1});
while (!wq.empty()) {
const Rect& r = wq.front(); wq.pop();
const Rect& ref = rects[P(r.lx, r.ly)];
const int x_idx =
lower_bound(verts.cbegin(), verts.cend(), r.lx) - verts.cbegin();
// 重複部分の計算
if (r.value > ref.value) {
// printf("[]--- cur x,y,w,h,new,old=%lld,%lld,%lld,%lld,%lld,%lld\n",
// r.lx, r.ly, ref.width, ref.height, r.value, ref.value);
ll temp = 0;
// 正方形
if (ref.width == ref.height) {
temp = square_lattice_point(r, ref);
// printf("(square)by new value=%lld\n", temp);
if (ref.value != -1) {
// printf("subtract value=%lld\n", rect_lattice_point(ref, ref));
temp -= rect_lattice_point(ref, ref);
}
}
// 長方形
else {
temp = rect_lattice_point(r, ref);
// printf("(rect)by new value=%lld\n", temp);
if (ref.value != -1) {
// printf("subtract value=%lld\n", rect_lattice_point(ref, ref));
temp -= rect_lattice_point(ref, ref);
}
}
if (x_idx != 1) {
if (r.value > ref.height) {
temp -= r.value - ref.height; // not necessaty plus one
}
else if (r.value == ref.height) {
temp -= 1;
}
}
// printf("lx,ly,w,h,new_x,temp=%lld,%lld,%lld,%lld,%lld,%lld\n",
// r.lx, r.ly, ref.width, ref.height, r.value, temp);
ans += temp;
} else {
continue;
}
if (r.value >= ref.width) {
auto itr = upper_bound(horis.cbegin(), horis.cend(), r.ly);
ll delta = r.value - ref.width;
// printf("push(right,counting) lx,ly,delta=%lld,%lld,%lld\n",
// r.lx, *itr, delta);
wq.push((Rect){r.lx, *itr, -1, -1, delta});
}
if (x_idx != 1 && r.value >= ref.height) {
auto itr = lower_bound(verts.cbegin(), verts.cend(), r.lx);
if (*itr != verts.front()) {
--itr;
ll delta = r.value - ref.height;
// printf("push(up,counting) lx,ly,delta=%lld,%lld,%lld\n",
// *itr, r.ly, delta);
wq.push((Rect){*itr, r.ly, -1, -1, delta});
}
}
}
printf("%lld\n", ans);
}
}
int main() {
while (cin >> N >> M) {
for (int i = 0; i < M; i++) {
cin >> A[i] >> B[i] >> C[i];
}
cin >> Q;
for (int i = 0; i < Q; i++) {
cin >> D[i] >> E[i] >> F[i];
}
solve();
}
return 0;
}
Submission Info
Submission Time |
|
Task |
E - 電波局 |
User |
brly |
Language |
C++11 (GCC 4.8.1) |
Score |
0 |
Code Size |
7451 Byte |
Status |
WA |
Exec Time |
2085 ms |
Memory |
413508 KB |
Judge Result
Set Name |
Sample |
Subtask1 |
Subtask2 |
Subtask3 |
Score / Max Score |
0 / 0 |
0 / 10 |
0 / 30 |
0 / 60 |
Status |
|
AC |
× 2 |
WA |
× 1 |
TLE |
× 16 |
RE |
× 3 |
|
AC |
× 2 |
WA |
× 1 |
TLE |
× 36 |
RE |
× 3 |
|
AC |
× 2 |
WA |
× 1 |
TLE |
× 57 |
RE |
× 4 |
|
Set Name |
Test Cases |
Sample |
subtask0-sample-01.txt, subtask0-sample-02.txt |
Subtask1 |
subtask0-sample-01.txt, subtask0-sample-02.txt, subtask1-01.txt, subtask1-02.txt, subtask1-03.txt, subtask1-04.txt, subtask1-05.txt, subtask1-06.txt, subtask1-07.txt, subtask1-08.txt, subtask1-09.txt, subtask1-10.txt, subtask1-11.txt, subtask1-12.txt, subtask1-13.txt, subtask1-14.txt, subtask1-15.txt, subtask1-16.txt, subtask1-17.txt, subtask1-18.txt, subtask1-19.txt, subtask1-20.txt |
Subtask2 |
subtask0-sample-01.txt, subtask0-sample-02.txt, subtask1-01.txt, subtask1-02.txt, subtask1-03.txt, subtask1-04.txt, subtask1-05.txt, subtask1-06.txt, subtask1-07.txt, subtask1-08.txt, subtask1-09.txt, subtask1-10.txt, subtask1-11.txt, subtask1-12.txt, subtask1-13.txt, subtask1-14.txt, subtask1-15.txt, subtask1-16.txt, subtask1-17.txt, subtask1-18.txt, subtask1-19.txt, subtask1-20.txt, subtask2-01.txt, subtask2-02.txt, subtask2-03.txt, subtask2-04.txt, subtask2-05.txt, subtask2-06.txt, subtask2-07.txt, subtask2-08.txt, subtask2-09.txt, subtask2-10.txt, subtask2-11.txt, subtask2-12.txt, subtask2-13.txt, subtask2-14.txt, subtask2-15.txt, subtask2-16.txt, subtask2-17.txt, subtask2-18.txt, subtask2-19.txt, subtask2-20.txt |
Subtask3 |
subtask0-sample-01.txt, subtask0-sample-02.txt, subtask1-01.txt, subtask1-02.txt, subtask1-03.txt, subtask1-04.txt, subtask1-05.txt, subtask1-06.txt, subtask1-07.txt, subtask1-08.txt, subtask1-09.txt, subtask1-10.txt, subtask1-11.txt, subtask1-12.txt, subtask1-13.txt, subtask1-14.txt, subtask1-15.txt, subtask1-16.txt, subtask1-17.txt, subtask1-18.txt, subtask1-19.txt, subtask1-20.txt, subtask2-01.txt, subtask2-02.txt, subtask2-03.txt, subtask2-04.txt, subtask2-05.txt, subtask2-06.txt, subtask2-07.txt, subtask2-08.txt, subtask2-09.txt, subtask2-10.txt, subtask2-11.txt, subtask2-12.txt, subtask2-13.txt, subtask2-14.txt, subtask2-15.txt, subtask2-16.txt, subtask2-17.txt, subtask2-18.txt, subtask2-19.txt, subtask2-20.txt, subtask3-01.txt, subtask3-02.txt, subtask3-03.txt, subtask3-04.txt, subtask3-05.txt, subtask3-06.txt, subtask3-07.txt, subtask3-08.txt, subtask3-09.txt, subtask3-10.txt, subtask3-11.txt, subtask3-12.txt, subtask3-13.txt, subtask3-14.txt, subtask3-15.txt, subtask3-16.txt, subtask3-17.txt, subtask3-18.txt, subtask3-19.txt, subtask3-20.txt, subtask3-21.txt, subtask3-22.txt |
Case Name |
Status |
Exec Time |
Memory |
subtask0-sample-01.txt |
AC |
25 ms |
920 KB |
subtask0-sample-02.txt |
AC |
25 ms |
932 KB |
subtask1-01.txt |
WA |
29 ms |
928 KB |
subtask1-02.txt |
RE |
320 ms |
6264 KB |
subtask1-03.txt |
TLE |
2068 ms |
315860 KB |
subtask1-04.txt |
TLE |
2064 ms |
302692 KB |
subtask1-05.txt |
TLE |
2068 ms |
320884 KB |
subtask1-06.txt |
TLE |
2070 ms |
314340 KB |
subtask1-07.txt |
RE |
869 ms |
44768 KB |
subtask1-08.txt |
TLE |
2078 ms |
401608 KB |
subtask1-09.txt |
TLE |
2068 ms |
353760 KB |
subtask1-10.txt |
TLE |
2069 ms |
341144 KB |
subtask1-11.txt |
TLE |
2077 ms |
405076 KB |
subtask1-12.txt |
TLE |
2074 ms |
340704 KB |
subtask1-13.txt |
TLE |
2074 ms |
401120 KB |
subtask1-14.txt |
TLE |
2081 ms |
405196 KB |
subtask1-15.txt |
TLE |
2076 ms |
358368 KB |
subtask1-16.txt |
TLE |
2080 ms |
397828 KB |
subtask1-17.txt |
TLE |
2078 ms |
315292 KB |
subtask1-18.txt |
TLE |
2078 ms |
413508 KB |
subtask1-19.txt |
RE |
288 ms |
3364 KB |
subtask1-20.txt |
TLE |
2085 ms |
401176 KB |
subtask2-01.txt |
TLE |
2062 ms |
227864 KB |
subtask2-02.txt |
TLE |
2061 ms |
223848 KB |
subtask2-03.txt |
TLE |
2060 ms |
242512 KB |
subtask2-04.txt |
TLE |
2055 ms |
224608 KB |
subtask2-05.txt |
TLE |
2058 ms |
233820 KB |
subtask2-06.txt |
TLE |
2059 ms |
242268 KB |
subtask2-07.txt |
TLE |
2061 ms |
249184 KB |
subtask2-08.txt |
TLE |
2063 ms |
260900 KB |
subtask2-09.txt |
TLE |
2065 ms |
300636 KB |
subtask2-10.txt |
TLE |
2062 ms |
278624 KB |
subtask2-11.txt |
TLE |
2065 ms |
287792 KB |
subtask2-12.txt |
TLE |
2060 ms |
254176 KB |
subtask2-13.txt |
TLE |
2056 ms |
205616 KB |
subtask2-14.txt |
TLE |
2067 ms |
237364 KB |
subtask2-15.txt |
TLE |
2060 ms |
215332 KB |
subtask2-16.txt |
TLE |
2059 ms |
234164 KB |
subtask2-17.txt |
TLE |
2062 ms |
230108 KB |
subtask2-18.txt |
TLE |
2060 ms |
242276 KB |
subtask2-19.txt |
TLE |
2046 ms |
108620 KB |
subtask2-20.txt |
TLE |
2065 ms |
282844 KB |
subtask3-01.txt |
TLE |
2058 ms |
202164 KB |
subtask3-02.txt |
TLE |
2058 ms |
219172 KB |
subtask3-03.txt |
TLE |
2067 ms |
280028 KB |
subtask3-04.txt |
TLE |
2058 ms |
226776 KB |
subtask3-05.txt |
TLE |
2063 ms |
224036 KB |
subtask3-06.txt |
TLE |
2061 ms |
225444 KB |
subtask3-07.txt |
TLE |
2058 ms |
221292 KB |
subtask3-08.txt |
TLE |
2058 ms |
221344 KB |
subtask3-09.txt |
TLE |
2060 ms |
223648 KB |
subtask3-10.txt |
TLE |
2059 ms |
220128 KB |
subtask3-11.txt |
TLE |
2060 ms |
219424 KB |
subtask3-12.txt |
TLE |
2060 ms |
258260 KB |
subtask3-13.txt |
TLE |
2070 ms |
222176 KB |
subtask3-14.txt |
TLE |
2055 ms |
224680 KB |
subtask3-15.txt |
TLE |
2057 ms |
224156 KB |
subtask3-16.txt |
TLE |
2058 ms |
221668 KB |
subtask3-17.txt |
TLE |
2057 ms |
223136 KB |
subtask3-18.txt |
TLE |
2058 ms |
225052 KB |
subtask3-19.txt |
TLE |
2062 ms |
221468 KB |
subtask3-20.txt |
RE |
508 ms |
17884 KB |
subtask3-21.txt |
TLE |
2060 ms |
224800 KB |
subtask3-22.txt |
TLE |
2057 ms |
223776 KB |