最优解报道。
先声明:本题解中所有公式不考虑边界情况。
首先,题目里的 一看就不好维护,观察到只要不走回头路,每走一条特殊路径(就是 连向 )就比原来不走特殊路径少走一条边,所以记 为走到 且不走回头路最多可以经过的特殊路径数量,原式就变成:
而
所以
问题就变成求 。
在 上挂扫描线 的转移是 ,若 上有特殊道路,则 可以和 取 。此时,我们需要一个数据结构解决区间和一个数取 并求出更改的值。注意到 是单调的,也就是说 ,所以原问题就变成了区间覆盖,求更改的值。
第一篇题解中用线段树实现,实际上可以用珂爱的珂朵莉树解决:因为每次最多增加一段区间,每次推平的时间复杂度是它推掉的区间个数,最劣情况也是 的,下面给出实现:
code for ODT
// @author fkxr(luogu uid = 995934)
// https://hydro.ac/d/Fkxr/p/IO
// CC BY-SA 4.0 https://creativecommons.org/licenses/by-sa/4.0/
#define luogu
#ifndef luogu
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#else
#include <bits/stdc++.h>
#endif
#ifndef db
#define db 0
#endif
#define endl cerr<<"------------------I Love the Olympiad in Informatics------------------\n";
#define int long long
#define _CRT_SECURE_NO_WARNINGS 1
#define _4x _4x
#define all(x) x.begin(),x.end()
//#define BIT BIT
//#define ST ST
//#define dsu dsu
using namespace std;
#ifdef __linux__
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc getchar
#define pc putchar
#define __int128 __int128_t
#endif
#define ds(x) (x=='\r'||x=='\n'||x==' ')
#define MAX 20
namespace fastIO {//吾常于「oi-wiki」习,自悟「快读快写」之术。
template<typename T>inline void r(T& a) { a = 0; char ch = gc(); bool ok = 0; for (; ch < '0' || ch>'9';)ok ^= (ch == '-'), ch = gc(); for (; ch >= '0' && ch <= '9';)a = (a << 1) + (a << 3) + (ch ^ 48), ch = gc(); if (ok)a = -a; }
template<typename T>inline void w(T a) { if (a == 0) { pc('0'); return; }static char ch[MAX]; int till = 0; if (a < 0) { pc('-'); for (; a;)ch[till++] = -(a % 10), a /= 10; } else for (; a;)ch[till++] = a % 10, a /= 10; for (; till;)pc(ch[--till] ^ 48); }
struct FIstream {
inline FIstream operator>>(int& a) { r(a); return{}; }
inline FIstream operator>>(char& ch) { ch = gc(); for (; ds(ch);)ch = gc(); return{}; }
inline FIstream operator>>(string& s) { s = ""; char ch = gc(); for (; ds(ch);)ch = gc(); for (; !(ds(ch) || ch == EOF);) { s.push_back(ch); ch = gc(); }return{}; }
template<typename T>inline FIstream operator<<(T& a) { r(a); return{}; }
inline void is(int n, string& s) { s = ""; char ch = gc(); for (; ds(ch);)ch = gc(); for (; n--;) { s.push_back(ch); ch = gc(); } }
}in;
struct FOstream {
inline FOstream operator<<(const int a) { w(a); return{}; }
inline FOstream operator<<(const char ch) { pc(ch); return{}; }
inline FOstream operator<<(const string s) { for (int i = 0; i < s.size(); i++)pc(s[i]); return{}; }
template<typename T>inline FOstream operator>>(const T a) { w(a); return{}; }
}out;
}using fastIO::in; using fastIO::out;
#undef ds
#define eout cerr
namespace Maths {//吾常赴「MO」之赛,自悟「数学」之术。
const bool __is_P[] = { 0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1 };
inline bool IP1(const int a) { if (a <= 29)return __is_P[a]; if (a % 2 == 0 || a % 3 == 0 || a % 5 == 0)return 0; for (int i = 6;; i += 6) { if (((i + 1) * (i + 1)) > a)return 1; if (a % (i + 1) == 0)return 0; if (((i + 5) * (i + 5)) > a)return 1; if (a % (i + 5) == 0)return 0; } }
#define times(a,b,m) (c=(unsigned long long)a*b-(unsigned long long)((long double)a/m*b+0.5L)*m,c<m?c:m+c)
inline int power(int a, int b, const int mod = -1) { unsigned long long c; int ans = 1; if (mod == -1) { for (; b;) { if (b & 1)ans *= a; b >>= 1; a *= a; }return ans; }for (; b;) { if (b & 1)ans = times(ans, a, mod); b >>= 1; a = times(a, a, mod); }return ans; }
const int Suk[] = { 2,325,9375,28178,450775,9780504,1795265022 };
inline bool chk(const int n, int a, int b, int x) { if (x >= n) return 1; unsigned long long c; int v = power(x, a, n); if (v == 1)return 1; int j = 1; while (j <= b) { if (v == n - 1)break; v = times(v, v, n); j++; }if (j > b)return 0; return 1; }
inline bool IP(int n) { if (n < 3 || n % 2 == 0)return n == 2; if (n <= 1e6) { return IP1(n); } else { int a = n - 1, b = 0; while (a % 2 == 0)a >>= 1, b++; for (int k : Suk)if (!chk(n, a, b, k))return 0; return 1; } }
#undef times
} using Maths::power;
using Maths::IP;
namespace exs {
#ifdef _4x
int dx[] = { 1,-1,0,0 }, dy[] = { 0,0,1,-1 };
#else
int dx[] = { 1,0,-1,-1,1,1,0,-1 }, dy[] = { 1,1,1,0,0,-1,-1,-1 };
#endif
template<typename T, typename T1, typename T2>inline bool bt(T l, T2 x, T1 r) { return l <= x && x <= r; }
inline bool emc(const int& a, const int& b) { return a > b; }
#define popcti(val) ((int)__builtin_popcount(val))
#define popct(val) ((int)__builtin_popcountll(val))
#ifndef _MSC_VER
void Freopen(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); }
#endif
#ifdef BIT//吾常读「《算法竞赛进阶指南》」之书,自悟「树状数组」之术。
struct bit {
vector<int> c0, c1; int n;
inline void Add(vector<int>& c, int p, int v) { for (; p <= n; p += p & -p) c[p] += v; }
inline int Sum(const vector<int>& c, int p) { int t = 0; for (; p; p -= p & -p) t += c[p]; return t; }
inline int sum(int l, int r) { assert(n != 0); return Sum(c0, r) * r - Sum(c1, r) - Sum(c0, l - 1) * (l - 1) + Sum(c1, l - 1); }
inline void add(int l, int r, int v) { assert(n != 0); Add(c0, l, v); Add(c0, r + 1, -v); Add(c1, l, (l - 1) * v); Add(c1, r + 1, -r * v); }
inline void init(const vector<int>& c) { n = c.size() - 1; c0.resize(n + 2); c1.resize(n + 2); int last = 0; for (int i = 1; i <= n; i++) { last = c[i] - last; Add(c0, i, last); Add(c1, i, last * (i - 1)); last = c[i]; } }
};
#endif
#ifdef ST//吾常读「《算法竞赛进阶指南》」之书,自悟「ST表」之术。
struct st {
vector<int> lg; vector<vector<int>> f;
inline void init(const vector<int>& c) { int len = c.size() - 1; lg.resize(len + 2); for (int i = 2; i <= len; i++) lg[i] = lg[i >> 1] + 1; f.resize(18, vector<int>(len + 2)); for (int i = 1; i <= len; i++) f[0][i] = c[i]; for (int j = 1; (1 << j) <= len; j++) for (int i = 1; i + (1 << j) - 1 <= len; i++) f[j][i] = max(f[j - 1][i], f[j - 1][i + (1 << (j - 1))]); }
inline int q(int l, int r) { int j = lg[r - l + 1]; return max(f[j][l], f[j][r - (1 << j) + 1]); }
};
#endif
#ifdef dsu//0x 吾常读「《算法竞赛进阶指南》」之书,自悟「并查集」之术。
struct dsu {
vector<int>fa;
dsu(int n):fa(n){iota(all(fa),0);}
inline int find(int x){return fa[x]==x?x:fa[x]=fa[fa[x]]=fa[fa[fa[x]]]=find(fa[fa[fa[x]]]);}
inline void unite(int x,int y){fa[find(x)]=find(y);}
//x->y
};
#endif
}using namespace exs;
struct node{
int l,r,v;
node(int ll,int rr=0,int vv=0){l=ll,r=rr,v=vv;}
friend bool operator<(node a,node b){return a.l<b.l;}
};
set<node>s;
set<node>::iterator split(int x){
auto it=s.lower_bound(node(x));
if(it!=s.end()&&it->l==x)return it;
node tmp=*(--it);
if(tmp.r<x)return s.end();
s.erase(it);
s.insert(node(tmp.l,x-1,tmp.v));
return s.insert(node(x,tmp.r,tmp.v)).first;
}
int ans;
void update(int l,int time){
auto it=split(l);int r=l-1;
auto tmp=it;--tmp;
int x=tmp->v+1;
for(;it!=s.end()&&it->v<=x;r=it->r,++it)
ans+=(it->r-it->l+1)*(it->v-x)*time;
if(r>=l){
auto itr=split(r+1),itl=split(l);
s.erase(itl,itr);
s.insert(node(l,r,x));
}
}
void Main() {
int T;in>>T;
for(;T--;){
int n,p,q;
in>>n>>p>>q;
s.clear();ans=p*(p+1)*(q+1)/2+q*(q+1)*(p+1)/2;
s.insert(node(0,q,0));
vector<pair<int,int>>e;
for(;n--;){
int x,y;in>>x>>y;
if(x>=p||y>=q)continue;
e.push_back({x,-y});
}
sort(all(e));
for(auto i:e)update(-i.second+1,p-i.first/*+1*/);
out<<ans<<"\n";
}
}
signed main() {
Main();
return 0;
}但用数据结构写实在太 low 了,注意到 ,所以 原来的 ODT 只会推平一段区间的数 记录 的位置,记它为更改点,每一次推平只会是把一个更改点前移,或在最后一段区间创建一个更改点,使用 vector(普通数组也行)维护,然后二分,时间复杂度 。
code for vector
// @author fkxr(luogu uid = 995934)
// https://hydro.ac/d/Fkxr/p/IO
// CC BY-SA 4.0 https://creativecommons.org/licenses/by-sa/4.0/
#define luogu
#ifndef luogu
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#else
#include <bits/stdc++.h>
#endif
#ifndef db
#define db 0
#endif
#define endl cerr<<"------------------I Love the Olympiad in Informatics------------------\n";
#define int long long
#define _CRT_SECURE_NO_WARNINGS 1
#define _4x _4x
#define all(x) x.begin(),x.end()
//#define BIT BIT
//#define ST ST
//#define dsu dsu
using namespace std;
#ifdef __linux__
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc getchar
#define pc putchar
#define __int128 __int128_t
#endif
#define ds(x) (x=='\r'||x=='\n'||x==' ')
#define MAX 20
namespace fastIO {//吾常于「oi-wiki」习,自悟「快读快写」之术。
template<typename T>inline void r(T& a) { a = 0; char ch = gc(); bool ok = 0; for (; ch < '0' || ch>'9';)ok ^= (ch == '-'), ch = gc(); for (; ch >= '0' && ch <= '9';)a = (a << 1) + (a << 3) + (ch ^ 48), ch = gc(); if (ok)a = -a; }
template<typename T>inline void w(T a) { if (a == 0) { pc('0'); return; }static char ch[MAX]; int till = 0; if (a < 0) { pc('-'); for (; a;)ch[till++] = -(a % 10), a /= 10; } else for (; a;)ch[till++] = a % 10, a /= 10; for (; till;)pc(ch[--till] ^ 48); }
struct FIstream {
inline FIstream operator>>(int& a) { r(a); return{}; }
inline FIstream operator>>(char& ch) { ch = gc(); for (; ds(ch);)ch = gc(); return{}; }
inline FIstream operator>>(string& s) { s = ""; char ch = gc(); for (; ds(ch);)ch = gc(); for (; !(ds(ch) || ch == EOF);) { s.push_back(ch); ch = gc(); }return{}; }
template<typename T>inline FIstream operator<<(T& a) { r(a); return{}; }
inline void is(int n, string& s) { s = ""; char ch = gc(); for (; ds(ch);)ch = gc(); for (; n--;) { s.push_back(ch); ch = gc(); } }
}in;
struct FOstream {
inline FOstream operator<<(const int a) { w(a); return{}; }
inline FOstream operator<<(const char ch) { pc(ch); return{}; }
inline FOstream operator<<(const string s) { for (int i = 0; i < s.size(); i++)pc(s[i]); return{}; }
template<typename T>inline FOstream operator>>(const T a) { w(a); return{}; }
}out;
}using fastIO::in; using fastIO::out;
#undef ds
#define eout cerr
namespace Maths {//吾常赴「MO」之赛,自悟「数学」之术。
const bool __is_P[] = { 0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1 };
inline bool IP1(const int a) { if (a <= 29)return __is_P[a]; if (a % 2 == 0 || a % 3 == 0 || a % 5 == 0)return 0; for (int i = 6;; i += 6) { if (((i + 1) * (i + 1)) > a)return 1; if (a % (i + 1) == 0)return 0; if (((i + 5) * (i + 5)) > a)return 1; if (a % (i + 5) == 0)return 0; } }
#define times(a,b,m) (c=(unsigned long long)a*b-(unsigned long long)((long double)a/m*b+0.5L)*m,c<m?c:m+c)
inline int power(int a, int b, const int mod = -1) { unsigned long long c; int ans = 1; if (mod == -1) { for (; b;) { if (b & 1)ans *= a; b >>= 1; a *= a; }return ans; }for (; b;) { if (b & 1)ans = times(ans, a, mod); b >>= 1; a = times(a, a, mod); }return ans; }
const int Suk[] = { 2,325,9375,28178,450775,9780504,1795265022 };
inline bool chk(const int n, int a, int b, int x) { if (x >= n) return 1; unsigned long long c; int v = power(x, a, n); if (v == 1)return 1; int j = 1; while (j <= b) { if (v == n - 1)break; v = times(v, v, n); j++; }if (j > b)return 0; return 1; }
inline bool IP(int n) { if (n < 3 || n % 2 == 0)return n == 2; if (n <= 1e6) { return IP1(n); } else { int a = n - 1, b = 0; while (a % 2 == 0)a >>= 1, b++; for (int k : Suk)if (!chk(n, a, b, k))return 0; return 1; } }
#undef times
} using Maths::power;
using Maths::IP;
namespace exs {
#ifdef _4x
int dx[] = { 1,-1,0,0 }, dy[] = { 0,0,1,-1 };
#else
int dx[] = { 1,0,-1,-1,1,1,0,-1 }, dy[] = { 1,1,1,0,0,-1,-1,-1 };
#endif
template<typename T, typename T1, typename T2>inline bool bt(T l, T2 x, T1 r) { return l <= x && x <= r; }
inline bool emc(const int& a, const int& b) { return a > b; }
#define popcti(val) ((int)__builtin_popcount(val))
#define popct(val) ((int)__builtin_popcountll(val))
#ifndef _MSC_VER
void Freopen(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); }
#endif
#ifdef BIT//吾常读「《算法竞赛进阶指南》」之书,自悟「树状数组」之术。
struct bit {
vector<int> c0, c1; int n;
inline void Add(vector<int>& c, int p, int v) { for (; p <= n; p += p & -p) c[p] += v; }
inline int Sum(const vector<int>& c, int p) { int t = 0; for (; p; p -= p & -p) t += c[p]; return t; }
inline int sum(int l, int r) { assert(n != 0); return Sum(c0, r) * r - Sum(c1, r) - Sum(c0, l - 1) * (l - 1) + Sum(c1, l - 1); }
inline void add(int l, int r, int v) { assert(n != 0); Add(c0, l, v); Add(c0, r + 1, -v); Add(c1, l, (l - 1) * v); Add(c1, r + 1, -r * v); }
inline void init(const vector<int>& c) { n = c.size() - 1; c0.resize(n + 2); c1.resize(n + 2); int last = 0; for (int i = 1; i <= n; i++) { last = c[i] - last; Add(c0, i, last); Add(c1, i, last * (i - 1)); last = c[i]; } }
};
#endif
#ifdef ST//吾常读「《算法竞赛进阶指南》」之书,自悟「ST表」之术。
struct st {
vector<int> lg; vector<vector<int>> f;
inline void init(const vector<int>& c) { int len = c.size() - 1; lg.resize(len + 2); for (int i = 2; i <= len; i++) lg[i] = lg[i >> 1] + 1; f.resize(18, vector<int>(len + 2)); for (int i = 1; i <= len; i++) f[0][i] = c[i]; for (int j = 1; (1 << j) <= len; j++) for (int i = 1; i + (1 << j) - 1 <= len; i++) f[j][i] = max(f[j - 1][i], f[j - 1][i + (1 << (j - 1))]); }
inline int q(int l, int r) { int j = lg[r - l + 1]; return max(f[j][l], f[j][r - (1 << j) + 1]); }
};
#endif
#ifdef dsu//0x 吾常读「《算法竞赛进阶指南》」之书,自悟「并查集」之术。
struct dsu {
vector<int>fa;
dsu(int n):fa(n){iota(all(fa),0);}
inline int find(int x){return fa[x]==x?x:fa[x]=fa[fa[x]]=fa[fa[fa[x]]]=find(fa[fa[fa[x]]]);}
inline void unite(int x,int y){fa[find(x)]=find(y);}
//x->y
};
#endif
}using namespace exs;
void Main() {
int T;in>>T;
for(;T--;){
int n,p,q;in>>n>>p>>q;
int ans=(q+1)*(p+1)*(q+p)/2;
vector<pair<int,int>>a;
for(int i=1;i<=n;i++){
int x,y;in>>x>>y;
if(x>=p||y>=q)continue;
a.push_back({x,-y});
}
sort(all(a));
vector<int>tmp;
for(auto i:a){
int x=i.first,y=-i.second;
int w=lower_bound(all(tmp),y+1)-tmp.begin();
if(w>=tmp.size()){
tmp.push_back(y+1);
ans-=(q-y)*(p-x);
}else{
ans-=(tmp[w]-y-1)*(p-x);
tmp[w]=y+1;
}
}
out<<ans<<"\n";
}
}
signed main() {
Main();
return 0;
}