QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#188137 | #4887. Fast Bridges | Dualqwq | TL | 1244ms | 32404kb | C++23 | 5.2kb | 2023-09-25 15:29:59 | 2023-09-25 15:29:59 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 5e2 + 5,P = 998244353,inv3 = (P + 1) / 3;
typedef pair<int,int> pii;
#define FI first
#define SE second
inline void Plus(int &x,const int &y) { x += y;if(x >= P) x -= P;}
template<typename T> inline void ckmax(T &x,const T &y) { (x < y) && (x = y);}
int n,K;
struct Bridge {
int x1,y1,x2,y2;
Bridge(){}
bool operator < (const Bridge &rhs) const { return x2 < rhs.x2;}
};
Bridge p[N];
inline bool Trans1(const Bridge &i,const Bridge &j) {
return i.x2 <= j.x1 && i.y2 <= j.y1;
}
inline bool Trans2(const Bridge &i,const Bridge &j) {
return i.x2 <= j.x1 && i.y2 >= j.y1;
}
int dp[N][N];
int val1[N << 1],len1;
int val2[N << 1],len2;
int f[2][N << 1][N]; // f_{x,y,i}
vector<pii> Poi[N]; // x2 升序
vector<int> brs[N << 1][N << 1];
int Area; // 2-side 矩形面积并
int LowY; // Y 的最值
Bridge p1[N],p2[N];
int n1,n2;
inline int Solve1() {
for(int i = 1;i <= n1;i++)
for(int j = 1;j <= n1;j++) dp[i][j] = 0;
for(int i = 1;i <= n1;i++) dp[i][i] = 1;
for(int len = 2;len <= n1;len++)
for(int i = 1;i + len - 1 <= n1;i++) {
int j = i + len - 1;
if(!Trans1(p1[i],p1[j])) continue;
dp[i][j] = 2;
for(int k = i + 1;k < j;k++)
if(Trans1(p1[i],p1[k]) && Trans1(p1[k],p1[j]))
dp[i][j] = max(dp[i][j],dp[i][k] + dp[k][j] - 1);
}
for(int i = 1;i <= len1;i++)
for(int j = 1;j <= len2;j++) brs[i][j].clear();
for(int i = 1;i <= n1;i++)
brs[p1[i].x1][p1[i].y1].push_back(i);
memset(f,0,sizeof f);
int ans = 0;
for(int x = len1 - 1;x >= 1;x--) {
// for(int y = 1;y <= len2;y++)
// for(int i = 1;i <= n1;i++) f[x&1][y][i] = 0;
// memcpy(f[x & 1],f[(x+1)&1],sizeof f[(x+1)&1]);
for(int y = len2 - 1;y >= 1;y--) {
for(int i = 1;i <= n1;i++) {
// ckmax(f[x & 1][y][i],f[x & 1][y + 1][i]);
f[x & 1][y][i] = max(f[x & 1][y + 1][i],f[(x+1) & 1][y][i]);
for(auto id : brs[x][y])
ckmax(f[x & 1][y][i],dp[id][i]);
// printf("f[%d][%d][%d]=%d\n",x,y,i,f[x&1][y][i]);
}
for(int i = 1;i <= n1;i++) Poi[i].clear();
for(int i = 1;i <= n1;i++)
if(f[x & 1][y][i])
Poi[f[x & 1][y][i]].emplace_back(val1[p1[i].x2],val2[p1[i].y2]);
for(int t = 1;t <= n1;t++) if(!Poi[t].empty()) {
Area = 0;LowY = K + 1;
for(auto it : Poi[t])
if(it.SE < LowY)
Plus(Area,1ll * (LowY - it.SE) * (K - it.FI + 1) % P),
LowY = it.SE;
Plus(ans,1ll * Area * (val1[x] - val1[x - 1]) % P * (val2[y] - val2[y - 1]) % P);
}
}
}
// printf("ans:%d\n",ans);
return ans;
}
inline int Solve2() {
for(int i = 1;i <= n2;i++) for(int j = 1;j <= n2;j++) dp[i][j] = 0;
for(int i = 1;i <= n2;i++) dp[i][i] = 1;
for(int len = 2;len <= n2;len++)
for(int i = 1;i + len - 1 <= n2;i++) {
int j = i + len - 1;
if(!Trans2(p2[i],p2[j])) continue;
dp[i][j] = 2;
for(int k = i + 1;k < j;k++)
if(Trans2(p2[i],p2[k]) && Trans2(p2[k],p2[j]))
dp[i][j] = max(dp[i][j],dp[i][k] + dp[k][j] - 1);
}
for(int i = 1;i <= len1;i++)
for(int j = 1;j <= len2;j++)
brs[i][j].clear();
for(int i = 1;i <= n2;i++)
brs[p2[i].x1][p2[i].y1].emplace_back(i);
memset(f,0,sizeof f);
int ans = 0;
for(int x = len1 - 1;x >= 1;x--) {
// for(int y = 1;y <= len2;y++)
// for(int i = 1;i <= n2;i++) f[x & 1][y][i] = 0;
// memcpy(f[x & 1],f[(x+1)&1],sizeof f[(x+1)&1]);
for(int y = 1;y < len2;y++) {
for(int i = 1;i <= n2;i++) {
// ckmax(f[x & 1][y][i],f[x & 1][y - 1][i]);
f[x & 1][y][i] = max(f[(x+1)&1][y][i],f[x & 1][y - 1][i]);
for(auto id : brs[x][y])
ckmax(f[x & 1][y][i],dp[id][i]);
}
for(int i = 1;i <= n2;i++) Poi[i].clear();
for(int i = 1;i <= n2;i++)
if(f[x & 1][y][i]) Poi[f[x & 1][y][i]].emplace_back(val1[p2[i].x2],val2[p2[i].y2]);
for(int t = 1;t <= n2;t++) if(!Poi[t].empty()) {
Area = 0;LowY = 0;
for(auto it : Poi[t])
if(it.SE > LowY)
Plus(Area,1ll * (it.SE - LowY) * (K - it.FI + 1) % P),
LowY = it.SE;
Plus(ans,1ll * Area * (val1[x] - val1[x - 1]) % P * (val2[y + 1] - val2[y]) % P);
}
}
}
// printf("ans:%d\n",ans);
return ans;
}
int main() {
cin >> n >> K;
for(int i = 1;i <= n;i++)
cin >> p[i].x1 >> p[i].y1 >> p[i].x2 >> p[i].y2;
sort(p + 1,p + n + 1);
for(int i = 1;i <= n;i++) val1[++len1] = p[i].x1,val1[++len1] = p[i].x2;
val1[++len1] = K + 1;
sort(val1 + 1,val1 + len1 + 1);
len1 = unique(val1 + 1,val1 + len1 + 1) - val1 - 1;
for(int i = 1;i <= n;i++) val2[++len2] = p[i].y1,val2[++len2] = p[i].y2;
val2[++len2] = K + 1;
sort(val2 + 1,val2 + len2 + 1);
len2 = unique(val2 + 1,val2 + len2 + 1) - val2 - 1;
auto idX = [&](const int &t) { return lower_bound(val1 + 1,val1 + len1 + 1,t) - val1;};
auto idY = [&](const int &t) { return lower_bound(val2 + 1,val2 + len2 + 1,t) - val2;};
for(int i = 1;i <= n;i++)
p[i].x1 = idX(p[i].x1),p[i].x2 = idX(p[i].x2),
p[i].y1 = idY(p[i].y1),p[i].y2 = idY(p[i].y2);
for(int i = 1;i <= n;i++)
if(p[i].y1 < p[i].y2) p1[++n1] = p[i];
else p2[++n2] = p[i];
int ans = 1ll * K * K % P * K % P * K % P * (K + 1) % P;
Plus(ans,P - 1ll * K * K % P * K % P * (K + 1) % P * (K + K + 1) % P * inv3 % P);
Plus(ans,P - Solve1());
Plus(ans,P - Solve2());
cout << ans << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 31484kb
input:
2 2 1 1 2 2 1 2 2 1
output:
6
result:
ok answer is '6'
Test #2:
score: 0
Accepted
time: 8ms
memory: 31372kb
input:
0 1000000000
output:
916520226
result:
ok answer is '916520226'
Test #3:
score: 0
Accepted
time: 4ms
memory: 31372kb
input:
5 5 1 1 3 3 3 3 5 1 3 3 4 5 3 3 5 4 1 5 3 3
output:
946
result:
ok answer is '946'
Test #4:
score: 0
Accepted
time: 9ms
memory: 31732kb
input:
200 5 1 1 4 2 2 5 4 4 2 3 4 2 2 4 3 5 1 4 4 2 2 5 4 2 1 2 4 4 1 2 2 4 1 4 5 1 3 4 5 1 4 2 5 1 2 2 5 4 3 2 5 1 3 1 5 2 4 2 5 3 1 3 5 1 3 4 4 5 2 2 4 3 2 3 5 4 1 4 5 3 2 2 3 1 2 5 3 3 1 1 5 3 4 4 5 5 1 3 4 4 4 3 5 1 2 3 3 4 3 4 4 2 1 4 4 5 2 1 4 4 1 3 5 2 1 1 3 3 1 5 3 1 1 1 3 5 1 4 3 5 4 5 5 4 1 1 4 ...
output:
708
result:
ok answer is '708'
Test #5:
score: 0
Accepted
time: 10ms
memory: 31904kb
input:
500 10 5 6 7 10 1 3 8 10 3 3 4 9 2 10 10 2 9 4 10 10 5 4 7 8 7 1 10 7 3 1 7 10 5 2 8 9 6 3 7 10 3 10 7 9 4 9 5 1 2 5 3 3 7 10 8 2 7 7 9 8 6 6 8 3 5 10 8 8 1 1 5 5 3 3 10 5 5 5 7 6 3 8 4 7 6 7 7 5 7 3 10 9 5 3 9 4 4 6 10 5 1 5 9 10 5 6 9 7 3 10 10 3 1 2 5 7 4 6 5 1 3 1 8 5 5 8 8 9 1 8 4 3 6 4 7 10 7 ...
output:
27373
result:
ok answer is '27373'
Test #6:
score: 0
Accepted
time: 4ms
memory: 31900kb
input:
500 30 3 13 20 29 14 5 16 25 2 29 9 15 23 30 24 9 1 18 24 28 4 16 5 2 3 29 30 25 4 8 24 19 8 26 10 24 20 14 26 25 15 8 25 25 5 13 18 28 3 30 29 10 14 26 25 11 11 19 16 4 9 4 29 30 15 10 16 8 2 29 12 2 11 22 20 28 4 10 28 1 24 17 30 1 8 26 27 9 15 25 30 14 16 20 24 17 9 23 12 13 9 16 25 28 2 15 8 16 ...
output:
7717993
result:
ok answer is '7717993'
Test #7:
score: 0
Accepted
time: 14ms
memory: 31904kb
input:
500 100 25 55 55 43 14 22 84 5 57 7 79 15 63 9 86 23 22 3 53 97 2 22 64 65 32 52 66 30 76 37 79 22 46 100 76 22 21 78 78 44 29 41 92 55 43 14 46 3 14 97 42 1 16 7 35 64 15 27 29 3 11 92 92 70 4 13 66 2 3 38 55 82 41 94 83 44 52 90 100 82 6 100 99 70 18 38 24 22 74 17 98 20 17 94 44 82 33 97 48 19 12...
output:
291628571
result:
ok answer is '291628571'
Test #8:
score: 0
Accepted
time: 4ms
memory: 32376kb
input:
500 8 2 4 8 2 3 7 5 4 2 6 8 1 4 8 5 5 6 6 7 5 2 6 5 5 1 6 8 5 6 5 7 3 4 8 5 7 5 7 6 5 1 6 4 5 2 3 4 2 2 8 8 6 3 8 4 3 5 6 7 2 7 8 8 3 1 8 4 7 1 6 6 1 1 8 7 1 1 4 3 3 2 3 3 1 1 4 5 1 1 8 5 4 7 7 8 5 2 7 4 1 3 7 4 3 2 3 5 1 3 7 8 1 4 7 5 5 6 6 8 3 2 7 5 1 2 5 4 3 5 4 8 2 4 5 8 3 2 3 4 1 2 8 3 2 5 6 8 ...
output:
9321
result:
ok answer is '9321'
Test #9:
score: 0
Accepted
time: 1229ms
memory: 31860kb
input:
500 1000000000 228604634 522874974 789854111 585676486 340802063 175643637 661594207 749079321 490078806 844144515 583746323 707696611 833939453 901516824 867397264 848066012 553537526 886003963 679012061 187030606 351500555 847099665 751201742 855105070 169763646 729114554 248951243 211939611 17040...
output:
230090667
result:
ok answer is '230090667'
Test #10:
score: 0
Accepted
time: 1148ms
memory: 32388kb
input:
500 1000000000 536804949 618264275 757262973 133194920 206604343 420304040 244005624 331707206 64548973 877773848 685024560 565782395 13572244 271309598 835979107 128627415 128103153 561746493 703898577 9276472 209282309 997406956 216339996 279878227 386095652 999498735 908610032 582414132 232191790...
output:
404991176
result:
ok answer is '404991176'
Test #11:
score: 0
Accepted
time: 1174ms
memory: 32380kb
input:
500 1000000000 435165109 887707979 541968631 834720917 43164344 595179931 731392283 541750474 51147932 885859385 525997101 813310992 581745995 569929983 666239343 349298365 720599913 330436249 751561895 84593980 254142704 924477087 706739688 760929039 282091849 414650019 853811117 121534462 21407507...
output:
174105246
result:
ok answer is '174105246'
Test #12:
score: 0
Accepted
time: 1244ms
memory: 32404kb
input:
500 1000000000 334968963 60182667 683993047 330063742 372714145 727060351 391638535 972082352 15288009 443033033 549932294 626507494 551292358 201286324 844192128 989162325 138957062 834473180 233314539 840742618 774917762 293038146 784290713 868100668 88362426 108423246 90763875 635080794 197409326...
output:
819654628
result:
ok answer is '819654628'
Test #13:
score: 0
Accepted
time: 1170ms
memory: 32344kb
input:
500 1000000000 407797655 600906761 451028876 557753318 739109786 505834673 914488662 267694229 21613693 815099602 741520301 86754775 749426136 864500481 989644055 760004108 97929570 281277866 645537954 194083134 386298407 900097354 590149576 876589970 225981751 604462892 313700311 201620926 13512935...
output:
704804476
result:
ok answer is '704804476'
Test #14:
score: 0
Accepted
time: 1233ms
memory: 31780kb
input:
500 1000000000 136588729 322381152 198423052 586895024 146201252 78771798 320963978 33171878 103014217 582579333 112482565 472327049 363500012 171569343 779799989 210605961 916348434 897403875 958218658 848653603 81959275 288412262 293263271 877464982 155884974 409342051 490632310 353856648 42868173...
output:
701057894
result:
ok answer is '701057894'
Test #15:
score: 0
Accepted
time: 1223ms
memory: 31824kb
input:
500 1000000000 70732466 818210159 101241592 180120566 551559764 430141447 558477026 919623562 842854549 898003264 988655980 690377539 365038538 842566580 988616538 612555368 119137999 522482797 776356145 341894154 134943863 753491473 621956497 857574689 860979233 313689040 912231580 819779431 253383...
output:
849305849
result:
ok answer is '849305849'
Test #16:
score: 0
Accepted
time: 1243ms
memory: 31852kb
input:
500 1000000000 76067493 226360208 588463712 997370258 247139391 228988779 876938260 628658287 173490201 249999131 402004522 332729284 73514885 82656638 357464837 702514607 288650085 526722777 582817141 741491871 859774917 73498480 878952996 868608989 248586909 115745356 485233299 599896403 302539166...
output:
980753674
result:
ok answer is '980753674'
Test #17:
score: 0
Accepted
time: 605ms
memory: 32400kb
input:
500 919069957 742507159 740217847 742778031 741238898 320301045 312370945 321929532 313537690 344928356 347275650 349920032 348402734 128430402 156747983 128702472 159673979 89940237 122339622 90602165 123930504 638094551 604903042 638437986 606101004 118489244 152414022 121260981 154139858 41785067...
output:
347610358
result:
ok answer is '347610358'
Test #18:
score: -100
Time Limit Exceeded
input:
500 975373400 777474465 198550754 778099765 197445181 19790862 954672658 20856536 953636596 827980256 147778510 829125529 145266113 619221505 350128003 619713737 347384388 495799407 482522585 498152766 482508636 228703 974561916 1215128 974398280 950927762 28239912 951166074 26737006 102318845 88350...