当前位置:网站首页>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;
}
边栏推荐
- Qtime definition (manual waste utilization is simple and beautiful)
- C # fine sorting knowledge points 9 Set 2 (recommended Collection)
- 为什么PrepareStatement性能更好更安全?
- UIDocumentInteractionController UIDocumentPickerViewController
- Pat grade a 1152 Google recruitment (20 points)
- ML - natural language processing - Introduction to natural language processing
- JVM garbage collector details
- The difference between Apple buy in and apple pay
- 二进制补码
- Pytorch学习笔记-刘二老师RNN高级篇-代码注释及结果
猜你喜欢
SQL cultivation manual from scratch - practical part

Pat grade a 1152 Google recruitment (20 points)

ML - natural language processing - Key Technologies

ML - 语音 - 语音处理介绍

Node learning

ML - natural language processing - Basics

Pytorch学习笔记--SEResNet50搭建

Pytorch学习笔记--Pytorch常用函数总结1

小波变换--dwt2 与wavedec2

分布式原理 - 什么是分布式系统
随机推荐
JS URLEncode function
matlab randint,Matlab的randint函数用法「建议收藏」
The difference between Apple buy in and apple pay
2021上海市赛-D-卡特兰数变种,dp
Geogle Colab笔记1--运行Geogle云端硬盘上的.py文件
Flex layout
window系统黑窗口redis报错20Creating Server TCP listening socket *:6379: listen: Unknown error19-07-28
MATLAB读取显示图像时数据格式转换原因
Delayed loading source code analysis:
Is it safe to open futures online? Which company has the lowest handling charge?
UITextField的inputView和inputAccessoryView注意点
Submarine cable detector tss350 (I)
How to solve the login problem after the 30 day experience period of visual stuido2019
C#精挑整理知识要点10 泛型(建议收藏)
JVM knowledge brain map sharing
JVM parameter configuration details
2021江苏省赛A. Array-线段树,维护值域,欧拉降幂
Singleton mode 3-- singleton mode
Notes on inputview and inputaccessoryview of uitextfield
Endnote 无法编辑range 解决