当前位置:网站首页>Icpc2021 Kunming m-violence + chairman tree
Icpc2021 Kunming m-violence + chairman tree
2022-07-25 15:34:00 【Brother Tazi is here】
The main idea of the topic :
Give you a sequence . Ask one interval at a time Select or deselect each number The smallest value that cannot be added .
Topic ideas :
Consider violence : After sorting, if a prefix and < The next number - 1 Just stop .
If the value range is set to bucket , Then preprocess the prefix and . It's easy to prove that we can lognlogn Within the complexity of violence dichotomy .
It's easy to put this process on the chairman tree . Then remember to discretize the data
Complexity : O ( n l o g n l o g n ) O(nlognlogn) O(nlognlogn)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define pb push_back
#define vi vector<int>
#define vll vector<ll>
#define fi first
#define se second
#define mid ((l + r) >> 1)
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;
ll a[maxn] , b[maxn];
int rt[maxn] , ls[maxn << 5] , rs[maxn << 5] , cnt;
ll s[maxn << 5];
ll dist[maxn] , tot;
void pushup (int t){
s[t] = s[ls[t]] + s[rs[t]];
}
int add (int t , int l , int r , int p , int c){
int now = ++cnt;
ls[now] = ls[t];
rs[now] = rs[t];
s[now] = s[t];
if (l == r){
s[now] += c;
return now;
}
if (p <= mid)
ls[now] = add(ls[now] , l , mid , p , c);
else
rs[now] = add(rs[now] , mid + 1, r , p , c);
pushup(now);
return now;
}
ll ask (int t , int l , int r , int L , int R){
if (!t) return 0;
if (L <= l && r <= R) return s[t];
ll ans = 0;
if (L <= mid) ans += ask(ls[t] , l , mid , L , R);
if (R > mid) ans += ask(rs[t] , mid + 1 , r , L , R);
return ans;
}
ll getR (ll x){
ll g = upper_bound(dist + 1 , dist + 1 + tot , x) - dist;
g--;
return g;
}
ll getL (ll x){
ll g = lower_bound(dist + 1 , dist + 1 + tot , x) - dist;
return g;
}
ll calc (int l , int r , ll x , ll y){
x = getL(x);
y = getR(y);
if (x > y) return -1;
ll a = ask(rt[r] , 1 , tot , x , y);
ll b = ask(rt[l - 1] , 1 , tot , x , y);
return a - b;
}
int main()
{
int n , m; scanf("%d%d" , &n , &m);
for (int i = 1 ; i <= n ; i++) {
scanf("%lld" , a + i);
dist[++tot] = a[i];
}
sort(dist + 1 , dist + 1 + tot);
tot = unique(dist + 1 , dist + 1 + tot ) - dist - 1;
for (int i = 1 ; i <= n ; i++){
b[i] = lower_bound(dist + 1 ,dist + 1 + tot , a[i]) - dist;
rt[i] = add(rt[i - 1] , 1 , tot , b[i] , a[i]);
}
ll ans = 0;
for (int i = 1 ; i <= m ; i++){
int nl , nr; scanf("%d%d" , &nl , &nr);
int l = min((nl + ans) % n + 1 , (nr + ans) % n + 1);
int r = max((nl + ans) % n + 1 , (nr + ans) % n + 1);
ll now = 0;
vector<ll> g;
int ok = true;
int yy = 0;
while (1){
// cout << "now this range can be made:" << 0 << " " << now << endl;
yy++;
assert(yy <= 100);
now = calc(l , r , 1 , now + 1);
if (now == -1){
ok = false;
break;
}
if (g.size() && g.back() == now) break;
g.push_back(now);
}
if (!ok){
printf("%lld\n" , 1);
continue;
}
ans = g.back() + 1;
printf("%lld\n" , ans);
}
return 0;
}
边栏推荐
- 盒子躲避鼠标
- 苹果内购和Apple Pay 的区别
- 解决vender-base.66c6fc1c0b393478adf7.js:6 TypeError: Cannot read property ‘validate‘ of undefined问题
- Spark memory management mechanism new version
- Qtime definition (manual waste utilization is simple and beautiful)
- 本地缓存--Ehcache
- MySQL heap table_ MySQL memory table heap Usage Summary - Ninth Five Year Plan small pang
- Take you to learn more about JS basic grammar (recommended Collection)
- 如何解决跨域问题
- 2019陕西省省赛J-位运算+贪心
猜你喜欢

Spark memory management mechanism new version

伤透脑筋的CPU 上下文切换

Pytorch学习笔记-刘二老师RNN高级篇-代码注释及结果

盒子躲避鼠标

Box avoiding mouse

How to solve the login problem after the 30 day experience period of visual stuido2019

ML - 图像 - 深度学习和卷积神经网络

你准备好脱离“内卷化怪圈”了吗?

Take you to create your first C program (recommended Collection)

Reflection - Notes
随机推荐
Spark memory management mechanism new version
ML - natural language processing - Key Technologies
Remember that spark foreachpartition once led to oom
我想问下变量配置功能是只能在SQL模式下使用吗
UITextField的inputView和inputAccessoryView注意点
ML - 语音 - 语音处理介绍
Cf685b find the center of gravity of each subtree of a rooted tree
JVM-垃圾收集器详解
带你创建你的第一个C#程序(建议收藏)
PAT甲级1151 LCA in a Binary Tree (30 分)
自定义注解校验API参数电话号
GAMES101复习:变换
JVM parameter configuration details
Local cache --ehcache
MySQL heap table_ MySQL memory table heap Usage Summary - Ninth Five Year Plan small pang
Qtime定义(手工废物利用简单好看)
Pytorch框架练习(基于Kaggle Titanic竞赛)
pageHelper不生效,sql没有自动加上limit
分布式原理 - 什么是分布式系统
ML - 语音 - 传统语音模型