AtCoder Beginner Contest 221 E - LEQ(组合数学 树状数组)

简介: AtCoder Beginner Contest 221 E - LEQ(组合数学 树状数组)

linkkkk

题意

给出长度为n的序列a,问有多少种子序列满足首元素< =尾元素

思路:

假设现在已经确定了首元素的下标为x xx,尾元素的下标为y,那么方案数为2yx1。中间的每个元素都有选/不选两种可能性。

问题转化成了∑ 1 < = x < = y < = n 2 y − x − 1 [ a x < = a y ]

考虑每个元素的贡献,假设现在枚举到元素y,那么这个元素的贡献就是2 y − 1 ∗ ∑ ( 1 2 ) x计算完总贡献后计算该元素对后面元素的贡献,加上( 1 /2 ) y将1/2

转化为逆元,用树状数组维护。

代码:

// Problem: E - LEQ
// Contest: AtCoder - AtCoder Beginner Contest 221
// URL: https://atcoder.jp/contests/abc221/tasks/abc221_e
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;typedef unsigned long long ull;
typedef pair<ll,ll>PLL;typedef pair<int,int>PII;typedef pair<double,double>PDD;
#define I_int ll
inline ll read(){ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
#define read read()
#define rep(i, a, b) for(int i=(a);i<=(b);++i)
#define dep(i, a, b) for(int i=(a);i>=(b);--i)
ll ksm(ll a,ll b,ll p){ll res=1;while(b){if(b&1)res=res*a%p;a=a*a%p;b>>=1;}return res;}
const int maxn=4e5+7,maxm=1e6+7,mod=998244353;
int a[maxn],tr[maxn],n;
vector<int>nums;
int lowbit(int x){
  return x&-x;
}
void update(int pos,int val){
  while(pos<=n) tr[pos]=(val+tr[pos])%mod,pos+=lowbit(pos);
}
int query(int pos){
  int ans=0;
  while(pos) ans=(ans+tr[pos])%mod,pos-=lowbit(pos);
  return ans;
}
int main(){
  n=read;
  rep(i,1,n) a[i]=read,nums.push_back(a[i]);
  sort(nums.begin(),nums.end());
  nums.erase(unique(nums.begin(),nums.end()),nums.end());
  rep(i,1,n){
    a[i]=lower_bound(nums.begin(),nums.end(),a[i])-nums.begin()+1;
  }
  int ans=0,t=ksm(2,mod-2,mod);
  rep(i,1,n){
    ans=(ans+ksm(2,i-1,mod)*query(a[i])%mod)%mod;
    update(a[i],ksm(t,i,mod));
  }
  cout<<ans<<endl;
  return 0;
}


目录
相关文章
|
存储 安全 关系型数据库
什么是存储引擎
什么是存储引擎
1201 0
|
SQL Oracle 关系型数据库
Oracle-index索引解读
Oracle-index索引解读
639 0
|
2月前
|
消息中间件 NoSQL Kafka
【Kafka核心】消息投递语义、Exactly-Once实现、幂等性、事务消息
本文系统梳理Kafka消息一致性核心体系:以「不丢不重」为目标,详解At-Most-Once、At-Least-Once、Exactly-Once三类投递语义;深入剖析幂等性(单会话单分区去重)与事务机制(跨分区/跨会话原子性)的原理与配置;最终整合生产者、Broker、消费者三方协同,实现端到端Exactly-Once。附最佳实践与避坑指南。
|
5月前
|
JSON 算法 API
淘宝商品列表 API 使用指南
淘宝商品列表API(taobao.items.search)支持按关键词、价格、销量等条件检索商品,返回商品ID、标题、价格等结构化数据,适用于比价、市场分析。需注册开放平台、获取AppKey/AppSecret并实名认证。接口限100次/秒,建议先测沙箱。请求含基础参数与筛选条件,签名通过MD5加密生成。
|
人工智能 缓存 Cloud Native
DeepSeek-R1 来了,从 OpenAI 平滑迁移到 DeepSeek的方法
Higress 作为一款开源的 AI 网关工具,可以提供基于灰度+观测的平滑迁移方案。
2450 254
|
关系型数据库 PostgreSQL
PostgreSQL 计算字符串字符数函数(CHAR_LENGTH(str))和字符串长度函数(LENGTH(str))
PostgreSQL 计算字符串字符数函数(CHAR_LENGTH(str))和字符串长度函数(LENGTH(str))
3838 0
|
存储 关系型数据库 MySQL
MySQL中为什么要使用索引合并(Index Merge)?
通过这些内容的详细介绍和实际案例分析,希望能帮助您深入理解索引合并及其在MySQL中的
848 10
|
前端开发 Java 应用服务中间件
开发指南031-安装ssl证书
为增强安全性,平台可安装ssl证书。对于平台不同的组成部分需要采用不同的方式,使用不同的证书格式
|
数据采集 机器学习/深度学习 算法