NOIP2023模拟13联测34 B.competition

编程入门 行业动态 更新时间:2024-10-18 14:22:31

NOIP2023模拟13<a href=https://www.elefans.com/category/jswz/34/1761362.html style=联测34 B.competition"/>

NOIP2023模拟13联测34 B.competition

NOIP2023模拟13联测34 Bpetition

文章目录

  • NOIP2023模拟13联测34 Bpetition
    • 题目大意
    • 思路
    • code

题目大意

现在有 n n n 个区间 [ l i , r i ] [l_i , r_i] [li​,ri​] ,现在问你选取若干的连续的区间的区间并的大小的和。

思路

设 p r e i , j pre_{i , j} prei,j​ 表示前 i − 1 i - 1 i−1 个区间内,包含点 j j j 的最靠右的数是多少。

可以发现答案就是
∑ i = 1 n ( r i − l i + 1 ) ∗ i ∗ ( n − i + 1 ) − p r e i , j ∗ ( n − i + 1 ) \sum_{i = 1}^n (r_i - l_i +1) * i * (n - i + 1) - pre_{i , j} * (n - i +1) i=1∑n​(ri​−li​+1)∗i∗(n−i+1)−prei,j​∗(n−i+1)
也就是这个区间被记入答案的次数乘上区间的大小再减去重复的次数

可以用一棵线段树维护加离散化来维护。

先统计答案,然后用线段树更新 p r e pre pre

要卡常

code

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 1e6 + 5;
const LL mod = 1e9 + 7; 
int n , m , re1;
LL l[N] , r[N] , re[N << 2] , tr[N << 4] , lzy[N << 4] , ans;
unordered_map<LL , int> id;
inline void pushdown (int p , int l , int r) {if (!lzy[p]) return;int mid = l + r >> 1;tr[p << 1] = (re[mid + 1] - re[l] + mod) % mod * lzy[p] % mod;tr[p << 1 | 1] = (re[r + 1] - re[mid + 1] + mod) % mod * lzy[p] % mod;lzy[p << 1] = lzy[p << 1 | 1] = lzy[p];lzy[p] = 0;
}
inline LL Mod (LL x) { return x >= mod ? x - mod : x; }
inline LL qc (int p , int l , int r , int L , int R , LL x) {if (L <= l && R >= r) {LL z = tr[p];tr[p] = (re[r + 1] - re[l]) % mod * x;lzy[p] = x;return z;}else {int mid = l + r >> 1;LL z = 0;pushdown (p , l , r);if (L <= mid) z += qc (p << 1 , l , mid , L , R , x);if (mid < R) z += qc (p << 1 | 1 , mid + 1 , r , L , R , x);tr[p] = tr[p << 1] + tr[p << 1 | 1];return z;}
}
inline LL ksm (LL x , LL y) {LL z = 1;while (y) {if (y & 1) z = z * x % mod;y >>= 1;x = x * x % mod;}return z;
}
LL read () {LL val = 0;char ch = getchar ();while (ch < '0' || ch > '9') ch = getchar ();while (ch >= '0' && ch <= '9') {val = val * 10 + (ch - '0');ch = getchar ();}return val;
}
int main () {freopen ("competition.in" , "r" , stdin);freopen ("competition.out" , "w" , stdout); n = read () , m = read ();for (int i = 1 ; i <= n ; i ++) {l[i] = read () , r[i] = read ();re[++re1] = l[i];re[++re1] = r[i] + 1;}sort (re + 1 , re + re1 + 1);m = unique(re + 1 , re + re1 + 1) - re - 1;for (int i = 1 ; i <= m ; i ++) id[re[i]] = i;for (int i = 1 ; i <= n ; i ++)ans = (ans + qc (1 , 1 , m - 1 , id[l[i]] , id[r[i] + 1] - 1 , i) % mod * (n - i + 1)) % mod;ans = Mod (-ans + mod);for (int i = 1 ; i <= n ; i ++) ans = (ans + (r[i] - l[i] + 1) % mod * i % mod * (n - i + 1)) % mod;printf ("%lld" , ans * ksm ((1ll * n * (n + 1) / 2) % mod , mod - 2) % mod);return 0;
}

更多推荐

NOIP2023模拟13联测34 B.competition

本文发布于:2023-11-16 02:47:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1611666.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:联测   competition

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!