CF 551 E GukiZ and GukiZiana
https://codeforces.com/contest/551/problem/E
分块真强。
题意就是1、区间加,2、询问整个区间中,最远的两个x的距离。
分块,然后,每次找位子用二分找即可。
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> using namespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; typedef pair<ll,int>pli; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl '\n' //#define R register #define OKC ios::sync_with_stdio(false);cin.tie(0) #define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行 #define REP(i , j , k) for(int i = j ; i < k ; ++i) #define max3(a,b,c) max(max(a,b), c); #define min3(a,b,c) min(min(a,b), c); //priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9+7; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黄金分割点 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } /*-----------------------showtime----------------------*/ const int maxn = 5e5+9; ll a[maxn]; int be[maxn]; int blo; ll add[maxn]; vector<pli>v[maxn]; int n,m; void rebuild(int id){ v[id].clear(); for(int i=(id-1)*blo + 1; i<= min(n,id*blo); i++){ a[i] += add[id]; v[id].pb(pli(a[i],i)); } sort(v[id].begin(),v[id].end()); add[id] = 0; } int main(){ scanf("%d%d", &n, &m); for(int i=1; i<=n; i++) scanf("%lld", &a[i]); // blo = (int)sqrt(n); blo = 1000; for(int i=1; i<=n; i++){ be[i] = (i-1)/blo + 1; v[be[i]].pb(pli(a[i],i)); } for(int i=1; i<=be[n]; i++){ sort(v[i].begin(),v[i].end()); } while(m--){ int op; scanf("%d", &op); if(op == 1){ int l,r,x; scanf("%d%d%d", &l, &r, &x); for(int i=l; i<= min(r, be[l]*blo); i++){ a[i] += x; } rebuild(be[l]); if(be[l] < be[r]){ for(int i=be[l]+1; i<=be[r]-1; i++){ add[i] += x; } for(int i=(be[r]-1)*blo+1; i<= r; i++){ a[i] += x; } rebuild(be[r]); } } else { int x; scanf("%d", &x); int le=n+1,ri=-1; for(int i=1; i<=be[n]; i++){ int tmp = lower_bound(v[i].begin(),v[i].end(),pli(1ll*(x-add[i]),0)) - v[i].begin(); if(v[i][tmp].fi == x-add[i]){ le = min(le, v[i][tmp].se); } tmp = upper_bound(v[i].begin(), v[i].end(), pli(1ll*(x-add[i]),n+1)) - v[i].begin() - 1; if(tmp>=0 && v[i][tmp].fi == x-add[i]){ ri = max(ri, v[i][tmp].se); } } if(le <= ri){ printf("%d\n", ri - le); } else puts("-1"); } } return 0; }
自己一开始把 块的id 和 i 搞混了...
skr