版本比较
11.22&&11.23&& 12.3 ABC #434 A-E && 12月 5-7&&14-17做题记录
正文
增加 305 行删除 139 行
3 行未改动456788910111258 行未改动7172737475757677787980808182838424 行未改动10911011111211311311411511611711811912012012112212312412512612712812912913013113213332 行未改动16616716816917017017117217317417517617617717817817918018018118218318418518618718818918919019119219319419519619719719819919920020120220311 行未改动21521621721821921922022122222342 行未改动2662672682692702702712722732742 行未改动2772782792802812822812822832842852864 行未改动2912922932942952952962972982992 行未改动30230330430530630630730830830931031131231331431531631631731831932025 行未改动3463473483493503513503513523533543553563563573583593603603613623633633643653663674 行未改动37237337437537637637737837937938038138238338438538638738738838939039139239339439539639739739839940040112 行未改动4144154164174184184194204214223 行未改动4264274284294304304314324334344354364374384394394404414414424434444454464474484494494504514524537 行未改动46146246346446546546646746846918 行未改动4884894904914924934924934944954964974984994974984995005015025032 行未改动5065075085095105105115125135145155165165175185195205215225235245255265265275285295305315325335335345355365375385395395405415425435435445455465473 行未改动55155255355455555555655755855918 行未改动5785795805815825835825835845855865875885895875885895905915925932 行未改动5965975985996006006016026036046056066066076086096106116126136146156166166176186196206216226236236246256266276286296296306316326336336346356366373 行未改动6416426436446456456466476486496 行未改动65665765865966066066166266366366466566666712 行未改动6806816826836846846856866876886 行未改动6956966976986996997007017027027037047057063 行未改动7107117127137147147157167177187167177187197207217227237247257267277277287297307317327337347357367377377387397407417427437447447457467477486 行未改动75575675775875975976076176276311 行未改动7757767777787797797807817827837847857867877877887897907917927927937947957965 行未改动80280380480580680680780880880981081181241 行未改动85485585685785885885986086186214 行未改动8778788798808818818828838848859 行未改动89589689789889989990090190290324 行未改动92892993093193293293393493593693793893994094094194294394421 行未改动9669679689699709709719729739742 行未改动97797897998098198198298398498516 行未改动100210031004100510061006100710081009101022 行未改动10331034103510361037103710381039104010411041104210431044104526 行未改动107210731074107510761076107710781079108099 行未改动1180118111821183118411841185118611871188130 行未改动全部展开131913201321132213231323132413251326132722 行未改动135013511352135313541354135513561357135835 行未改动139413951396139713981398139914001401140216 行未改动1419142014211422142314231424142514251426142714281429135 行未改动全部展开1565156615671568156915691570157115721573141 行未改动全部展开171517161717171817191719172017211722172318 行未改动174217431744174517461746174717481749175048 行未改动179918001801180218031803180418051806180744 行未改动185218531854185518561856185718581859186018611862186318641864186518661867186812 行未改动188118821883188418851885188618871888188916 行未改动19061907190819091910191019111912191319141915191619161917191819191920192119221923192419251925192619271928192974 行未改动200420052006200720082008200920102011201299 行未改动211221132114211521162116211721182119212064 行未改动218521862187218821892189219021912192219337 行未改动22312232223322342235223522362237223822399 行未改动224922502251225222532253225422552256225711 行未改动2269227022712272227322742273227422752276227722783 行未改动228222832284228522862286228722882289229029 行未改动232023212322232323242324232523262327232888 行未改动241724182419242024212421242224232424242524252426242724282429243024312432243324342434243524362437243854 行未改动249324942495249624972497249824992500250111 行未改动251325142515251625172517251825192520252122 行未改动2544254525462547254825482549255025512552254 行未改动全部展开280728082809281028112811281228132814281524 行未改动2840284128422843284428442845284628472848188 行未改动全部展开30373038303930403041304130423043304430456 行未改动30523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308118 行未改动31003101310231033082308330843085310431053106310731083087308831093110309031113112309230933113311430953115311631173097309831183119312031213122312331243125312631273128312931303131313231333100310131023134310431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703 行未改动分层图板子题::::success[代码]```cpp#include <bits/stdc++.h>#define ll long long #define ll long long#define rep(i,a,n) for(int i = (a);i<=(n);++i)#define endl '\n'
using namespace std;58 行未改动 vec[v+j*n].push_back(node(u+j*n,w)); } } ll ans = LLONG_MAX;
dij(s); for(int i = 0;i<=k;++i){ ans = min(ans,dis[t+i*n]); }// for(int j = 0;j<=k;++j){ // for(int j = 0;j<=k;++j){// for(int i = 0;i<n;++i){// cout <<dis[i+j*n]<<" ";// }// cout <<endl;24 行未改动
为了使答案尽可能的小,我们想让答案的最高位尽可能的多的为 0,
我们考虑从高位开始, 一位一位的看能不能变成 0,
这一位能变成 0,前提使所有的数的这一位都是 0,且这些数在左移的过程中不能使更高位变成了 1,
不然之前的努力就白费了。
所以代码实现:
1. 从高到低枚举每一位。 1. 从高到低枚举每一位。
2. 枚举每一个数,看这个数能不能在有限的操作次数里把这一位变成 0 且不影响前面的位置。
3. 如果所有数的这一位都可以的话,就进行下一位的操作。否则还原操作过的这些数。
::::success[代码]```cpp#include <bits/stdc++.h>#define ll long long #define ll long long#define rep(i,a,n) for(int i = (a);i<=(n);++i)#define endl '\n'#define lc (root<<1)#define rc (root<<1|1)32 行未改动 maxx = max(maxx,__lg(a[i])); } read(m); int ans = 1<<maxx; for(int bit = maxx-1;bit>=0;--bit){//从最高位开始。 for(int bit = maxx-1;bit>=0;--bit){//从最高位开始。 int tot = 0,num = 0; for(int j = 1;j<=n;++j){ cnt1[j] = cnt[j]; num+=cnt[j]; }
for(int j = 1;j<=n;++j){ int x = a[j] <<cnt1[j];//上一次操作把 a[i] 变成了 x。 int x = a[j] <<cnt1[j];//上一次操作把 a[i] 变成了 x。 int p = cnt1[j]; if(x >>bit & 1){//x 的这一位是 1。 if(x >>bit & 1){//x 的这一位是 1。 while(num+tot<=m&&__lg(x)<=maxx){ x<<=1; ++p; ++tot; if(((ans>>bit+1)|(x>>bit+1))==(ans>>bit+1)&&!(x>>bit&1)){ break; } } if(num+tot>m||__lg(x)>maxx){//不能实现,去下一位。 if(num+tot>m||__lg(x)>maxx){//不能实现,去下一位。 tot = 1e9; break; } cnt1[j] = p; } } if(tot==1e9){ ans |= (1<<bit);//这一位只能是 1 了。 ans |= (1<<bit);//这一位只能是 1 了。 }else{ for(int j = 1;j<=n;++j){//更新 a[i] 的操作状态。 for(int j = 1;j<=n;++j){//更新 a[i] 的操作状态。 cnt[j] = cnt1[j]; } } }11 行未改动然而我自己并没有看出来公差会用多少项的公式,还是看了题解。::::success[代码]```cpp#include <bits/stdc++.h>#define ll long long #define ll long long#define rep(i,a,n) for(int i = (a);i<=(n);++i)#define endl '\n'#define _x putchar('\n')using namespace std;42 行未改动 }}signed main(){ read(n);read(m);
rep(i,1,n){ read(a[i]);// int p = a[i];// for(int j = 1;j<=12;++j){2 行未改动// } } while(m--){ int l = 0,r = 0; read(l);read(r); b[l] += 1;b[r+1] -= 1; read(l);read(r); b[l] += 1;b[r+1] -= 1; } rep(i,1,n){ sum[i] = sum[i-1] + b[i]; }4 行未改动 putchar(' '); continue; } write(solve(a[i],sum[i])); putchar(' '); putchar(' '); } return 0;}/*2 行未改动通过打表我们发现,一个数 p ,每次加log p 相当于加了 p 的位数-1这个数
显然每个数被加了多少次是很好得到的,那问题就是我们怎么刻画每次加位数-1这个过程
就是有什么规律? 就是有什么规律?
这个规律不是很好找, 这个规律不是很好找,*/```::::#### T4 P1269 信号放大器::::success[代码]```cpp#include <bits/stdc++.h>#define ll long long #define ll long long#define rep(i,a,n) for(int i = (a);i<=(n);++i)#define endl '\n'#define _x putchar('\n');using namespace std;25 行未改动};vector<node> vec[N];int ans = 0,f[N],g[N];//f[i] 从父节点传到 i 的损耗。//g[i] 表示以 i 为跟的子树内(包括 i )的衰减量的最大值 //g[i] 表示以 i 为跟的子树内(包括 i )的衰减量的最大值
void dfs(int u,int fa){ for(vector<node>::iterator it = vec[u].begin();it!=vec[u].end();++it){ node v = *it; if(v.num == fa)continue; f[v.num] = v.val; f[v.num] = v.val; dfs(v.num,u); g[u] = max(g[u],g[v.num] + v.val); } if(f[u] + g[u] > p){//到i的衰减加上子树里的最大衰减大于信号强度 if(f[u] + g[u] > p){//到i的衰减加上子树里的最大衰减大于信号强度 ++ans; g[u] = 0; } }}signed main(){ read(n); int maxx = 0;4 行未改动 vec[i].push_back(node(v,w)); maxx = max(maxx,w); } } read(p); read(p); if(maxx >= p){ cout <<"No solution."<<endl; return 0; return 0; } dfs(1,1); cout <<ans; return 0;}/*
考虑无解的情况,所有的衰减量中,有一个是大于等于 p 的,那我无论怎么处理都不可能成立。 考虑无解的情况,所有的衰减量中,有一个是大于等于 p 的,那我无论怎么处理都不可能成立。怎么贪心?从叶子节点往上贪心,如果当前到这个节点需要一个信号增大器,那我就用。*/```::::
#### T5 P14570 「LAOI-11」Metamorphosism::::success[代码]```cpp#include <bits/stdc++.h>#define ll long long #define ll long long#define rep(i,a,n) for(int i = (a);i<=(n);++i)#define endl '\n'using namespace std;
12 行未改动 if(x>=10){ write(x/10); } putchar(x%10+'0');} }int n = 0,m = 0;signed main(){ read(n);read(m); if(m == 10000){3 行未改动 } }else if(m == 170000||m == 210000){ for(int i = n;i<=2*n-1;++i){ printf("%d ",i); } } }else{ int l = sqrt(m); if(l%2 == 0)l++; else l += 2; for(int i = 1;i<=n;++i){ printf("%d ",l); l += 2; }
}
return 0;}/*
给出一组 n 个数的构造a[i]<=m,使得不存在三个数 a[i]、a[j]、a[k],满足a[i] + a[j] = a[k] 或a[i]*a[j] = a[k] 或 a[i]*a[j] = a[k] 或a[i]^a[j] = a[k];
n<=4.4*1e5 m=8.9*1e5*/7 行未改动
::::success[代码]```cpp#include <bits/stdc++.h>#define ll long long #define ll long long#define rep(i,a,n) for(int i = (a);i<=(n);++i)#define endl '\n'#define _x putchar('\n')using namespace std;18 行未改动const int N = 60,M = 1e3+500;int a[N],b[M];ll tot = 0,sum[M];int n = 0,m = 0;bool check(int x,int y){//从后往前处理到第 x 张嘴,当前吃到第 y 个蛋糕 if(!x) return 1;//已经处理结束 bool check(int x,int y){//从后往前处理到第 x 张嘴,当前吃到第 y 个蛋糕 if(!x) return 1;//已经处理结束 if(sum[x]>tot)return 0; bool flag = 0; for(int i = y;i<=n;++i){ if(a[i]>=b[x]){//当前的蛋糕能给这个嘴吃 a[i]-=b[x];tot -= b[x];//给他吃!!! if(a[i]<b[1])tot-=a[i];//吃完剩的残渣没有了利用价值 if(a[i]>=b[x]){//当前的蛋糕能给这个嘴吃 a[i]-=b[x];tot -= b[x];//给他吃!!! if(a[i]<b[1])tot-=a[i];//吃完剩的残渣没有了利用价值 if(b[x] == b[x-1]){ flag = check(x-1,i); }else{ flag = check(x-1,1);2 行未改动 a[i]+=b[x];tot += b[x]; if(flag) return 1; } } return 0; return 0;}signed main(){ read(n); for(int i = 1;i<=n;++i){ read(a[i]);tot += a[i]; } } read(m); rep(i,1,m){ read(b[i]); } sort(a+1,a+1+n); sort(b+1,b+1+m); rep(i,1,m){ sum[i] = sum[i-1] + b[i]; }
if(tot<b[1]){ cout <<0<<endl; return 0; } int l = 0,r = m; for(int i = 1;i<=m;++i){ //前面的人吃的总和大于总蛋糕,或者第 i 个人吃的蛋糕压根匹配不上 //前面的人吃的总和大于总蛋糕,或者第 i 个人吃的蛋糕压根匹配不上 if(sum[i]>tot||b[i]>a[n]){ r = i-1; break; } }
while(l<r){ int mid = (l+r+1)>>1;// cout <<"D: "<<l<<" "<<r<<" "<<mid<<endl; if(check(mid,1))l = mid;//check 能不能满足 mid 个人吃的。 if(check(mid,1))l = mid;//check 能不能满足 mid 个人吃的。 else r = mid-1; } cout <<l<<endl; return 0;3 行未改动#### T7 P2329 [SCOI2005] 栅栏::::success[代码]```cpp#include <bits/stdc++.h>#define ll long long #define ll long long#define rep(i,a,n) for(int i = (a);i<=(n);++i)#define endl '\n'#define _x putchar('\n')using namespace std;18 行未改动const int N = 60,M = 1e3+500;int a[N],b[M];ll tot = 0,sum[M];int n = 0,m = 0;bool check(int x,int y){//从后往前处理到第 x 张嘴,当前吃到第 y 个蛋糕 if(!x) return 1;//已经处理结束 bool check(int x,int y){//从后往前处理到第 x 张嘴,当前吃到第 y 个蛋糕 if(!x) return 1;//已经处理结束 if(sum[x]>tot)return 0; bool flag = 0; for(int i = y;i<=n;++i){ if(a[i]>=b[x]){//当前的蛋糕能给这个嘴吃 a[i]-=b[x];tot -= b[x];//给他吃!!! if(a[i]<b[1])tot-=a[i];//吃完剩的残渣没有了利用价值 if(a[i]>=b[x]){//当前的蛋糕能给这个嘴吃 a[i]-=b[x];tot -= b[x];//给他吃!!! if(a[i]<b[1])tot-=a[i];//吃完剩的残渣没有了利用价值 if(b[x] == b[x-1]){ flag = check(x-1,i); }else{ flag = check(x-1,1);2 行未改动 a[i]+=b[x];tot += b[x]; if(flag) return 1; } } return 0; return 0;}signed main(){ read(n); for(int i = 1;i<=n;++i){ read(a[i]);tot += a[i]; } } read(m); rep(i,1,m){ read(b[i]); } sort(a+1,a+1+n); sort(b+1,b+1+m); rep(i,1,m){ sum[i] = sum[i-1] + b[i]; }
if(tot<b[1]){ cout <<0<<endl; return 0; } int l = 0,r = m; for(int i = 1;i<=m;++i){ //前面的人吃的总和大于总蛋糕,或者第 i 个人吃的蛋糕压根匹配不上 //前面的人吃的总和大于总蛋糕,或者第 i 个人吃的蛋糕压根匹配不上 if(sum[i]>tot||b[i]>a[n]){ r = i-1; break; } }
while(l<r){ int mid = (l+r+1)>>1;// cout <<"D: "<<l<<" "<<r<<" "<<mid<<endl; if(check(mid,1))l = mid;//check 能不能满足 mid 个人吃的。 if(check(mid,1))l = mid;//check 能不能满足 mid 个人吃的。 else r = mid-1; } cout <<l<<endl; return 0;3 行未改动#### T8 P4053 [JSOI2007] 建筑抢修::::success[代码]```cpp#include <bits/stdc++.h>#define ll long long #define ll long long#define rep(i,a,n) for(int i = (a);i<=(n);++i)#define endl '\n'#define _x putchar('\n')
6 行未改动 x*=t;}const int N = 2e5+10;int n = 0;ll tot = 0; ll tot = 0;struct node{ int t1,t2;}a[N]; }a[N];priority_queue<int> q;bool cmp(node x,node y){ if(x.t2 == y.t2){ return x.t1<y.t1;12 行未改动 tot += a[i].t1; q.push(a[i].t1); if(tot<=a[i].t2){ ++ans; }else{//不修理这一个,那当前用时还是 tot。 }else{//不修理这一个,那当前用时还是 tot。 tot -= q.top(); q.pop(); } }6 行未改动数据保证 t1<t2,一个直接的想法是对 t1 求前缀和,t2 < sum[2] 那么这个建筑就修不了了但是直接做肯定是的错的,我们考虑怎么排序,就是贪心的策略。按t2从小到大排序,你先处理前面的肯定比先处理后面的更优,等到后面的时候前面的都爆炸了。基于此我们进一步考虑,直接前缀和 t1,当前的sum[i] <t2 就ans++; 基于此我们进一步考虑,直接前缀和 t1,当前的sum[i] <t2 就ans++;对吗?感觉有点对。但这是个蓝题,所以大概率不对。 但这是个蓝题,所以大概率不对。
我们考虑 a在b 前面可以,b在a前面就不可以的情况。
t + a.t1+b.t1<b.t23 行未改动所以肯定是按照 t2 排序,然后呢?
52 min看了题解...之前写的大部分是对的,问题在于,当前这个tot>b[i]之后,你肯定要报废一个 之前写的大部分是对的,问题在于,当前这个tot>b[i]之后,你肯定要报废一个你应该选择一个之前选过的最大的最大修理时间的报废,而不是就简单的报废当前这个。剩下应该就没什么问题了。 剩下应该就没什么问题了。
*/```::::
#### T9 P2024 [NOI2001] 食物链::::info[代码]```cpp#include <bits/stdc++.h>#define ll long long #define ll long long#define rep(i,a,n) for(int i = (a);i<=(n);++i)#define endl '\n'
using namespace std;template<typename T>inline void read(T &x){ char c = getchar(); short int t = 1;x = 0; for(;!isdigit(c);c = getchar())if(c == '-')t=-1; for(;isdigit(c);c = getchar())x = (x<<1) + (x<<3) + (c^48); x*=t; x*=t;}const int N = 5e4+10;int n = 0,k = 0;int fa[3*N];int findf(int x){ return fa[x] == x?x:fa[x] = findf(fa[x]);} }
signed main(){ read(n);read(k); for(int i = 1;i<=3*n;++i)fa[i] = i;6 行未改动 continue; } if(opt == 1){ if(findf(x+n) == findf(y)||findf(x) == findf(y+n)){ ++ans; ++ans; }else{ fa[findf(x)] = findf(y); fa[findf(x+n)] = findf(y+n); fa[findf(x+n+n)] = findf(y+n+n);11 行未改动 return 0;}/*这种东西一看就是并查集了。我们考虑怎么维护集合间的信息 我们考虑怎么维护集合间的信息条件2 3是显然的,重点是条件 1,我们考虑维护两个并查集,一个表示同类关系,一个表示吃关系,opt == 1 时,先检查在 b 集合中有没有所属关系,没有的话就在 a 集合中合并opt == 2 时,先检查在 a 集合中有没有所属关系,没有的话就在 b 集合中合并否则就 ++ans;路径压缩的均摊时间复杂度是 O(klog2(n)) 大概 22,360,679 2e7多,因为每次合并都在两个集合内操作,做四次 findf,所以前面还有一个 4 的常数,大概 8e7,屏幕前的友友们你猜洛谷评测及能跑 8e7 吗? 大概 8e7,屏幕前的友友们你猜洛谷评测及能跑 8e7 吗?
但是我好像忽略了吃是一个单项的关系, 就是假设当前已经有了A->B ,这时候再来一个B->A 我的程序就认为是错的,但是这实际上是对的??
所以说我们应该建图,跑最短路 所以说我们应该建图,跑最短路
no
这个里面有三种关系,种类并查集。5 行未改动#### T10 P1525 [NOIP 2010 提高组] 关押罪犯::::info[代码]```cpp#include <bits/stdc++.h>#define ll long long #define ll long long#define rep(i,a,n) for(int i = (a);i<=(n);++i)#define endl '\n' #define endl '\n'using namespace std;template<typename T>inline void read(T &x){ char c = getchar(); short int t = 1;x = 0;41 行未改动}/*依旧种类并查集,(扩展并查集)首先这是一定可以贪心的,我们尽量的安排大的不和谐值的罪犯不在一块,如果是在安排不开了再更新答案。 再更新答案。拓展域并查集,主要用于一种状态表示无法解决问题的题目。例如本题,一个开关有两种状态,用和不用。
本文主要介绍二倍拓展域的拓展域并查集。
14 行未改动#### A::::info[代码]```cpp#include <bits/stdc++.h>#define ll long long #define ll long longusing namespace std;int a = 0,b = 0;signed main(){ cin >> a>>b;9 行未改动
::::info[代码]```cpp#include <bits/stdc++.h>#define ll long long #define ll long long#define rep(i,a,n) for(int i = (a);i<=(n);++i)#define endl '\n'#define lowbit(x) ((x)&(-x))#define _x putchar('\n')24 行未改动 ans /= vec[i].size(); printf("%.6lf",ans);_x; } return 0;
}```::::#### C::::info[代码]```cpp#include <bits/stdc++.h>#define ll long long #define ll long long#define rep(i,a,n) for(int i = (a);i<=(n);++i)#define endl '\n'#define lowbit(x) ((x)&(-x))#define _x putchar('\n')21 行未改动 L = L - d; R = R + d; L = max(L,l[i]); R = min(R,r[i]);
if(L>R){ flag = 0; } }2 行未改动 }else{ cout <<"No";_x; } }
return 0;}/*有 n 个限制,他想在第 t[i] 秒,到达 [l[i],r[i]] 这个高度。16 行未改动
::::info[代码]```cpp#include <bits/stdc++.h>#define ll long long #define ll long long#define rep(i,a,n) for(int i = (a);i<=(n);++i)#define endl '\n'#define lowbit(x) ((x)&(-x))#define _x putchar('\n')22 行未改动 b[x1][y1]++; b[x1][y2+1]--; b[x2+1][y1]--; b[x2+1][y2+1]++;
q[i] = node(x1,y1,x2,y2); } int cnt = 0;
for(int i = 1;i<=2000;++i){ for(int j = 1;j<=2000;++j){ b[i][j] = b[i-1][j] + b[i][j-1] - b[i-1][j-1] + b[i][j]; if(b[i][j] == 0)++cnt;26 行未改动
::::info[代码]```cpp#include <bits/stdc++.h>#define ll long long #define ll long long#define rep(i,a,n) for(int i = (a);i<=(n);++i)#define endl '\n'#define lowbit(x) ((x)&(-x))#define _x putchar('\n')99 行未改动
::::info[代码]```cpp#include <bits/stdc++.h>#define ll long long #define ll long long#define rep(i,a,n) for(int i = (a);i<=(n);++i)#define lowbit(x) ((x)&(-x))#define _x putchar('\n')#define lc (root<<1)130 行未改动全部展开```::::#### [P1787 [入门赛 #22] 非众数 Hard Version](https://www.luogu.com.cn/problem/P1787)
求非空子串中非众数串的个数,非空总字串数是 n*(n+1) / 2。减去众数串就是非空子串。 求非空子串中非众数串的个数,非空总字串数是 n*(n+1) / 2。减去众数串就是非空子串。
显然大于 len/2 的只能有一个字母,
就变成求 sum(i = [1,26],sum(l = [0,len],sum(r = [i,len],bit[i].tree[r] - bit[i].tree[l-1] > (r-l+1)/2 ) ) )22 行未改动
::::info[代码]```cpp#include <bits/stdc++.h>#define ll long long #define ll long long#define rep(i,a,n) for(int i = (a);i<=(n);++i)#define endl '\n'#define lc (root<<1)#define rc (root<<1|1)35 行未改动 for(int i = 1;i<=26;++i){ memset(tree,0,sizeof tree); memset(p,0,sizeof p); update(2*n+1,1);
for(int j = 1;j<=n;++j){ p[j] = p[j-1] + (c[j]-'a'+1 == i); tot += query(2*n+1 + 2*p[j]-j-1); update(2*n+1+2*p[j]-j,1);16 行未改动// }// }// }// cout <<ans<<" "<<tot<<endl;
cout <<ans - tot;
return 0;}```::::135 行未改动全部展开 int l = 0,r = 0; read(l);read(r); que[i].l = l,que[i].r = r;que[i].id = i; las_query[i] = t[r]; // 区间 i 的上一个区间的右端点的查询。 t[r] = i; //右端点的编号。 t[r] = i; //右端点的编号。 }// for(int i = 1;i<=q;++i){// cout <<t[que[i].r]<<" ";// }141 行未改动全部展开#define rc (x<<1|1)#define mid ((l+r)>>1)#define DB double#define rep(i,a,n) for(int i = (a);i<=(n);++i)#define ll long long #define ll long long#define debug "Here!!!::Debug-=>"using namespace std;
template<typename T>inline void read(T &x){18 行未改动set<int> s,t;//没病不戴口罩,有病不戴口罩。signed main(){ read(n);read(q);
rep(i,1,n){ read(a[i]); b[i] = 1; }48 行未改动拆出去有一个 sum(i),这个 i 就是所有与 j 产生逆序对的下标,因此用树状数组维护这个下标,求前缀和。::::info[代码]```cpp#include <bits/stdc++.h>#define ll long long #define ll long long#define rep(i,a,n) for(int i = (a);i<=(n);++i)#define endl '\n'#define lc (root<<1)#define rc (root<<1|1)44 行未改动 ++cnt; } sort(b+1,b+1+cnt); cnt = unique(b+1,b+1+cnt)-b-1;
rep(i,1,n){ a[i] = lower_bound(b+1,b+1+cnt,a[i])-b; } __int128 p = 1; for(int i = 1;i<=n;++i){ ans += p*(n-i+1)*getsum(a[i]+1,cnt); update(a[i],i);
} write(ans); return 0;}12 行未改动::::info[代码]```cpp#include <bits/stdc++.h>#define ll long long#define rep(i,a,n) for(int i = (a);i<=(n);++i) #define rep(i,a,n) for(int i = (a);i<=(n);++i)using namespace std;template<typename T>inline void read(T &x){ char c = getchar(); short int t = 1;x = 0;16 行未改动 cout <<-q.top()<<endl; for(int i = k+1;i<=n;++i){ q.push(-a[i]); q.pop(); cout <<-q.top()<<endl; cout <<-q.top()<<endl; } return 0;}```::::#### AtCoder - abc237_d LR insertion #### AtCoder - abc237_d LR insertion
链表暴力维护即可,要注意的是迭代器指向的是这个元素左边的位置。
::::info[代码]```cpp#include <bits/stdc++.h>#define ll long long#define endl '\n'#define rep(i,a,n) for(int i = (a);i<=(n);++i) #define rep(i,a,n) for(int i = (a);i<=(n);++i)using namespace std;template<typename T>inline void read(T &x){ char c = getchar(); short int t = 1;x = 0;74 行未改动 }else{ stk[++top] = a[i]; } }
for(int i = 1; i <= top; ++i){ if(stk[i] == 'L'){ x = 2 * x; }else if(stk[i] == 'R'){99 行未改动18 1 0 0 1 0a: 1 0 0 1 0 b: 0 0 0 0 0 0 18a: 1 0 0 0 1 b: 0 0 0 0 1 1 20 1 1 0 0 1a: 1 0 1 1 1 a: 1 0 1 1 1b: 0 1 1 1 0这种情况 1 的位置会贡献一个 1<<pos,0 的位置会贡献两个 1 <<pos1 0 1 这种情况下,开头的 1 选完之后后面那个 0 没有贡献,因为要是选的话一定不满足 a<=n,1 0 0 164 行未改动 printf("%lld\n",ans); }else{ for(int i = 1;i<=cnt;++i){//实际上这一位的权值是 1<<i>>1 if(b[i] == 1){ ans += (1ll<<i>>1ll); ans += (1ll<<i>>1ll); }else{ ans += 2*(1ll<<i>>1ll); } }37 行未改动 if(tot <=m){ return 1; } }
for(int s = 1;s<=n;++s){ int now = cnt1[s]; int end = s+n-1; int tot = cnt1[s];9 行未改动 if(tot <=m){ return 1; } } return 0; return 0;}
signed main(){; read(n);read(m);11 行未改动 }else{ r = mid-1; } } cout <<maxn - l; cout <<maxn - l;
return 0;}```::::3 行未改动分层图板子题。::::info[代码]```cpp#include <bits/stdc++.h>#define ll long long #define ll long long#define rep(i,a,n) for(int i = (a);i<=(n);++i)#define endl '\n'#define lc (root<<1)#define rc (root<<1|1)29 行未改动 dis[1]=0; while(!q.empty()){ node u = q.top(); q.pop();
if(vis[u.num]) continue; vis[u.num]=1; for(auto v : vec[u.num]){ if(dis[v.num]>dis[u.num]+v.dis){88 行未改动 return res;}ll get(int l,int r){ return query(r) - query(l-1);} }
signed main(){ read(n);read(l);read(r);
rep(i,1,n){ read(a[i]); sum[i] = sum[i-1] + a[i]; b[++cnt] = sum[i]; } b[++cnt] = 0; sort(b+1,b+1+cnt); cnt = unique(b+1,b+1+cnt)-b-1;
ll ans = 0; update(lower_bound(b+1,b+1+cnt,0)-b,1); for(int i = 1;i<=n;++i){//枚举 sum[i] int L = lower_bound(b+1,b+1+cnt,sum[i] - r)-b;54 行未改动 rep(i,1,m) read(c[i]); for(int i = 1;i<=n;++i){ if(i <= m) d[i] = c[i]; else d[i] = ((1ll*x*d[i-2]%MOD + 1ll*y*d[i-1]%MOD + z)%MOD)+1; } } } for(int i = 1;i<=n;++i){ d[i+n] = d[i]; } for(int i = 1;i<=2*n;++i){d[i] -= i; } deque<int> q;11 行未改动 if(i>=n){ int p = i-n+1; // 窗口左端点,即最大值对应的 p 。 if(p>n)break; ll maxn = d[q.front()];//区间最大值。 ll a = maxn + p; //对应最小灵密度。 ll a = maxn + p; //对应最小灵密度。 if(a < mina){ mina = a; index = p; }22 行未改动2 1 | 6 5 4 3 | 8 7 翻转三次 : 1 2 3 4 5 6 7 8
反转需要调用的次数
统计 j < i 且 a[j] > a[i] 的个数。 统计 j < i 且 a[j] > a[i] 的个数。
题目里要求了递减序列的长度是偶数,
这题有点小难,看完题解了后才知道了结论,254 行未改动全部展开 dp[u] = max(dp[u],min(dp[v],1.0*sz[v]/(sz[u]-1))); } if(sz[u] > k){ ans = max(ans,dp[u]); } }}signed main(){ read(n);read(k); rep(i,2,n){24 行未改动
好像这两个特殊性质都没什么用。让剩下的子序列的最大值是 n 的最大因数,除了 n 且 <m x,为什么 <m 呢,我造了一个数据来理解一下。假设 n = 8,m = 3 假设 n = 8,m = 3
1 2 4 3 5 6 7 88 的最大除 8 外的因数是 4,我们贪心的将 4 放到第 3 个位置上,但是后面就兼顾不了了,只能放一个 2 ,那最终的 ans 就是 2 了。至于为什么不取等应该好想。188 行未改动全部展开 t[b[2][j]] = j; } for(int j = 1;j<=n;++j){ p[++cnt1] = t[a[1][j]]; } } for(int i = 1;i<=n;++i){ ans += query(n) - query(p[i]-1); update(p[i],1); }6 行未改动#### T6 P11036 【MX-X3-T3】「RiOI-4」GCD 与 LCM 问题
这个题还要仔细想想从奇数推广到偶数。
```cpp
给定一个 a构造 b,c,d 使得 a + b + c + d = gcd(a,b) + lcm(c,d)b,c,d <= 1634826193 1.6e10左右 ,感觉这个没什么用。
钦定 b = 1,a+c+d = lcm(b,d)。注意到这个时候假设我们已经构造出了 gcd(c,d) = 1;a + c + d = cd
a+1 = (c-1)(d-1),考虑特殊性质,a 为奇数,a+1 为偶数,直接构造 c-1 = 1,c = 2,d = a+2 ,其它构造的话要满足 gcd(c,d) = 1 好像都不太行。我尝试 c-1 = 2 是不完全对的。
考虑 a+1 为偶数的时候,a + b + c + d = gcd(a,b) + lcm(b,d)ak+bk+ck+dk = k*gcd(a,b) + k*lcm(c,d)
ak+bk+ck+dk = gcd(ak,bk) + lcm(ck,dk) 和一位。
a 为偶数时, a+1 = (c-1)(d-1) 此时 gcd(c,d) = 2
```
:::info[代码]```cpp#include <bits/stdc++.h>#define ll long long18 行未改动 cout <<1<<" "<<(x<<1) <<" "<<n+(x<<1)<<endl; } return 0;}/*给定一个 a构造 b,c,d 使得 a + b + c + d = gcd(a,b) + lcm(c,d)b,c,d <= 1634826193 1.6e10左右 ,感觉这个没什么用。```:::### 12.17今天怎么又在效率拉闸。写个题写了一下午。
钦定 b = 1,a+c+d = lcm(b,d)。注意到这个时候假设我们已经构造出了 gcd(c,d) = 1;a + c + d = cd#### P9002 [RC-07] 心跳 题解
a+1 = (c-1)(d-1),考虑特殊性质,a 为奇数,a+1 为偶数,直接构造 c-1 = 1,c = 2,d = a+2 ,其它构造的话要满足 gcd(c,d) = 1 好像都不太行。我尝试 c-1 = 2 是不完全对的。### 12.18
考虑 a+1 为偶数的时候,a + b + c + d = gcd(a,b) + lcm(b,d)ak+bk+ck+dk = k*gcd(a,b) + k*lcm(c,d)做了个双倍经验的 BFS,黄题,没啥要记录的。
ak+bk+ck+dk = gcd(ak,bk) + lcm(ck,dk) 和一位。#### T3 P13980 数列分块入门 5 (蓝)这个题我是拿线段树 A 的,区间开根实际上还是双倍经验。
a 为偶数时, a+1 = (c-1)(d-1) 此时 gcd(c,d) = 2*/因为一个数开根的次数大概是 $\log \log $ 级别的,所以一个数开根的次数是有限的,区间开根的时候直接对单点暴力修改,那么当这个区间的区间和小于等于区间长度的时候,就说明这个区间已经全部修改完了。
代码就不放了,跟原来一个题一样,就重新写了一遍。
#### T4 P4370 [Code+#4] 组合数问题 (紫)
组合数学的一道好题。值得我再重新想一遍。
```cpp我们发现(n,m)比(n-1,m)要大,因为两者相除结果大于1(n/(n-m)) 也可以理解为分母不变,分子变小 所以可以用大根堆比较 循环拿出放入操作,但组合数太大了 我们考虑对数操作log2(n,m)=log2(n!)-log2(m!)-log2(n-m)! 然后把阶乘操作改成对数相加```:::### 12.17今天怎么又在效率拉闸。写个题写了一下午。
#### P9002 [RC-07] 心跳 题解代码里有注释。
:::info[代码]```cpp#include <bits/stdc++.h>#define ll long long#define rep(i,a,n) for(int i = (a);i<=(n);++i)#define endl '\n'#define DB double#define pii pair<int,int>#define fi first#define sc secondusing namespace std;
template<typename T>inline void read(T &x){ char c = getchar(); short int t = 1;x = 0; for(;!isdigit(c);c = getchar())if(c == '-')t=-1; for(;isdigit(c);c = getchar())x = (x<<1) + (x<<3) + (c^48); x*=t;}const int mod = 1e9+7,N = 1e6+10;ll n,k,jc[N],ny[N],ans;double lg[N];priority_queue<pair<DB,pii> > q;ll mpow(ll x,ll y){//快速幂 ll ans=1; while(y){ if(y&1) ans=ans*x%mod; x=x*x%mod; y/=2; } return ans;}void init(int n){//预处理阶乘,逆元,对数前缀和 jc[0]=ny[0]=1; for(int i=1;i<=n;i++){ jc[i]=jc[i-1]*i%mod; jc[i]%=mod; } ny[n]=mpow(jc[n],mod-2); for(int i=n;i>=2;i--){ ny[i-1]=ny[i]*i%mod; } for(int i=1;i<=n;i++){ lg[i]=lg[i-1]+log(i); }}int main(){ /*我们发现(n,m)比(n-1,m)要大,因为两者相除结果大于1(n/(n-m)) 也可以理解为分母不变,分子变小 所以可以用大根堆比较 循环拿出放入操作,但组合数太大了 我们考虑对数操作log2(n,m)=log2(n!)-log2(m!)-log2(n-m)! 然后把阶乘操作改成对数相加*/ init(1000000); cin>>n>>k; for(int i=0;i<=n;i++){ q.push(make_pair(lg[n]-lg[n-i]-lg[i],pii(n,i)) ); } for(int i=1;i<=k;i++){ ll x=q.top().sc.fi; ll y=q.top().sc.sc; q.pop(); ans=(ans+jc[x]*ny[y]%mod*ny[x-y]%mod)%mod; //直接求组合数 x--; q.push(make_pair(lg[x]-lg[x-y]-lg[y],pii(x,y)) ); } cout<<ans; return 0;}#include <bits/stdc++.h>#define ll long long#define rep(i,a,n) for(int i = (a);i<=(n);++i)#define endl '\n'#define DB double#define pii pair<int,int>#define fi first#define sc secondusing namespace std;
template<typename T>inline void read(T &x){ char c = getchar(); short int t = 1;x = 0; for(;!isdigit(c);c = getchar())if(c == '-')t=-1; for(;isdigit(c);c = getchar())x = (x<<1) + (x<<3) + (c^48); x*=t;}const int mod = 1e9+7,N = 1e6+10;ll n,k,jc[N],ny[N],ans;double lg[N];priority_queue<pair<DB,pii> > q;ll mpow(ll x,ll y){//快速幂 ll ans=1; while(y){ if(y&1) ans=ans*x%mod; x=x*x%mod; y/=2; } return ans;}void init(int n){//预处理阶乘,逆元,对数前缀和 jc[0]=ny[0]=1; for(int i=1;i<=n;i++){ jc[i]=jc[i-1]*i%mod; jc[i]%=mod; } ny[n]=mpow(jc[n],mod-2); for(int i=n;i>=2;i--){ ny[i-1]=ny[i]*i%mod; } for(int i=1;i<=n;i++){ lg[i]=lg[i-1]+log(i); }}int main(){ init(1000000); cin>>n>>k; for(int i=0;i<=n;i++){ q.push(make_pair(lg[n]-lg[n-i]-lg[i],pii(n,i)) ); } for(int i=1;i<=k;i++){ ll x=q.top().sc.fi; ll y=q.top().sc.sc; q.pop(); ans=(ans+jc[x]*ny[y]%mod*ny[x-y]%mod)%mod; //直接求组合数 x--; q.push(make_pair(lg[x]-lg[x-y]-lg[y],pii(x,y)) ); } cout<<ans; return 0;}```:::