Pagini recente » Cod sursa (job #1546687) | Istoria paginii runda/going_oni | Cod sursa (job #1976983) | Cod sursa (job #2097244) | Cod sursa (job #495318)
Cod sursa(job #495318)
#include <stdio.h>
#include <bitset>
#include <vector>
#include <math.h>
using namespace std;
#define FOR(i, v) for(vector<int>::iterator i = v.begin(); i != v.end(); ++i)
#define NMAX 100010
bitset<NMAX*10> S[400];
vector <int> G[NMAX];
int viz[NMAX], v[NMAX];
int in[NMAX], out[NMAX];
int A[NMAX], B[NMAX];
int n, m, nsqrt;
void df(int x){
viz[x] = 1;
v[++v[0]] = x;
in[x] = v[0];
FOR(i, G[x])
if(!viz[*i])
df(*i);
out[x] = v[0];
}
inline int next(int p){
return ( (p-1) / nsqrt + 1) * nsqrt ;
}
inline int last(int p){
return ((p-1) / nsqrt ) * nsqrt + 1;
}
inline int next_bucata(int p){
return (p-1) / nsqrt + 2;
}
inline int last_bucata(int p){
return (p-1) / nsqrt;
}
inline int this_bucata(int p){
return (p-1) / nsqrt + 1;
}
void bonus(int p, int s){
int p1 = in[p], p2 = out[p];
int pstop = next(p1), pstart = last(p2);
if(this_bucata(p1) == this_bucata(p2)){
for(int i = last(p1) ; i <= pstop; ++i)
S[this_bucata(p1)][A[i]] = 0;
for(int i = p1; i <= p2; ++i)
A[i] += s;
for(int i = last(p1) ; i <= pstop; ++i)
S[this_bucata(p1)][A[i]] = 1;
return ;
}
for(int i = last(p1) ; i <= pstop; ++i)
S[this_bucata(p1)][A[i]] = 0;
for(int i = p1; i <= pstop; ++i)
A[i] += s;
for(int i = last(p1) ; i <= pstop; ++i)
S[this_bucata(p1)][A[i]] = 1;
for(int i = next_bucata(p1); i <= last_bucata(p2); ++i)
B[i] += s;
for(int i = pstart; i <= next(p2); ++i)
S[this_bucata(p2)][A[i]] = 0;
for(int i = pstart; i <= p2; ++i)
A[i] += s;
for(int i = pstart; i <= next(p2); ++i)
S[this_bucata(p2)][A[i]] = 1;
}
void cine(int s){
for(int i = 1; i <= nsqrt; ++i)
if(S[i][s - B[i]])
for(int j = nsqrt * (i-1) + 1; j <= nsqrt * i && j <= n; ++j)
if(A[j] + B[i] == s) { printf("%d\n", v[j]); return; }
printf("-1\n");
}
int main(){
freopen("arbore.in", "r", stdin);
freopen("arbore.out", "w", stdout);
scanf("%d%d", &n, &m);
for(int i = 1; i < n; ++i){
int x, y;
scanf("%d%d", &x, &y);
G[x].push_back(y);
G[y].push_back(x);
}
df(1);
nsqrt =(int) sqrt(n);
if(sqrt(n) != nsqrt) nsqrt++;
for(int i = 1; i <= m; ++i){
int x, s, p;
scanf("%d", &x);
if(x == 1){
scanf("%d%d", &p, &s);
bonus(p, s);
}
else {
scanf("%d", &s);
cine(s);
}
}
return 0;
}