New task CodeForces - 788E (线段树优化dp)
比较套路的一个题, 对每个数维护一颗线段树来转移就好了.
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc tr[o].l
#define rc tr[o].r
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 1e5+10;
int n, m, ans, tot, T[N];
int a[N], s[N], b[N], c[N], L[N], R[N];
int sub(int x, int y) {int r=x-y;if(r<0)r+=P;return r;}
int add(int x, int y) {int r=x+y;if(r>=P)r-=P;return r;}
int mul(int x, int y) {return (ll)x*y%P;}
struct _ {
int A,C,S,AB,BC,ABC;
_ () {}
_ (int A, int C, int S) : A(A),C(C),S(S),AB(0),BC(0),ABC(0) {}
_ operator + (const _ &rhs) const {
_ r;
r.A = add(A,rhs.A);
r.C = add(C,rhs.C);
r.S = add(S,rhs.S);
r.AB = add(add(AB,rhs.AB),mul(A,rhs.S));
r.BC = add(add(BC,rhs.BC),mul(S,rhs.C));
r.ABC = add(add(ABC,rhs.ABC),add(mul(AB,rhs.C),mul(A,rhs.BC)));
return r;
}
};
struct {int l,r;_ v;} tr[N<<5]; void ins(int &o, int l, int r, int x, int v1, int v2, int v3) {
if (!o) o=++tot;
if (l==r) {
tr[o].v=_(v1,v2,v3),void();
return ;
}
if (mid>=x) ins(ls,x,v1,v2,v3);
else ins(rs,x,v1,v2,v3);
tr[o].v=tr[lc].v+tr[rc].v;
}
int main() {
scanf("%d", &n);
REP(i,1,n) scanf("%d", a+i),b[i]=a[i];
sort(b+1,b+1+n),*b=unique(b+1,b+1+n)-b-1;
REP(i,1,n) {
a[i] = lower_bound(b+1,b+1+*b,a[i])-b;
for (int j=a[i]; j; j^=j&-j) L[i]+=c[j];
for (int j=a[i]; j<=*b; j+=j&-j) ++c[j];
}
memset(c,0,sizeof c);
PER(i,1,n) {
for (int j=a[i]; j; j^=j&-j) R[i]+=c[j];
for (int j=a[i]; j<=*b; j+=j&-j) ++c[j];
}
REP(i,1,n) {
ans = sub(ans,tr[T[a[i]]].v.ABC);
ins(T[a[i]],1,n,i,L[i],R[i],1);
ans = add(ans,tr[T[a[i]]].v.ABC);
}
scanf("%d", &m);
REP(i,1,m) {
int op, x;
scanf("%d%d", &op, &x);
ans = sub(ans,tr[T[a[x]]].v.ABC);
ins(T[a[x]],1,n,x,(op==2)*L[x],(op==2)*R[x],op==2);
ans = add(ans,tr[T[a[x]]].v.ABC);
printf("%d\n", ans);
}
}
New task CodeForces - 788E (线段树优化dp)的更多相关文章
- D - The Bakery CodeForces - 834D 线段树优化dp···
D - The Bakery CodeForces - 834D 这个题目好难啊,我理解了好久,都没有怎么理解好, 这种线段树优化dp,感觉还是很难的. 直接说思路吧,说不清楚就看代码吧. 这个题目转 ...
- Linear Kingdom Races CodeForces - 115E (线段树优化dp)
大意: n条赛道, 初始全坏, 修复第$i$条花费$a_i$, m场比赛, 第$i$场比赛需要占用$[l_i,r_i]$的所有赛道, 收益为$w_i$, 求一个比赛方案使得收益最大. 设$dp[i]$ ...
- Codeforces 1603D - Artistic Partition(莫反+线段树优化 dp)
Codeforces 题面传送门 & 洛谷题面传送门 学 whk 时比较无聊开了道题做做发现是道神题( 介绍一种不太一样的做法,不观察出决策单调性也可以做. 首先一个很 trivial 的 o ...
- Codeforces Round #426 (Div. 2) D 线段树优化dp
D. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...
- BZOJ2090: [Poi2010]Monotonicity 2【线段树优化DP】
BZOJ2090: [Poi2010]Monotonicity 2[线段树优化DP] Description 给出N个正整数a[1..N],再给出K个关系符号(>.<或=)s[1..k]. ...
- [AGC011F] Train Service Planning [线段树优化dp+思维]
思路 模意义 这题真tm有意思 我上下楼梯了半天做出来的qwq 首先,考虑到每K分钟有一辆车,那么可以把所有的操作都放到模$K$意义下进行 这时,我们只需要考虑两边的两辆车就好了. 定义一些称呼: 上 ...
- 【bzoj3939】[Usaco2015 Feb]Cow Hopscotch 动态开点线段树优化dp
题目描述 Just like humans enjoy playing the game of Hopscotch, Farmer John's cows have invented a varian ...
- POJ 2376 Cleaning Shifts (线段树优化DP)
题目大意:给你很多条线段,开头结尾是$[l,r]$,让你覆盖整个区间$[1,T]$,求最少的线段数 题目传送门 线段树优化$DP$裸题.. 先去掉所有能被其他线段包含的线段,这种线段一定不在最优解里 ...
- 洛谷$P2605\ [ZJOI2010]$基站选址 线段树优化$dp$
正解:线段树优化$dp$ 解题报告: 传送门$QwQ$ 难受阿,,,本来想做考试题的,我还造了个精妙无比的题面,然后今天讲$dp$的时候被讲到了$kk$ 先考虑暴力$dp$?就设$f_{i,j}$表示 ...
随机推荐
- Balloons
题目链接:http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=2401 类似求连通块的问题,可以参考紫书(P162 油田),对这两个人分别执行dfs. ...
- Docker 开发概述
This page lists resources for application developers using Docker. Develop new apps on Docker If you ...
- Kubernetes工作流之Pods二
Init Containers This feature has exited beta in 1.6. Init Containers can be specified in the PodSpec ...
- Wijmo 2017 V1发布
2017年Wijmo的第1个Release已经发布了!它充满了令人兴奋的新控件和新功能.一个新的TreeView控件:一个只有看到你才会相信的MultiAutoComplete控件:移动平台报表查看器 ...
- 在centos6中编译安装httpd-2.4/搭建LAMP
首先确保centos6安装平台安装一下环境: #yum groupinstall "Development Tools" "Server Platform Develo ...
- JMeter中关于动态切换不同CSV文件解决方案
最近写case,需要当前播放节目的数据作为输入数据,所以每个时刻所用的数据只能是当前时刻附件的数据,尝试用CSV Data Set Config动态加载不同的文件,没有成功,好像CSV Data Se ...
- Python 安装 lxml 插件
1.下载 lxml 地址:https://pypi.python.org/pypi/lxml/3.8.0#downloads 我用的是python 3.6,我下载了 lxml-3.8.0-cp36- ...
- Linux Shell脚本攻略:shell中各种括号()、(())、[]、[[]]、{}的作用
技巧小结: 字符串比较用双中括号[[ ]]:算数比较用单中括号[ ]——左右留空格 算数运算用双小括号(( )) :shell命令及输出用小括号( )——左右不留空格 快速替换用花括号{ }——左右留 ...
- 理解 Redis(4) - 关于 string 的更多命令(SETEX, SETNX, INCR, DECR, MSET...)
上一节介绍了关于字符串值的一些基本命令, 这一节将介绍一些进阶命令: 清理终端: 127.0.0.1:6379> clear 设置一个键值对, 同时设置过期时间为10秒: 127.0.0.1:6 ...
- _itemmod_extract_enchant随机附魔提取
技能 脚本 spell_extract_enchant 提取一条随机FM 随机FM提取 物品脚本:1.spell_extract_enchant 提取一条随机FM2.spell_extract_enc ...