当前位置:网站首页>Joseph sequence segment tree o (nlogn)
Joseph sequence segment tree o (nlogn)
2022-04-23 06:46:00 【Round moon】
Joseph sequence
At present, the most common Joseph solutions are implemented through violent simulation . Even python The complexity of array slicing is far less than O(nlogn) If the situation is bad enough , Then the complexity of slicing can reach O(n^2) It's no different from violence .
Then we use the line segment tree to count the number , To record the subscript . In this way, you can quickly query .
Accepted Code
//#include<unordered_map>
#include<algorithm>
#include<iostream>
#include<string.h>
#include <iomanip>
#include<stdio.h>
#include<vector>
#include<string>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll ll_inf = 9223372036854775807;
const int int_inf = 2147483647;
const short short_inf = 32767;
const ll less_inf = 0x3f3f3f3f;
const char char_inf = 127;
#pragma GCC optimize(2)
#define accelerate cin.tie(NULL);cout.tie(NULL);ios::sync_with_stdio(false);
#define PI 3.141592653589793
#define EPS 1.0e-8
ll gcd(ll a, ll b) {
return b ? gcd(b, a % b) : a;
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
inline ll read() {
ll c = getchar(), Nig = 1, x = 0;
while (!isdigit(c) && c != '-')c = getchar();
if (c == '-')Nig = -1, c = getchar();
while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
return Nig * x;
}
inline void out(ll a) {
if (a < 0)putchar('-'), a = -a;
if (a > 9)out(a / 10);
putchar(a % 10 + '0');
}
ll qpow(ll x, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)res = (res * x) % mod;
x = (x * x) % mod;
n >>= 1;
}
return res;
}
#define read read()
const int N = 1e5 + 7;
ll tree[N * 4];
void push_up(int rt)
{
tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
}
int Get_id(int val, int l, int r, int rt)
{
if (l == r)return l;
int mid = l + r >> 1;
if (val <= tree[rt << 1])return Get_id(val, l, mid, rt << 1);
return Get_id(val - tree[rt << 1], mid + 1, r, rt << 1 | 1);
}
void update(int pos, int l, int r, int rt)
{
if (l == r)tree[rt] = 0;
else
{
int mid = l + r >> 1;
if (pos <= mid)update(pos, l, mid, rt << 1);
else update(pos, mid + 1, r, rt << 1 | 1);
push_up(rt);
}
}
void creat(int l, int r, int rt)
{
if (l == r)
{
tree[rt] = 1;
return;
}
int mid = l + r >> 1;
creat(l, mid, rt << 1);
creat(mid + 1, r, rt << 1 | 1);
push_up(rt);
}
queue<int>q;
int main()
{
int n = read, m = read;
creat(1, n, 1);
int now = 0;
while (tree[1])
{
now = (now + m) % tree[1];
if (now == 0)now = tree[1];
int pos = Get_id(now, 1, n, 1);
update(pos, 1, n, 1);
now--;
q.push(pos);
}
out(q.front());
q.pop();
while (!q.empty())
{
putchar(' ');
out(q.front());
q.pop();
}
puts("");
}
By-Round Moon
版权声明
本文为[Round moon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230549499086.html
边栏推荐
- [UDS unified diagnosis service] i. diagnosis overview (3) - ISO 15765 architecture
- 类和对象
- C语言实用小技巧合集(持续更新)
- Generate shortcut
- cv_bridge 与opencv 版本不匹配的解决
- C语言进阶要点笔记5
- Camera calibration: key point method vs direct method
- 【UDS统一诊断服务】(补充)五、ECU bootloader开发要点详解 (2)
- [ThreadX] h743zi + lan8720 + ThreadX + netx duo transplantation
- _findnext 报错
猜你喜欢

【UDS统一诊断服务】(补充)五、ECU bootloader开发要点详解 (2)
![[UDS] unified diagnostic service (UDS)](/img/ed/8c16e4f1136fff95a829be410cab11.png)
[UDS] unified diagnostic service (UDS)
![[UDS unified diagnostic service] II. Network layer protocol (2) - data transmission rules (single frame and multi frame)](/img/4f/315a9b4cd85ebaad39cfa985dea45b.png)
[UDS unified diagnostic service] II. Network layer protocol (2) - data transmission rules (single frame and multi frame)

cv_bridge 与opencv 版本不匹配的解决

【UDS统一诊断服务】五、诊断应用示例:Flash Bootloader
VHDL-任意分频器(50%占空比)

基于SSD的物体检测案例实现
![[UDS unified diagnosis service] IV. typical diagnosis service (1) - diagnosis and communication management function unit](/img/4f/7ca6505b545fb825b0dba36f474da7.png)
[UDS unified diagnosis service] IV. typical diagnosis service (1) - diagnosis and communication management function unit

【UDS统一诊断服务】一、诊断概述(2)— 主要诊断协议(K线和CAN)

Initialization of classes and objects (constructors and destructors)
随机推荐
类和对象
[UDS unified diagnostic service] IV. typical diagnostic service (2) - data transmission function unit
Using printf in MFC
Shell脚本 &&和||的使用
ES6
cv_bridge 与opencv 版本不匹配的解决
JS高频面试题
带默认模板实参的类模板与模板模板形参的匹配
[UDS] unified diagnostic service (UDS)
FOC 单电阻采样 位置环控制伺服电机
ES6面试题(参考文档)
【UDS统一诊断服务】四、诊断典型服务(1)— 诊断和通信管理功能单元
Camera calibration: key point method vs direct method
HDU-Memory Control
[opencv] use filestorage to read and write eigenvectors
修改注册表的值
Detailed explanation and application principle of token
copy constructor
浮点数双精度,单精度以及半精度知识总结
WMI技术介绍和应用