QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#751553 | #9733. Heavy-light Decomposition | Rosmontis_L | RE | 0ms | 3556kb | C++20 | 2.4kb | 2024-11-15 19:21:37 | 2024-11-15 19:21:37 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const long double PI = acos(-1);
//#define int long long
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll, ll> PLL;
typedef tuple<ll, ll, ll> TLL;
int lowbit(int x) {return x & (- x);};
const int N = 200010, mod = 998244353;
const int Mod = 1e9 + 7, INF = 0x3f3f3f3f;
void solve(){
ll n, k;
cin >> n >> k;
vector<PLL> seg(k + 1);
vector<PLL> l1, l2, l3;
ll maxlen = 0;
for(int i = 1; i <= k; i ++){
ll l, r;
cin >> l >> r;
seg[i] = {l, r};
ll len = r - l;
maxlen = max(maxlen, len);
}
if(n == 1){
for(int i = 1; i <= n; i ++) cout << i - 1 << ' ';
cout << '\n';
return;
}
bool ok = 1;
ll pl = 0, pr = 0;
for(int i = 1; i <= k; i ++){
auto [l, r] = seg[i];
ll len = r - l;
if(len == maxlen){
if(ok == 1){
pl = l, pr = r;
ok ^= 1;
}
else l2.push_back({l, r});
}
else if(len == maxlen - 1){
l3.push_back({l, r});
}
else l1.push_back({l, r});
}
ll p = 0;
if(l2.size()) p = max(p, maxlen);
if(l3.size()) p = max(p, maxlen - 1);
vector<ll> ans(n + 1);
ans[pl] = 0;
for(int i = pl + 1; i <= pr; i ++) ans[i] = i - 1;
ll siz = maxlen;
for(auto [l, r] : l1){
ll len = r - l;
siz += len + 1;
ll rt = pr - len - 1;
//cout << l << ' ' << r << ' ' << rt << '\n';
ans[l] = rt;
for(int i = l + 1; i <= r; i ++) ans[i] = i - 1;
}
ll ss = 0;
for(int i = 1; i <= n; i ++){
auto [l, r] = seg[i];
ss += (r - l == maxlen);
}
if(siz < p || ss == n){
cout << "IMPOSSIBLE\n";
}
else{
for(auto [l, r] : l2){
ans[l] = pl;
for(int i = l + 1; i <= r; i ++) ans[i] = i - 1;
}
for(auto [l, r] : l3){
ans[l] = pl;
for(int i = l + 1; i <= r; i ++) ans[i] = i - 1;
}
for(int i = 1; i <= n; i ++){
cout << ans[i] << " \n"[i == n];
}
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int t = 1;
cin >> t;
while(t --){
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3556kb
input:
3 12 5 1 5 9 11 7 8 6 6 12 12 4 3 1 1 4 4 2 3 2 2 1 1 2 2
output:
0 1 2 3 4 4 3 7 2 9 10 4 2 0 2 2 IMPOSSIBLE
result:
ok Correct. (3 test cases)
Test #2:
score: -100
Runtime Error
input:
10 1 1 1 1 100000 1 1 100000 12 4 1 3 4 6 7 9 10 12 6 3 4 6 2 3 1 1 8999 3 1 3000 3001 6000 6001 8999 14 4 1 3 4 6 7 10 11 14 17 5 1 3 4 6 7 10 11 14 15 17 19999 2 1 9999 10000 19999 1 1 1 1 5 3 1 1 2 3 4 5