当前位置: 首页 > news >正文

P4168 蒲公英 分块

把信息全部按块处理,在本题中体现的是块内众数(p[i])和块间次数(sum[i][j])
Remarks:
1.桶暴力
2.离散化
3.前缀和

#include<bits/stdc++.h>
#define ll long long
#define maxn 40003
#define inf 1e9+2
using namespace std;
int n, m, pp, num;
int a[maxn], b[maxn], L[220], R[220], pos[maxn], color[maxn], tong[maxn], ans[maxn];
int p[220][220], sum[220][maxn];void init(int n) {int bz = sqrt(n * 1.0);num = n / bz;if (n % bz) num++;for (int i = 1; i <= num; ++i) L[i] = (i - 1) * bz + 1, R[i] = i * bz;R[num] = n;for (int i = 1; i <= num; ++i) {for (int j = L[i];  j <= R[i]; ++j) {pos[j] = i;sum[i][a[j]]++;}  }for (int i = 1; i <= num; ++i) {for (int j = 1; j <= pp; ++j) sum[i][j] += sum[i - 1][j];}for (int i = 1; i <= num; ++i) {int tot = 0, col = inf;for (int j = L[i]; j <= n; ++j) {tong[a[j]]++;if (tong[a[j]] > tot || (tong[a[j]] == tot && a[j] < col)) col = a[j], tot = tong[a[j]];p[i][pos[j]] = col;}for (int j = L[i]; j <= n; ++j) tong[a[j]]=0;}		
} int query(int l, int r) {memset(tong, 0, sizeof(tong));int p1 = pos[l], p2 = pos[r];int tot = 0, col = inf;if (p2 - p1 <= 1) {for (int j = l;  j <= r; ++j) {tong[a[j]]++;if (tong[a[j]] > tot || (tong[a[j]] == tot) && a[j] < col) tot = tong[a[j]], col = a[j];}return col;}int ans = p[p1 + 1][p2 - 1];tot = sum[p2 - 1][ans] - sum[p1][ans];int x = l, y = L[p1 + 1] - 1;for (int i = x; i <= y; ++i) {++tong[a[i]];if (tong[a[i]] + sum[p2 - 1][a[i]] - sum[p1][a[i]] > tot || (tong[a[i]] + sum[p2 - 1][a[i]] - sum[p1][a[i]] == tot && ans > a[i])) tot = tong[a[i]] + sum[p2 - 1][a[i]] - sum[p1][a[i]], ans = a[i];}x = L[p2], y = r;for (int i = x; i <= y; ++i) {++tong[a[i]];if (tong[a[i]] + sum[p2 - 1][a[i]] - sum[p1][a[i]] > tot || (tong[a[i]] + sum[p2 - 1][a[i]] - sum[p1][a[i]] == tot && ans > a[i])) tot = tong[a[i]] + sum[p2 - 1][a[i]] - sum[p1][a[i]], ans = a[i];}return ans;
}int main() {cin>>n>>m;for (int i = 1; i <= n; ++i) cin>>a[i];for (int i = 1; i <= n; ++i) b[i] = a[i];sort(b + 1, b + n + 1);pp = unique(b + 1, b + n + 1) - b - 1;for (int i = 1; i <= n; ++i) {int col = a[i];a[i] = lower_bound(b + 1, b + 1 + pp, a[i]) - b;color[a[i]] = col;} init(n);for (int i = 1; i <= m; ++i) {int l, r; cin>>l>>r;int x = (l + ans[i - 1] - 1) % n + 1;int y = (r + ans[i - 1] - 1) % n + 1;if (x > y) swap(x, y);ans[i] = color[query(x, y)];cout<<ans[i]<<endl;}return 0;
}
http://www.gsyq.cn/news/75581.html

相关文章:

  • 2025防静电吸盘厂家哪家好?海绵吸盘定制厂家实力榜单
  • 2025真空吸盘定制厂家哪家好?优质真空吸盘厂家精选指南
  • 2025年口碑好的抽条韩国绒厂家推荐及采购指南
  • 2025激光切管机哪家好?激光切管机品牌推荐榜单
  • 广东科技项目申报咨询机构哪家好?2025综合实力榜单
  • 2025美国整柜门到门货代攻略:美国整柜门到门物流公司合集
  • 2025联轴器厂家哪家好?工业级联轴器优质厂家汇总
  • 100kW微型燃气轮机Simulink建模探索 - 详解
  • D. Taxes
  • 自动码垛包装机厂家哪家好?2025包装机械厂家实力榜单
  • 2025年不锈钢衣柜加盟五大靠谱品牌推荐,专业代理合作分析与
  • 2025年度实验室反应釜服务商TOP5权威推荐:定制化解决方
  • 2025年口碑好的沙发金钻绒/素色金钻绒厂家选购指南与推荐
  • Cisco Firepower 1000 Series FTD Software 10.0.0 发布 - 思科下一代防火墙系统软件
  • Cisco Firepower 9300 Series FTD Software 10.0.0 发布 - 思科下一代防火墙系统软件
  • 2025年热门的动力配电箱/工地配电箱厂家最新TOP排行榜
  • 2025年行业内较好的胶合建筑模板厂家推荐及选购参考榜
  • 基于时间的 SQL 盲注-延时判断和基于布尔的 SQL 盲注 - 指南
  • 2025年知名的实验用平板硫化机厂家实力及用户口碑排行榜
  • 2025年质量好的高性价比全品类五金厂家最新权威实力榜
  • 从人脑神经元到PyTorch深度神经网络:感知机、多层感知器与卷积神经网络的通俗解读
  • 学烹饪哪学校好?重庆新东方烹饪职业学校教学模式先进
  • 2025年质量好的大型空压机厂家最新权威推荐排行榜
  • 2025年热门的耐乙醇涂料/耐溶剂涂料厂家推荐及选择指南
  • 从人脑神经元到MindSpore深度神经网络:感知机、多层感知器与卷积神经网络的通俗解读
  • 2025年口碑好的烟台礼盒包装厂家推荐及选择指南
  • 2025年比较好的窗帘面料/枕套面料热门厂家推荐榜单
  • 2025年评价高的聚氨酯发泡保温管道厂家最新TOP实力排行
  • 别再把DevOps当软件!90%企业都在做假DevOps,文化才是根本,很多团队都搞反了!
  • 2025年热门的不锈钢保温提锅/不锈钢餐具实力厂家TOP推荐榜