//#include <stdio.h> #include <bits/stdc++.h> //#include <ctime> using namespace std; typedef vector<int> VI; typedef long double LD; typedef long long LL; typedef vector<VI> VVI; typedef vector<LL> VLL; typedef vector<double> VD; typedef vector<string> VS; typedef pair<int, int> PII; typedef vector<PII> VPII; typedef pair<LL,LL> PLL; typedef vector <PLL> VPLL; #define FOR(x, b, e) for(int x = b; x <= (e); ++x) #define FORD(x, b, e) for(int x = b; x >= (e); --x) #define REP(x, n) for(int x = 0; x < (n); ++x) #define VAR(v, n) typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define PB push_back #define ST first #define ND second #define MP make_pair #define PF push_front const bool dbg=false; const int maxn=2e5+5; const double INF=1e18; const double minf=1e17; const double eps=1e-9; int n,m; double os; double czekanie; vector <double> times; VPLL queries, answers; stack < pair<double,int> > s; VI input; int z; LL pluswyn; int ostatni; LL streak; LL SS=0; int spojna[maxn]; LL rozmiar[maxn]; vector <pair< double, int> > timesp; int znajdz(int x) { return spojna[x]==x ? x : spojna[x]=znajdz(spojna[x]) ; } void unia(int x, int y) { x=znajdz(x); y=znajdz(y); if(x!=y) { SS-=(rozmiar[x]*(rozmiar[x]-1))/2LL; SS-=(rozmiar[y]*(rozmiar[y]-1))/2LL; rozmiar[x]+=rozmiar[y]; spojna[y]=x; SS+=(rozmiar[x]*(rozmiar[x]-1))/2LL; } } void polacz(int x) { unia(x,x-1); } int main() { scanf("%d %d", &n, &m); REP(i,n+1) { spojna[i]=i; rozmiar[i]=1; } double last=0.0; int waga=1; double czas,okno; double res; int wolne=0; ostatni=-1; streak=0; REP(i,n) { scanf("%d", &z); if(z==ostatni) { streak++; } else { pluswyn+=(streak*(streak-1))/2LL; streak=0; } ostatni=z; input.PB(z); } pluswyn+=(streak*(streak-1))/2LL; REP(i,n) { //scanf("%lf", &os); os=double(input[i]); czas=os-last; res=0.0; waga=1; while(!s.empty() && (s.top().ST)<(res+czas/(double(waga))) ) { okno=(s.top().ST)-res; //res=(res+okno/(double(waga)) ); res=(s.top().ST); czas-=okno*(double(waga)); waga+=(s.top().ND); s.pop(); } res=res+(czas/(double(waga))); s.push(MP(res,waga)); timesp.PB(MP(res,i+1)); times.PB(res); if(dbg) { cerr<<fixed; cerr.precision(2); cerr<<"wrzuca ("<<res<<", "<<waga<<")\n"; } last=os; } sort(ALL(timesp)); timesp.PB(MP(INF,maxn-2)); if(dbg) { cerr<<"czasy: "<<endl; for( double w : times) cerr<<w<<" "; cerr<<endl; } LL x; REP(i,m) { scanf("%lld", &x); queries.PB(MP(x,i)); } sort(ALL(queries)); waga=0; czas=0.0; int wsk=0; czekanie=0.0; REP(i,m) { if(dbg) cerr<<"query "<<queries[i].ST<<" "<<queries[i].ND<<endl; while(timesp[wsk].ST<(double(queries[i].ST))) { if(dbg) cerr<<"times# "<<times[wsk]<<" "; czekanie=czekanie+(timesp[wsk].ST-czas)*(double(SS)); czas=timesp[wsk].ST; polacz(timesp[wsk].ND); wsk++; if(dbg) cerr<<"czekanie == "<<czekanie<<endl; } //cerr<<"SS=="<<SS<<endl; answers.PB(MP(queries[i].ND, LL(round(czekanie+((double(queries[i].ST))-czas)*(double(SS)) ) ) )); } sort(ALL(answers)); REP(i,m) { printf("%lld\n",answers[i].ND); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | //#include <stdio.h> #include <bits/stdc++.h> //#include <ctime> using namespace std; typedef vector<int> VI; typedef long double LD; typedef long long LL; typedef vector<VI> VVI; typedef vector<LL> VLL; typedef vector<double> VD; typedef vector<string> VS; typedef pair<int, int> PII; typedef vector<PII> VPII; typedef pair<LL,LL> PLL; typedef vector <PLL> VPLL; #define FOR(x, b, e) for(int x = b; x <= (e); ++x) #define FORD(x, b, e) for(int x = b; x >= (e); --x) #define REP(x, n) for(int x = 0; x < (n); ++x) #define VAR(v, n) typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define PB push_back #define ST first #define ND second #define MP make_pair #define PF push_front const bool dbg=false; const int maxn=2e5+5; const double INF=1e18; const double minf=1e17; const double eps=1e-9; int n,m; double os; double czekanie; vector <double> times; VPLL queries, answers; stack < pair<double,int> > s; VI input; int z; LL pluswyn; int ostatni; LL streak; LL SS=0; int spojna[maxn]; LL rozmiar[maxn]; vector <pair< double, int> > timesp; int znajdz(int x) { return spojna[x]==x ? x : spojna[x]=znajdz(spojna[x]) ; } void unia(int x, int y) { x=znajdz(x); y=znajdz(y); if(x!=y) { SS-=(rozmiar[x]*(rozmiar[x]-1))/2LL; SS-=(rozmiar[y]*(rozmiar[y]-1))/2LL; rozmiar[x]+=rozmiar[y]; spojna[y]=x; SS+=(rozmiar[x]*(rozmiar[x]-1))/2LL; } } void polacz(int x) { unia(x,x-1); } int main() { scanf("%d %d", &n, &m); REP(i,n+1) { spojna[i]=i; rozmiar[i]=1; } double last=0.0; int waga=1; double czas,okno; double res; int wolne=0; ostatni=-1; streak=0; REP(i,n) { scanf("%d", &z); if(z==ostatni) { streak++; } else { pluswyn+=(streak*(streak-1))/2LL; streak=0; } ostatni=z; input.PB(z); } pluswyn+=(streak*(streak-1))/2LL; REP(i,n) { //scanf("%lf", &os); os=double(input[i]); czas=os-last; res=0.0; waga=1; while(!s.empty() && (s.top().ST)<(res+czas/(double(waga))) ) { okno=(s.top().ST)-res; //res=(res+okno/(double(waga)) ); res=(s.top().ST); czas-=okno*(double(waga)); waga+=(s.top().ND); s.pop(); } res=res+(czas/(double(waga))); s.push(MP(res,waga)); timesp.PB(MP(res,i+1)); times.PB(res); if(dbg) { cerr<<fixed; cerr.precision(2); cerr<<"wrzuca ("<<res<<", "<<waga<<")\n"; } last=os; } sort(ALL(timesp)); timesp.PB(MP(INF,maxn-2)); if(dbg) { cerr<<"czasy: "<<endl; for( double w : times) cerr<<w<<" "; cerr<<endl; } LL x; REP(i,m) { scanf("%lld", &x); queries.PB(MP(x,i)); } sort(ALL(queries)); waga=0; czas=0.0; int wsk=0; czekanie=0.0; REP(i,m) { if(dbg) cerr<<"query "<<queries[i].ST<<" "<<queries[i].ND<<endl; while(timesp[wsk].ST<(double(queries[i].ST))) { if(dbg) cerr<<"times# "<<times[wsk]<<" "; czekanie=czekanie+(timesp[wsk].ST-czas)*(double(SS)); czas=timesp[wsk].ST; polacz(timesp[wsk].ND); wsk++; if(dbg) cerr<<"czekanie == "<<czekanie<<endl; } //cerr<<"SS=="<<SS<<endl; answers.PB(MP(queries[i].ND, LL(round(czekanie+((double(queries[i].ST))-czas)*(double(SS)) ) ) )); } sort(ALL(answers)); REP(i,m) { printf("%lld\n",answers[i].ND); } } |