ZR1153

首先我们可以发现一个比较简单的容斥做法

直接暴力枚举\(2^m\)个限制强制不合法,算贡献

注意如果两个限制冲突那么答案为0

直接暴力差分就好了

这样就有了快乐的\(30\)分了

接下来考虑对容斥进行DP

把所有点区间按照右端点排序,如果出来两个颜色相同的区间一个包含了另外一个,那么大区间是没有用的,因为小区间满足条件大区间一定满足

我们设\(f_{i}\)表示满足第\(i\)个限制的带容斥系数的方案数

那么转移我们就枚举上一个没有交的区间

\[f_{i} =g_{;_i -1} + \sum_{co_i = co_j,l_j\ge r_i}f_j
\]

其中\(g_x\)表示\(1-x\)位置满足\(1-x\)的所有容斥之后的限制的前缀和

也就是说

\[g_i = g_{i - 1}\times s + \sum_{r_j = i}f_j
\]

就是看看第\(i\)位置上的限制满足还是不满足综合考虑的前缀和

继续回到求\(f_i\)的式子

既然\(i\)的这个限制要容斥,那么强制他不被满足,前面就是对所有和他没有交的限制求一个总的容斥

后面算有交的部分的贡献,必须满足和当前限制的颜色相同,

我们排序之后,有交的集合是一个区间,我们二分找到对应位置维护前缀和即可

#include<cstdio>
#include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cctype>
#include<vector>
#include<ctime>
#include<cmath>
#include<set>
#include<map>
#define LL long long
#define pii pair<int,int>
#define mk make_pair
#define fi first
#define se second
using namespace std;
const int N = 4e5 + 3;
const LL mod = 998244353;
struct seg{
int li,ri;
int xi;
}a[N],b[N];
vector <pii> co[N];
vector <LL> h[N];
LL f[N],g[N];
int n,m,s,cnt;
inline int read(){
int v = 0,c = 1;char ch = getchar();
while(!isdigit(ch)){
if(ch == '-') c = -1;
ch = getchar();
}
while(isdigit(ch)){
v = v * 10 + ch - 48;
ch = getchar();
}
return v * c;
}
inline bool cmp(seg x,seg y){
return x.ri < y.ri || (x.ri == y.li && x.li > y.li);
}
inline LL find(int x,int rr){
// printf("%d %d\n",x,rr);
int l = 0,r = h[x].size() - 1,ans = -1;
if(r < 0) return 0;
while(l <= r){
int mid = (l + r) >> 1;
if(co[x][mid].se < rr) l = mid + 1,ans = mid;
else r = mid - 1;
}
// printf("%d %d %d %lld\n",l,r,ans,ans == -1 ? h[x].back() : h[x].back() - h[x][ans]);
return ans == -1 ? h[x].back() : h[x].back() - h[x][ans];
}
inline LL mo(LL x){
if(x >= mod) x-= mod;
return x;
}
int main(){
n = read(),m = read(),s = read();
for(int i = 1;i <= m;++i){
a[i].li = read();
a[i].ri = read();
a[i].xi = read();
}
sort(a + 1,a + m + 1,cmp);
for(int i = 1;i <= m;++i){
bool flag = 0;
if(!co[a[i].xi].size()) co[a[i].xi].push_back(mk(a[i].li,a[i].ri));
else{
pii x = co[a[i].xi].back();
if(x.fi >= a[i].li && x.se <= a[i].ri) flag = 1;
else co[a[i].xi].push_back(mk(a[i].li,a[i].ri));
}
if(!flag) b[++cnt] = a[i];
}
m = cnt;
memcpy(a,b,sizeof(a));
// puts("new::");
// for(int i = 1;i <= m;++i) cerr << a[i].li << " " << a[i].ri << " " << a[i].xi << endl;
// puts("next::");
int now = 1;
f[0] = g[0] = 1;
for(int i = 1;i <= m;++i){
// cerr << "dsdas::"<< a[i].li << " " << a[i].ri << " " << a[i].xi << endl;
for(;now < a[i].ri;now++) g[now] = (g[now] + g[now - 1] * s) % mod;
f[i] = (-g[a[i].li - 1] + mod);
f[i] -= find(a[i].xi,a[i].li);
if(f[i] < 0) f[i] += mod;
LL gg = h[a[i].xi].empty() ? 0 : h[a[i].xi].back();
h[a[i].xi].push_back(mo(gg + f[i]));
g[a[i].ri] = mo(g[a[i].ri] + f[i]);
}
for(;now <= n;++now) g[now] = (g[now] + g[now - 1] * s) % mod;
printf("%lld\n",g[n]);
return 0;
}

ZR1153的更多相关文章

随机推荐

  1. TRS OM error

    http://192.168.1.1/ http://tplogin.cn/admin888 wddqaz123456789 package="com.trs.om.bean" m ...

  2. Tips:取消UICollectionView的隐式动画

    http://www.cocoachina.com/ios/20151204/14211.html UICollectionView在reloadItems的时候,默认会附加一个隐式的fade动画,有 ...

  3. day39-Spring 19-今天的内容总结

  4. day39-Spring 13-Spring的JDBC模板:默认连接池的配置

    Spring内置的连接池DriverManagerDataSource的源码. /* * Copyright 2002-2008 the original author or authors. * * ...

  5. python 只导入某个对象

  6. qbao

    # -*- coding: utf-8 -*- import Image, cStringIO, webbrowser, re, time, math import urllib, urllib2, ...

  7. Please upgrade the installed version of powershell to the minimum required version and run the command again.

    版权声明:本文为博主原创文章,转载请注明出处.谢谢 https://blog.csdn.net/cow66/article/details/77993908 我的系统是windows 7 安装了vag ...

  8. 【时光回溯】【JZOJ3568】【GDKOI2014】小纪的作业题

    题目描述 输入 输出 有M行,每个询问一行,输出结果mod 1,000,000,007的值. 样例输入 10 3 3 5 1 2 3 1 3 5 2 1 7 9 3 9 2 3 样例输出 10 19 ...

  9. java把Word文件转成html的字符串返回出去

    1.需求是把前端上传的word文件解析出来,生成html的字符串返回给前端去展示,Word里面的图片可以忽略不显示,所以这段代码去掉了解析图片的代码 package com.lieni.core.ut ...

  10. qt 自定义窗口绘制正弦曲线

    circlewidget.h #ifndef CIRCLAWIDGET_H #define CIRCLAWIDGET_H #include <QFrame> #include<QVe ...