超时暴力60分
#include <iostream>
#include <algorithm>
#include <cstring>
#include <sstream>
using namespace std;
typedef long long LL;
const int N = 5e5 + 4, mod = 1e9 + 7;vector<int> v[N];
int y[N], sizy, x[N], sizx, numx[N], a[N], b[N], n, q;
int c[N];// 这题暴力就是 先离散化之后看每个网格交点是否满足;
// 优化就是区间的时候可以先求出最大值,再一遍线段树更新每个点是否满足,修改是单点修改,查询是区间和 int main()
{scanf("%d%d", &n, &q);for(int i = 1;i <= n;i ++){scanf("%d%d", a+i, b+i);y[i] = a[i]; x[i] = b[i]; // 离散化数组,y和x好像有点抽象 }sort(y+1, y+n+1); sizy = unique(y+1, y+n+1)-y;sort(x+1, x+n+1); sizx = unique(x+1, x+n+1)-x;for(int i = 1;i <= n;i ++){a[i] = lower_bound(y+1, y+sizy, a[i]) - y;b[i] = lower_bound(x+1, x+sizx, b[i]) - x; numx[b[i]] ++;v[a[i]].push_back(b[i]); }for(int i = 1;i <= sizy;i ++)sort(v[i].begin(), v[i].end());int ma = 0, ans = 0;for(int i = 1;i < sizy;i ++){for(int j = 0;j < v[i].size()-1;j ++)for(int t = v[i][j]+1;t < v[i][j+1];t ++) // 遍历每个点算答案 {int mi = min(min(c[t], numx[t]-c[t]), min(j+1, (int)v[i].size()-j-1));if(mi>ma) // 更新答案 ma = mi, ans = 1; else if(ma == mi)ans ++;}for(auto x:v[i])c[x] ++; }if(q-1)cout<<ans;else cout<<ma;puts("");return 0;
}
100分线段树单点修改代码
#include <iostream>
#include <algorithm>
#include <cstring>
#include <sstream>
#define ls (u<<1)
#define rs (u<<1|1)
#define mid (l+r>>1)
using namespace std;
typedef long long LL;
const int N = 3e5 + 4, mod = 1e9 + 7;vector<int> v[N];
int y[N], sizy, x[N], sizx, numx[N], a[N], b[N], n, q;
int c[N], ma = 0;
LL ans = 0;
struct point{int ma, sum;
}tr[N<<2];void push_up(point &a, point &b, point &c){a.sum = b.sum+c.sum;a.ma=max(b.ma,c.ma);return ;}void modify(int u, int l, int r, int x, int f)
{if(l==r){tr[u].ma = min(c[x], numx[x]-c[x]);if(f) // 这个要判断,因该是 又一遍开始的时候有答案贡献把,奇怪; tr[u].sum = tr[u].ma >= ma ; }else {x <= mid ? modify(ls, l, mid, x, f) : modify(rs, mid+1, r, x, f);push_up(tr[u], tr[ls], tr[rs]);}return ;
}point quary(int u, int l, int r, int L, int R)
{ if(L<=l&&R>=r) return tr[u];point x = {0, 0}, z = {0, 0}, y = {0, 0};if(L <= mid) y = quary(ls, l, mid, L, R);if(R > mid) z = quary(rs, mid+1, r, L, R);push_up(x, z, y);return x;
}int main()
{scanf("%d%d", &n, &q);for(int i = 1;i <= n;i ++){scanf("%d%d", a+i, b+i);y[i] = a[i]; x[i] = b[i];}sort(y+1, y+n+1); sizy = unique(y+1, y+n+1)-y;sort(x+1, x+n+1); sizx = unique(x+1, x+n+1)-x;for(int i = 1;i <= n;i ++){a[i] = lower_bound(y+1, y+sizy, a[i]) - y;b[i] = lower_bound(x+1, x+sizx, b[i]) - x; numx[b[i]] ++;v[a[i]].push_back(b[i]); }for(int i = 1;i <= sizy;i ++)sort(v[i].begin(), v[i].end());for(int i = 1;i < sizy;i ++){for(int j = 0;j < v[i].size()-1;j ++)if(v[i][j]+1 < v[i][j+1]){int mi = min(min(j+1, (int)v[i].size()-j-1), quary(1, 1, sizx, v[i][j]+1, v[i][j+1]-1).ma);if(mi>ma) ma = mi; }for(auto x:v[i])c[x] ++, modify(1, 1, sizx, x, 0);}for(int i = 1;i < sizx;i ++) c[i] = 0;for(int i = 1;i < sizy;i ++){for(int j = 0;j < v[i].size()-1;j ++)if(v[i][j]+1 < v[i][j+1])ans += quary(1, 1, sizx, v[i][j]+1, v[i][j+1]-1).sum;for(auto x:v[i])c[x] ++, modify(1, 1, sizx, x, 1);}if(ma == 0)ans = 0;if(q&1)cout<<ma;else cout<<ans;puts("");return 0;
}