201609-5 祭坛
生活随笔
收集整理的這篇文章主要介紹了
201609-5 祭坛
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
超時(shí)暴力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];// 這題暴力就是 先離散化之后看每個(gè)網(wǎng)格交點(diǎn)是否滿足; // 優(yōu)化就是區(qū)間的時(shí)候可以先求出最大值,再一遍線段樹(shù)更新每個(gè)點(diǎn)是否滿足,修改是單點(diǎn)修改,查詢是區(qū)間和 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]; // 離散化數(shù)組,y和x好像有點(diǎn)抽象 }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 ++) // 遍歷每個(gè)點(diǎn)算答案 {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分線段樹(shù)單點(diǎn)修改代碼
#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) // 這個(gè)要判斷,因該是 又一遍開(kāi)始的時(shí)候有答案貢獻(xiàn)把,奇怪; 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; }總結(jié)
以上是生活随笔為你收集整理的201609-5 祭坛的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 201409-5 拼图
- 下一篇: Android仿微信语音聊天demo