当前位置:网站首页>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;
}
边栏推荐
- Pat grade a 1151 LCA in a binary tree (30 points)
- ZOJ - 4114 Flipping Game-dp,合理状态表示
- 《图书馆管理系统——“借书还书”模块》项目研发阶段性总结
- JVM dynamic bytecode technology details
- Find out what happened in the process of new
- 本地缓存--Ehcache
- matlab---错误使用 var 数据类型无效。第一个输入参数必须为单精度值或双精度值
- 哪里有搭建flink cdc抽mysql数的demo?
- 自定义注解校验API参数电话号
- 2019 Shaanxi Provincial race K-variant Dijstra
猜你喜欢
随机推荐
C#精挑整理知识要点11 委托和事件(建议收藏)
Local cache --ehcache
我想问下变量配置功能是只能在SQL模式下使用吗
Get the ask code corresponding to the key pressed by the keyboard
谷歌云盘如何关联Google Colab
2021上海市赛-D-卡特兰数变种,dp
<栈模拟递归>
Flink-1.13.6版本的 Flink sql以yarn session 模式运行,怎么禁用托管
matlab---错误使用 var 数据类型无效。第一个输入参数必须为单精度值或双精度值
数据系统分区设计 - 请求路由
C#精挑整理知识要点10 泛型(建议收藏)
Games101 review: linear algebra
Pytorch学习笔记--Pytorch常用函数总结1
数据系统分区设计 - 分区再平衡(rebalancing)
User defined annotation verification API parameter phone number
2021 Shanghai sai-d-cartland number variant, DP
Delayed loading source code analysis:
Xcode添加mobileprovision证书文件报错:Xcode encountered an error
MySQL optimization summary II
MATLAB 如何生产随机复序列








