首页 > 其他分享 >P3332 K大数查询 题解

P3332 K大数查询 题解

时间:2024-10-07 09:11:02浏览次数:1  
标签:P3332 大数 int 题解 mid back tim vector push

Solution

整体二分板子题

vector 太好写了111

#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define reo(i, j, k) for (int i = (j); i >= (k); --i)
typedef long long ll;
const int N = 50010;
int n, m, ans[N];
struct Modify {
	int tim, l, r, c;
};
struct Query {
	int tim, l, r, id;
	ll k;
};
vector<Modify> Ms;
vector<Query> Qs;

#define lc (u << 1)
#define rc ((u << 1) | 1)
#define mid ((l + r) >> 1)

ll sum[N << 2], add[N << 2];
int len[N << 2];
void up(int u) {
	sum[u] = sum[lc] + sum[rc];
}
void Add(int u, ll v) {
	add[u] += v, sum[u] += 1ll * len[u] * v;
}
void down(int u) {
	if (add[u]) Add(lc, add[u]), Add(rc, add[u]), add[u] = 0;
}
void Build(int u, int l, int r) {
	len[u] = r - l + 1;
	if (l == r) return;
	Build(lc, l, mid), Build(rc, mid + 1, r);
}
void Upd(int u, int l, int r, int x, int y, ll v) {
	if (y < l || r < x || x > y) return;
	if (x <= l && r <= y) return Add(u, v);
	down(u), Upd(lc, l, mid, x, y, v), Upd(rc, mid + 1, r, x, y, v), up(u);
}
ll Qry(int u, int l, int r, int x, int y) {
	if (y < l || r < x || x > y) return 0ll;
	if (x <= l && r <= y) return sum[u];
	return down(u), Qry(lc, l, mid, x, y) + Qry(rc, mid + 1, r, x, y);
}

#undef lc
#undef rc
#undef mid

void Solve(int l, int r, vector<Modify> &M, vector<Query> &Q) {
	if (l == r) {
		for (auto p : Q)
			ans[p.id] = l - n - 1;
		return;
	}
	int mid = (l + r) >> 1, x = mid - n - 1;
	vector<Modify> mL, mR;
	vector<Query> qL, qR;
	int Msize = M.size(), Qsize = Q.size();
	for (int i = 0, j = 0; j < Qsize || i < Msize; ) {
		if (i < Msize && (j >= Qsize || M[i].tim < Q[j].tim)) {
			if (M[i].c <= x) {
				mL.push_back(M[i]);
			} else {
				Upd(1, 1, n, M[i].l, M[i].r, 1);
				mR.push_back(M[i]);
			}
			++i;
		} else {
			ll cnt = Qry(1, 1, n, Q[j].l, Q[j].r);
			if (Q[j].k > cnt) {
				Q[j].k -= cnt;
				qL.push_back(Q[j]);
			} else {
				qR.push_back(Q[j]);
			}
			++j;
		}
	}
	for (auto p : M)
		if (p.c > x) Upd(1, 1, n, p.l, p.r, -1);
	Solve(l, mid, mL, qL), Solve(mid + 1, r, mR, qR);
}

int main() {
	ios::sync_with_stdio(false), cin.tie(nullptr);
	cin >> n >> m;
	int tot = 0;
	rep(i, 1, m) {
		int op, a, b, c;
		cin >> op >> a >> b >> c;
		if (op == 1) Ms.push_back((Modify){i, a, b, c});
		if (op == 2) Qs.push_back((Query){i, a, b, ++tot, c});
	}
	Build(1, 1, n);
	Solve(1, 2 * n + 1, Ms, Qs);
	rep(i, 1, tot) cout << ans[i] << '\n';
	return 0;
}

标签:P3332,大数,int,题解,mid,back,tim,vector,push
From: https://www.cnblogs.com/laijinyi/p/18449764

相关文章

  • P4093 序列 题解
    Statement给出\(n\) 个数的序列\(\{a_i\}\),接下来\(m\)秒中每一秒会有一个数发生变化,然后恢复。问最长的子序列长度,使得任意时刻这个子序列不下降。\(n\le10^5\)Solution设\(b_i\)为\(i\)最小能变成的数,\(c_i\)为\(i\)最大能变成的数\[f(i)=\max_{j<i\landc......
  • P4690 镜中的昆虫 (动态区间颜色数) 题解
    Statement区间涂颜色区间颜色数Solution\(O(\text{polysqrt})\)略。\(O(\text{polylog})\)颜色段均摊有两层含义:随机数据下:任意时刻的颜色段个数期望\(O(\logn)\)非随机数据下:每次推平时访问的颜色段个数均摊\(O(n)\)首先维护每个点\(i\)的\(pre_i\),一次询......
  • 智慧农业大数据平台解决方案
    来源:城市大脑解决方案......
  • 中国农业大学公布2024级研究生新生大数据
    来源:中国农大......
  • Codeforces Rund 977 div2 个人题解(A~E1)
    CodeforcesRund977div2个人题解(A,B,C1,C2,E1)Dashboard-CodeforcesRound977(Div.2,basedonCOMPFEST16-FinalRound)-Codeforces火车头#define_CRT_SECURE_NO_WARNINGS1#include<algorithm>#include<array>#include<bitset>#includ......
  • 【题解】C - STEP
    目录题目内容DescriptionInputOutput数据规模与约定做法一思路代码做法2思路代码题目内容原题:洛谷P6492Description给定一个长度为\(n\)的字符序列\(a\),初始时序列中全部都是字符L。有\(q\)次修改,每次给定一个\(x\),若\(a_x\)为L,则将\(a_x\)修改成R,否则将\(a_x\)......
  • Redis终极入门指南:万字解析帮你从零基础到掌握命令与五大数据结构
    目录命令学习:一、Redis基础操作二、Redis常用命令三、五种数据结构及其常用命令3.1String(字符串)3.2List(列表)3.3Set(集合)3.4Hash(哈希)3.5Zset(有序集合) 前言:  Redis是一款开源内存数据库,以高性能和多样数据结构广泛应用于缓存和消息队列等场景。本文为新......
  • 常见问题解决 --- maven手动安装依赖jar包报错
    报错内容:执行命令mvninstall:install-file-DgroupId=com.beidouapp-DartifactId=SSDK-Dversion=4.0.2.0 -Dfile=C:\1\SSDK-Release-4.0.2.0.jar-Dpackaging=jar报错Unknownlifecyclephase“.ggstar“.Youmustspecifyavalidlifecyclephaseoragoal原因:在pow......
  • CCF-CSP认证资格考试题解系列——第4次第2题数字排序
    #include<iostream>#include<algorithm>usingnamespacestd;structre{ intvalue;//数值 intnum;//次数}re[1010];boolcmp(structrea,structreb){ if(a.num==b.num)returna.value<b.value;//次数相同是小的优先 returna.num>b.num;//次数不相同是次数优......
  • CCF-CSP认证资格考试题解系列——第4次第3题节日
    #include<iostream>usingnamespacestd;intm[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};intis_run(intyear){ if(year%400==0||(year%4==0&&year%100))return1; return0;}intgetdays(intyear,intmonth){ if(month==2)returnm[month]+i......