1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| #include <iostream> #include <cstdio> #include <vector> #include <algorithm> #include <stack> #include <cctype> #include <queue> #define maxn 2010 #define ll long long #define cn const node #define cQ const Queue #define gc getchar #define pb push_back using namespace std;
int read() { int x = 0, f = 0; char c = gc(); while (!isdigit(c)) f |= c == '-', c = gc(); while (isdigit(c)) x = x * 10 + c - '0', c = gc(); return f ? -x : x; }
int n, m, k;
int b[maxn];
struct node { int x, y; } a[maxn];
struct Node { int v, id;
Node() = default; Node(int _v, int _id) { v = _v; id = _id; } }; vector<Node> A[maxn], B[maxn];
#define lc i << 1 #define rc i << 1 | 1 struct Seg { int v, add; } T[maxn * 4]; inline void maintain(int i) { T[i].v = T[lc].v + T[rc].v; }
void build(int i, int l, int r) { T[i].v = T[i].add = 0; if (l == r) return ; int m = l + r >> 1; build(lc, l, m); build(rc, m + 1, r); }
void update(int i, int l, int r, int L, int R, int v) { if (l > R || r < L) return ; T[i].v += (min(R, r) - max(l, L) + 1) * v; if (L <= l && r <= R) return (void) (T[i].add += v); int m = l + r >> 1; update(lc, l, m, L, R, v); update(rc, m + 1, r, L, R, v); }
int query(int i, int l, int r, int L, int R, int add) { if (l > R || r < L) return 0; if (L <= l && r <= R) return T[i].v + add * (r - l + 1); int m = l + r >> 1; return query(lc, l, m, L, R, add + T[i].add) + query(rc, m + 1, r, L, R, add + T[i].add); }
int ans; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> m >> k; for (int i = 1; i <= m; ++i) cin >> a[i].x >> a[i].y; for (int i = 1; i + k - 1 <= n; ++i) { int p = i + k - 1, sum = 0; for (int j = 1; j <= n; ++j) A[j].clear(), B[j].clear(); build(1, 1, n); for (int j = 1; j <= m; ++j) { int l = a[j].x, r = a[j].y, Len = min(r, p) - max(i, l) + 1; if (Len <= 0) { update(1, 1, n, l, r, 1); continue ; } A[max(1, Len + l - k)].pb(Node(Len, j)); B[r - Len + 1].pb(Node(Len, j)); sum += Len; } for (int j = 1; j + k - 1 <= n; ++j) { for (auto u : A[j]) { int v = u.v, id = u.id; update(1, 1, n, a[id].x, a[id].y, 1); sum -= v; } for (auto u : B[j]) { int v = u.v, id = u.id; update(1, 1, n, a[id].x, a[id].y, -1); sum += v; } ans = max(ans, sum + query(1, 1, n, j, j + k - 1, 0)); } } cout << ans << "\n"; return 0; }
|