首页 > 其他分享 >NOIP2023模拟13联测34 B.competition

NOIP2023模拟13联测34 B.competition

时间:2023-11-07 21:45:13浏览次数:29  
标签:13 ch int LL competition 34 联测 mod

NOIP2023模拟13联测34 B.competition

目录

题目大意

现在有 \(n\) 个区间 \([l_i , r_i]\) ,现在问你选取若干的连续的区间的区间并的大小的和。

思路

设 \(pre_{i , j}\) 表示前 \(i - 1\) 个区间内,包含点 \(j\) 的最靠右的数是多少。

可以发现答案就是

\[\sum_{i = 1}^n (r_i - l_i +1) * i * (n - i + 1) - pre_{i , j} * (n - i +1) \]

也就是这个区间被记入答案的次数乘上区间的大小再减去重复的次数

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

先统计答案,然后用线段树更新 \(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;
}

标签:13,ch,int,LL,competition,34,联测,mod
From: https://www.cnblogs.com/2020fengziyang/p/17816110.html

相关文章

  • NOIP2023模拟13联测34 A. origen
    NOIP2023模拟13联测34A.origen目录NOIP2023模拟13联测34A.origen题目大意思路code题目大意给定\(n\)个整数\(a_1,a_2,a_3\cdotsa_n\),求\[\sum_{i=1}^n\sum_{j=i}^n(\oplus_{k=i}^ja_k)^2\mod998244353\]\(n\le2*10^5,0\lea_i\le2*10^5\)思路设......
  • NOIP 模拟13(NOIP A层联测26)
    100+100+20+17,T3按理说应该想到考虑两部分分别的贡献的,明明这个套路很常见。5k:就喜欢这种数据结构专场,多来点。A.origen先前缀和,以下\(p_i\)表示前缀异或和。考虑将一个数\(k\)二进制差分,假设拆成\(2^a+2^b+2^c\),则\(k^2=(2^a+2^b+2^c)\times(2^a+2^b+2^c)\),也就是指数......
  • CF1359D Yet Another Yet Another Task
    貌似没有线段树做法。记\(s\)为\(a\)的前缀和数组。对于一个确定的右端点\(r\)和左端点\(l\),它对于答案的贡献是\(s_r-s_{l-1}-max\{a_i\},l\lei\ler\),如果枚举右端点,令\(c_l=s_{l-1}+max\{a_i\},l\lei\)。那么其实就是要求\(1\lek\ler-1\)的\(min\{c_k\}\)。线......
  • mac os13上安装apache\php\mysql
    macos13上安装1,下载并安装brew,brew是macos上的软件安装工具;2,安装apache2brewinstallhttpd 安装成功后提示:工程文件根目录DocumentRootis/usr/local/var/www配置文件Thedefaultportshavebeensetin/usr/local/etc/httpd/httpd.confto8080andin/usr/local/e......
  • [洛谷 P3481] [BZOJ1118] [POI2009] PRZ-Algorithm Speedup
    题目描述你需要计算一个函数\(F(x,y)\),其中\(x,y\)是两个正整数序列。boolF(std::vector<int>x,std::vector<int>y){if(W(x).size()!=W(y).size())returnfalse;if(W(x).size()==1)returntrue;returnF(p(x),p(y))&&F(s(x),s(y));}\(W......
  • 文心一言 VS 讯飞星火 VS chatgpt (130)-- 算法导论11.2 2题
    二、用go语言,对于一个用链接法解决冲突的散列表,说明将关键字5,28,19,15,20,33,12,17,10插入到该表中的过程。设该表中有9个槽位,并设其散列函数为h(k)=kmod9。文心一言:在这个问题中,我们使用Go语言来解决一个散列(Hash)表的问题,该问题需要我们使用链接法解决冲突。我们的散......
  • CTFshow Reverse BJDCTF2020 hamburger_competition wp
    游戏主函数在Assembly-CSharp.dll,.net写的用dnSpy打开打到主函数elseif(name=="汉堡顶"&&Init.spawnCount==5){Init.secret^=127;stringstr=Init.secret.ToString();if(ButtonSpawnFruit.Sha1(str)=="DD01903921EA24941C26A48F2CEC24E0......
  • IBM System x3400 怎么启动到可以安装?
    IBM服务器安装Windows操作系统的前提条件:IBMServerGuide光盘和Windows光盘。 IBMSystemX3400理论上不支持WindowsServer2000任何一个版本的安装(主要是因为ServerGuide光盘里面没有相关的驱动,在系统安装初始阶段无法加载阵列卡控制器的驱动)。但是你可以使用一个外置的USB......
  • USB转串口芯片对比选秀---推荐CP2102和CH340C
    参考应用文章:《USB转串口芯片你看好哪个(USB转串口芯片介绍)》简短不看版:建议选择这2款芯片:CP2102/CP2104和CH340C。稳定性较好。 1.FT232优势:最常用缺点:假货多,并不是不能用,而是稳定性差。串口容易丢。规格书:https://atta.szlcsc.com/upload/public/pdf/source/20130221/14......
  • day1311
    后缀分类动词后缀-ize(做成;变成;..........化)modernize(现代化)-en(使成为;引起)quicken(加快)-fy(使........化;使成)purify(净化)-ish(使;令)abolish(取消)-ate(成为........;处理)indicate(指示)形容词后缀-able/-ibleflexible(可弯......